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
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/theme/StyleSheetFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public URL getSceneStylesheet() {
* This method allows callers to obtain the theme's additional stylesheet.
*
* @return the stylesheet location if there is an additional stylesheet present and available. The
* location will be a local URL. Typically it will be a {@code 'data:'} URL where the CSS is embedded. However for
* location will be a local URL. Typically, it will be a {@code 'data:'} URL where the CSS is embedded. However, for
* large themes it can be {@code 'file:'}.
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.jabref.logic.protectedterms;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jabref.logic.l10n.Localization;
Expand All @@ -29,30 +30,37 @@ public class ProtectedTermsParser {
private String location;

public void readTermsFromResource(String resourceFileName, String descriptionString) {
try {
Path path = Path.of(Objects.requireNonNull(ProtectedTermsLoader.class.getResource(Objects.requireNonNull(resourceFileName))).toURI());
readTermsList(path);
description = descriptionString;
location = resourceFileName;
} catch (URISyntaxException e1) {
LOGGER.error("");
description = descriptionString;
location = resourceFileName;
try (InputStream inputStream = ProtectedTermsLoader.class.getResourceAsStream(Objects.requireNonNull(resourceFileName))) {
Copy link
Member

@Siedlerchr Siedlerchr Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use getClassLoader() first, don't ask me why but for getResourceAsStream it does not work otherwise

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
try (InputStream inputStream = ProtectedTermsLoader.class.getResourceAsStream(Objects.requireNonNull(resourceFileName))) {
try (InputStream inputStream = ProtectedTermsLoader.class.getClassLoader().getResourceAsStream(Objects.requireNonNull(resourceFileName))) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works without here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But might not work at runtime

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried with ./gradlew run and then protected terms. Worked.

if (inputStream == null) {
LOGGER.error("Cannot find resource '{}' ({})", resourceFileName, descriptionString);
return;
}
readTermsList(inputStream);
} catch (IOException e) {
LOGGER.error("Cannot open resource '{}'", resourceFileName, e);
}
}

public void readTermsFromFile(Path path) {
location = path.toAbsolutePath().toString();
readTermsList(path);
}

private void readTermsList(Path path) {
path = path.toAbsolutePath();
if (!Files.exists(path)) {
LOGGER.warn("Could not read terms from file {}", path);
return;
}
try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files lines does not work with graal? Tbh this looks like a major drawback here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BufferedReader#lines seems to be somewhat of an equivalent here...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 60, I use exactly this function.

This call is only a wrapper for the call if a user supplies a file!

GraalVM does not support .class.getResource( with hacks. See oracle/graal#7682 for a longer discussion. Especially oracle/graal#7682 (comment)

The reason NativeImageFileSystem behaves like JarFileSystem, not like UnixFileSystem, is that, unlike UnixFileSystem, you can't, for example, delete a file (resource) on both NativeImageFileSystem and JarFileSystem. The resources in NativeImageFileSystem are baked into the read-only section of the image heap

this.terms.addAll(lines.map(this::setDescription).filter(Objects::nonNull).collect(Collectors.toList()));
try (InputStream inputStream = Files.newInputStream(path)) {
readTermsList(inputStream);
} catch (IOException e) {
LOGGER.warn("Could not read terms from file {}", path, e);
LOGGER.error("Cannot open file '{}'", path, e);
}
}

private void readTermsList(InputStream inputStream) {
try (Stream<String> lines = new BufferedReader(new InputStreamReader(inputStream)).lines()) {
this.terms.addAll(lines.map(this::setDescription).filter(Objects::nonNull).toList());
} catch (UncheckedIOException e) {
LOGGER.warn("Could not read terms from stream", e);
}
}

Expand Down
Loading