-
Notifications
You must be signed in to change notification settings - Fork 304
SeekableInMemoryByteChannel.position(long) shouldn't throw an IllegalArgumentException for a new positive position that's too large #756
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
garydgregory
merged 3 commits into
apache:master
from
garydgregory:bugfix/SeekableInMemoryByteChannel.position(long)
Dec 8, 2025
+46
−20
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ public class SeekableInMemoryByteChannel implements SeekableByteChannel { | |
| private static final int NAIVE_RESIZE_LIMIT = Integer.MAX_VALUE >> 1; | ||
| private byte[] data; | ||
| private final AtomicBoolean closed = new AtomicBoolean(); | ||
| private int position; | ||
| private long position; | ||
| private int size; | ||
|
|
||
| /** | ||
|
|
@@ -113,25 +113,28 @@ public long position() throws ClosedChannelException { | |
| @Override | ||
| public SeekableByteChannel position(final long newPosition) throws IOException { | ||
| ensureOpen(); | ||
| if (newPosition < 0L || newPosition > Integer.MAX_VALUE) { | ||
| throw new IllegalArgumentException(String.format("Position must be in range [0..%,d]: %,d", Integer.MAX_VALUE, newPosition)); | ||
| if (newPosition < 0L) { | ||
| throw new IllegalArgumentException(String.format("New position is negative: %,d", newPosition)); | ||
| } | ||
| position = (int) newPosition; | ||
| position = newPosition; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public int read(final ByteBuffer buf) throws IOException { | ||
| ensureOpen(); | ||
| if (position > Integer.MAX_VALUE) { | ||
| return -1; | ||
| } | ||
| int wanted = buf.remaining(); | ||
| final int possible = size - position; | ||
| final int possible = size - (int) position; | ||
| if (possible <= 0) { | ||
| return -1; | ||
| } | ||
| if (wanted > possible) { | ||
| wanted = possible; | ||
| } | ||
| buf.put(data, position, wanted); | ||
| buf.put(data, (int) position, wanted); | ||
| position += wanted; | ||
| return wanted; | ||
| } | ||
|
|
@@ -160,36 +163,41 @@ public long size() throws ClosedChannelException { | |
| @Override | ||
| public SeekableByteChannel truncate(final long newSize) throws ClosedChannelException { | ||
| ensureOpen(); | ||
| if (newSize < 0L || newSize > Integer.MAX_VALUE) { | ||
| throw new IllegalArgumentException("Size must be range [0.." + Integer.MAX_VALUE + "]"); | ||
| if (newSize < 0L) { | ||
| throw new IllegalArgumentException(String.format("New size is negative: %,d", newSize)); | ||
| } | ||
| if (size > newSize) { | ||
| size = (int) newSize; | ||
| } | ||
| if (position > newSize) { | ||
| position = (int) newSize; | ||
| position = newSize; | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public int write(final ByteBuffer b) throws IOException { | ||
| ensureOpen(); | ||
| if (position > Integer.MAX_VALUE) { | ||
| throw new IOException("position > Integer.MAX_VALUE"); | ||
| } | ||
| int wanted = b.remaining(); | ||
| final int possibleWithoutResize = size - position; | ||
| final int intPos = (int) position; | ||
| final int possibleWithoutResize = size - intPos; | ||
| if (wanted > possibleWithoutResize) { | ||
| final int newSize = position + wanted; | ||
| final int newSize = intPos + wanted; | ||
| if (newSize < 0) { // overflow | ||
| resize(Integer.MAX_VALUE); | ||
| wanted = Integer.MAX_VALUE - position; | ||
| wanted = Integer.MAX_VALUE - intPos; | ||
| } else { | ||
| resize(newSize); | ||
| } | ||
| } | ||
| b.get(data, position, wanted); | ||
| b.get(data, intPos, wanted); | ||
| position += wanted; | ||
| if (size < position) { | ||
| size = position; | ||
| final int intPos2 = (int) position; | ||
| if (size < intPos2) { | ||
| size = intPos2; | ||
|
||
| } | ||
| return wanted; | ||
| } | ||
|
|
||
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
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.
I didn't catch it before, because I didn't have the Javadoc in mind, however the generic
WritableByteChannel#writeJavadoc says:So this edge case should throw instead of decreasing
wanted. Do you agree?