ppkarwasz commented on code in PR #756:
URL: https://github.com/apache/commons-compress/pull/756#discussion_r2599029596
##########
src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java:
##########
@@ -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;
Review Comment:
_Nit_: What about reusing `intPos`? Also a comment might be useful to
explain that `intPos + wanted` is at most `(Integer.MAX_VALUE - intPos) +
intPos`.
```java
position = intPos += wanted;
if (size < intPos) {
size = intPos;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]