-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix MultiSourceReader compatibility with Java 16+ #3670
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
Merged
bbakerman
merged 1 commit into
graphql-java:master
from
gummybug:multisource-reader-java-16
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,13 +23,28 @@ | |
| @PublicApi | ||
| public class MultiSourceReader extends Reader { | ||
|
|
||
| // In Java version 16+, LineNumberReader.read considers end-of-stream to be a line terminator | ||
| // and will increment the line number, whereas in previous versions it doesn't. | ||
| private static final boolean LINE_NUMBER_READER_EOS_IS_TERMINATOR; | ||
|
|
||
| private final List<SourcePart> sourceParts; | ||
| private final StringBuilder data = new StringBuilder(); | ||
| private int currentIndex = 0; | ||
| private int overallLineNumber = 0; | ||
| private final boolean trackData; | ||
| private final LockKit.ReentrantLock readerLock = new LockKit.ReentrantLock(); | ||
|
|
||
| static { | ||
| LineNumberReader reader = new LineNumberReader(new StringReader("a")); | ||
| try { | ||
| reader.read(); | ||
| reader.read(); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| LINE_NUMBER_READER_EOS_IS_TERMINATOR = reader.getLineNumber() > 0; | ||
| } | ||
|
|
||
|
Member
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. Clever! |
||
|
|
||
| private MultiSourceReader(Builder builder) { | ||
| this.sourceParts = builder.sourceParts; | ||
|
|
@@ -46,10 +61,16 @@ public int read(char[] cbuf, int off, int len) throws IOException { | |
| } | ||
| SourcePart sourcePart = sourceParts.get(currentIndex); | ||
| int read = sourcePart.lineReader.read(cbuf, off, len); | ||
| overallLineNumber = calcLineNumber(); | ||
| if (read == -1) { | ||
| currentIndex++; | ||
| } else { | ||
| sourcePart.reachedEndOfStream = true; | ||
| } else if (read > 0) { | ||
| sourcePart.lastRead = cbuf[off + read - 1]; | ||
| } | ||
| // note: calcLineNumber() must be called after updating sourcePart.reachedEndOfStream | ||
| // and sourcePart.lastRead | ||
| overallLineNumber = calcLineNumber(); | ||
| if (read != -1) { | ||
| trackData(cbuf, off, read); | ||
| return read; | ||
| } | ||
|
|
@@ -68,7 +89,7 @@ private void trackData(char[] cbuf, int off, int len) { | |
| private int calcLineNumber() { | ||
| int linenumber = 0; | ||
| for (SourcePart sourcePart : sourceParts) { | ||
| linenumber += sourcePart.lineReader.getLineNumber(); | ||
| linenumber += sourcePart.getLineNumber(); | ||
| } | ||
| return linenumber; | ||
| } | ||
|
|
@@ -125,7 +146,7 @@ public SourceAndLine getSourceAndLineFromOverallLine(int overallLineNumber) { | |
| sourceAndLine.sourceName = sourcePart.sourceName; | ||
| if (sourcePart == currentPart) { | ||
| // we cant go any further | ||
| int partLineNumber = currentPart.lineReader.getLineNumber(); | ||
| int partLineNumber = currentPart.getLineNumber(); | ||
| previousPage = page; | ||
| page += partLineNumber; | ||
| if (page > overallLineNumber) { | ||
|
|
@@ -136,7 +157,7 @@ public SourceAndLine getSourceAndLineFromOverallLine(int overallLineNumber) { | |
| return sourceAndLine; | ||
| } else { | ||
| previousPage = page; | ||
| int partLineNumber = sourcePart.lineReader.getLineNumber(); | ||
| int partLineNumber = sourcePart.getLineNumber(); | ||
| page += partLineNumber; | ||
| if (page > overallLineNumber) { | ||
| sourceAndLine.line = overallLineNumber - previousPage; | ||
|
|
@@ -157,9 +178,9 @@ public int getLineNumber() { | |
| return 0; | ||
| } | ||
| if (currentIndex >= sourceParts.size()) { | ||
| return sourceParts.get(sourceParts.size() - 1).lineReader.getLineNumber(); | ||
| return sourceParts.get(sourceParts.size() - 1).getLineNumber(); | ||
| } | ||
| return sourceParts.get(currentIndex).lineReader.getLineNumber(); | ||
| return sourceParts.get(currentIndex).getLineNumber(); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -220,6 +241,24 @@ private static class SourcePart { | |
| String sourceName; | ||
| LineNumberReader lineReader; | ||
| boolean closed; | ||
| char lastRead; | ||
| boolean reachedEndOfStream = false; | ||
|
|
||
| /** | ||
| * This handles the discrepancy between LineNumberReader.getLineNumber() for Java versions | ||
| * 16+ vs below. Use this instead of lineReader.getLineNumber() directly. | ||
| * @return The current line number. EOS is not considered a line terminator. | ||
| */ | ||
| int getLineNumber() { | ||
| int lineNumber = lineReader.getLineNumber(); | ||
| if (reachedEndOfStream | ||
| && LINE_NUMBER_READER_EOS_IS_TERMINATOR | ||
| && lastRead != '\r' | ||
| && lastRead != '\n') { | ||
| return Math.max(lineNumber - 1, 0); | ||
| } | ||
| return lineNumber; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Minor pick - I would encapsulate this into a static method
So you can do