Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to stream-based loading #11479

Merged
merged 16 commits into from
Jul 17, 2024
Prev Previous commit
Next Next commit
More change to asStream
  • Loading branch information
koppor committed Jul 11, 2024
commit 66d2d64309f8b5e8bad6dd7eeff9728cefa246d9
29 changes: 16 additions & 13 deletions src/main/java/org/jabref/logic/exporter/TemplateExporter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.jabref.logic.exporter;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -161,7 +164,7 @@ public void setCustomExport(boolean custom) {
*
* @param filename the filename
* @return a newly created reader
* @throws IOException if the reader could not be created
* @throws IOException if the reader could not be created (e.g., file is not found)
*/
private Reader getReader(String filename) throws IOException {
// If this is a custom export, just use the given filename:
Expand All @@ -171,24 +174,24 @@ private Reader getReader(String filename) throws IOException {
} else {
dir = LAYOUT_PREFIX + (directory == null ? "" : directory + '/');
}

// Attempt to get a Reader for the file path given, either by
// loading it as a resource (from within JAR), or as a normal file. If
// unsuccessful (e.g. file not found), an IOException is thrown.

String name = dir + filename;
// Try loading as a resource first. This works for files inside the JAR:
// If that did not work, try loading as a normal file URL:
try {
URL res = TemplateExporter.class.getResource(name);
Path reso;
if (res == null) {
reso = Path.of(name);
} else {
reso = Path.of(res.toURI());
}
return Files.newBufferedReader(reso, StandardCharsets.UTF_8);
} catch (FileNotFoundException | URISyntaxException ex) {

Path path = Path.of(name);
if (Files.exists(path)) {
return Files.newBufferedReader(path, StandardCharsets.UTF_8);
}

InputStream inputStream = TemplateExporter.class.getResourceAsStream(name);
if (inputStream == null) {
throw new IOException("Cannot find layout file: '" + name + "'.");
}

return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/logic/net/ssl/TrustStoreManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import org.glassfish.grizzly.streams.Input;
koppor marked this conversation as resolved.
Show resolved Hide resolved
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -148,9 +149,10 @@ public static void createTruststoreFileIfNotExist(Path storePath) {
try {
LOGGER.debug("Trust store path: {}", storePath.toAbsolutePath());
if (Files.notExists(storePath)) {
Path storeResourcePath = Path.of(TrustStoreManager.class.getResource("/ssl/truststore.jks").toURI());
Files.createDirectories(storePath.getParent());
Files.copy(storeResourcePath, storePath);
try (InputStream inputStream = TrustStoreManager.class.getResourceAsStream("/ssl/truststore.jks")) {
Files.copy(inputStream, storePath);
}
}

try {
Expand All @@ -160,8 +162,6 @@ public static void createTruststoreFileIfNotExist(Path storePath) {
}
} catch (IOException e) {
LOGGER.warn("Bad truststore path", e);
} catch (URISyntaxException e) {
LOGGER.warn("Bad resource path", e);
}
}

Expand Down
Loading