You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/09/21 20:16:54 UTC

(commons-compress) branch master updated: SeekableInMemoryByteChannel.position(long) now throws IllegalArgumentException instead of IOException when the position request is negative, complying with the SeekableByteChannel.position(long) contract

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


The following commit(s) were added to refs/heads/master by this push:
     new 5cdc27900 SeekableInMemoryByteChannel.position(long) now throws IllegalArgumentException instead of IOException when the position request is negative, complying with the SeekableByteChannel.position(long) contract
5cdc27900 is described below

commit 5cdc2790052522ca631275ea2641b5269b9a74ac
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 21 16:16:49 2025 -0400

    SeekableInMemoryByteChannel.position(long) now throws
    IllegalArgumentException instead of IOException when the position
    request is negative, complying with the
    SeekableByteChannel.position(long) contract
---
 src/changes/changes.xml                                               | 1 +
 .../apache/commons/compress/utils/SeekableInMemoryByteChannel.java    | 2 +-
 .../commons/compress/utils/SeekableInMemoryByteChannelTest.java       | 4 ++--
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 435445ce6..4f5f9cd2a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -101,6 +101,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Martin Wiesner, Gary Gregory">Fix grammar issues and typos #687.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">SeekableInMemoryByteChannel.SeekableInMemoryByteChannel() internal buffer now defaults to IOUtils.DEFAULT_BUFFER_SIZE.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">SeekableInMemoryByteChannel.position(), size(), and truncate() now comply with the SeekableByteChannel contract and throw ClosedChannelException.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory">SeekableInMemoryByteChannel.position(long) now throws IllegalArgumentException instead of IOException when the position request is negative, complying with the SeekableByteChannel.position(long) contract.</action>
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add MemoryLimitException.MemoryLimitException(long, long).</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add CompressException.CompressException(String, Object...).</action>
diff --git a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
index 88348a3e7..9c642b1a9 100644
--- a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
+++ b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
@@ -124,7 +124,7 @@ public long position() throws ClosedChannelException {
     public SeekableByteChannel position(final long newPosition) throws IOException {
         ensureOpen();
         if (newPosition < 0L || newPosition > Integer.MAX_VALUE) {
-            throw new IOException("Position must be in range [0.." + Integer.MAX_VALUE + "]");
+            throw new IllegalArgumentException(String.format("Position must be in range [0..%,d]: %,d", Integer.MAX_VALUE, newPosition));
         }
         position = (int) newPosition;
         return this;
diff --git a/src/test/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannelTest.java b/src/test/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannelTest.java
index 35c44599c..711ca6d2a 100644
--- a/src/test/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannelTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannelTest.java
@@ -154,7 +154,7 @@ void testShouldThrowExceptionOnWritingToClosedChannel() {
     @Test
     void testShouldThrowExceptionWhenSettingIncorrectPosition() {
         try (SeekableInMemoryByteChannel c = new SeekableInMemoryByteChannel()) {
-            assertThrows(IOException.class, () -> c.position(Integer.MAX_VALUE + 1L));
+            assertThrows(IllegalArgumentException.class, () -> c.position(Integer.MAX_VALUE + 1L));
         }
     }
 
@@ -230,7 +230,7 @@ void testThrowsIllegalArgumentExceptionWhenTruncatingToANegativeSize() throws Ex
     @Test
     void testThrowsIOExceptionWhenPositionIsSetToANegativeValue() throws Exception {
         try (SeekableByteChannel c = new SeekableInMemoryByteChannel()) {
-            assertThrows(IOException.class, () -> c.position(-1));
+            assertThrows(IllegalArgumentException.class, () -> c.position(-1));
         }
     }