ppkarwasz commented on code in PR #756:
URL: https://github.com/apache/commons-compress/pull/756#discussion_r2599999011


##########
src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java:
##########
@@ -160,36 +163,42 @@ 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;
+        // intPos <= Integer.MAX_VALUE
+        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 {

Review Comment:
   I didn't catch it before, because I didn't have the Javadoc in mind, however 
the generic [`WritableByteChannel#write` 
Javadoc](https://docs.oracle.com/javase/8/docs/api/java/nio/channels/WritableByteChannel.html)
 says:
   
   > Unless otherwise specified, a write operation will return only after 
writing all of the `r` requested bytes.
   
   So this edge case should throw instead of decreasing `wanted`. Do you agree?



-- 
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]

Reply via email to