-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 2 commits
2655432
157a448
cad404c
5dbc4a3
11c1be7
aa8f549
ba104cb
c140774
66d2d64
338f7b0
17aa7c0
acee78e
4e3a407
599b980
f36dfa3
0f068f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||||||
|
@@ -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))) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works without here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But might not work at runtime There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried with |
||||||
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)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BufferedReader#lines seems to be somewhat of an equivalent here... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
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); | ||||||
} | ||||||
} | ||||||
|
||||||
|
There was a problem hiding this comment.
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