You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "ppkarwasz (via GitHub)" <gi...@apache.org> on 2025/09/05 13:05:24 UTC

[PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

ppkarwasz opened a new pull request, #779:
URL: https://github.com/apache/commons-io/pull/779

   > [!NOTE]
   > This PR depends on the fixes proposed in #778. It should not be marked as ready for review until that PR has been approved or rejected.
   
   `BoundedInputStream.available()` previously returned the underlying stream’s availability without applying the configured read limit. It now caps the reported value by the remaining bytes allowed by the bound.
   
   Additional changes:
   
   * Removed the `onMaxLength` call from `available()`, since `available()` does not consume data and cannot exceed the maximum count.
   * Reworked the `testAvailableAfterClose` and `testReadAfterClose` tests:
   
     * Old versions relied too heavily on `CharSequenceInputStream`’s specific behavior.
     * New parameterized tests cover all three typical cases of the underlying stream after close: no-op, return special value, or throw an exception.
   * Added a dedicated unit test for the corrected `available()` behavior.
   
   Before you push a pull request, review this list:
   
   - [x] I used AI to improve unit tests and documentation.
   - [x] Run a successful build using the default [Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command line by itself.
   - [x] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best-practice.
   - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.
   


-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #779:
URL: https://github.com/apache/commons-io/pull/779#discussion_r2325737526


##########
src/main/java/org/apache/commons/io/input/BoundedInputStream.java:
##########
@@ -427,13 +421,19 @@ public long getMaxLength() {
     }
 
     /**
-     * Gets how many bytes remain to read.
+     * Returns the bytes remaining before the configured read limit is reached.

Review Comment:
   See above about documenting getter methods.



##########
src/main/java/org/apache/commons/io/input/BoundedInputStream.java:
##########
@@ -405,9 +399,9 @@ public synchronized long getCount() {
     }
 
     /**
-     * Gets the max count of bytes to read.
+     * Returns the maximum number of bytes this stream is allowed to read.
      *
-     * @return The max count of bytes to read.
+     * @return the maximum number of bytes permitted, or {@value IOUtils#EOF} if unbounded.

Review Comment:
   Please do not change the Commons convention that a sentence starts with a capital letter.



##########
src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java:
##########
@@ -80,21 +91,75 @@ void testAfterReadConsumer() throws Exception {
         // @formatter:on
     }
 
-    @SuppressWarnings("resource")
-    @Test
-    void testAvailableAfterClose() throws Exception {
+    static Stream<Arguments> testAvailableAfterClose() throws IOException {
+        // Case 1: behaves like ByteArrayInputStream — close() is a no-op, available() still returns a value (e.g., 42).
+        final InputStream noOpClose = mock(InputStream.class);
+        when(noOpClose.available()).thenReturn(42, 42);
+
+        // Case 2: returns 0 after close (Commons memory-backed streams that ignore close but report 0 when exhausted).
+        final InputStream returnsZeroAfterClose = mock(InputStream.class);
+        when(returnsZeroAfterClose.available()).thenReturn(42, 0);
+
+        // Case 3: throws IOException after close (e.g., FileInputStream-like behavior).
+        final InputStream throwsAfterClose = mock(InputStream.class);
+        when(throwsAfterClose.available()).thenReturn(42).thenThrow(new IOException("Stream closed"));
+
+        return Stream.of(
+                Arguments.of("no-op close → still returns 42", noOpClose, 42),

Review Comment:
   The arrow Unicode character is unnecessary IMO, elsewhere in this PR, you use "->", are either better explained with a word? Does arrow mean "convert", or "returns", or...?



##########
src/main/java/org/apache/commons/io/input/BoundedInputStream.java:
##########
@@ -370,16 +370,10 @@ protected synchronized void afterRead(final int n) throws IOException {
         super.afterRead(n);
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public int available() throws IOException {
-        if (isMaxCount()) {
-            onMaxLength(maxCount, getCount());
-            return 0;
-        }
-        return in.available();
+        final int remaining = Math.toIntExact(Math.min(getRemaining(), Integer.MAX_VALUE));

Review Comment:
   This method now throws `ArithmeticException`; should we:
   - Javadoc it, or,
   - Convert it to an `IllegalStateException`, or,
   - Something else?
   



##########
src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java:
##########
@@ -203,10 +268,10 @@ void testCounts(final long startCount) throws Exception {
 
     @Test
     void testMarkReset() throws Exception {
-        final byte[] helloWorld = "Hello World".getBytes(StandardCharsets.UTF_8);
+        final byte[] helloWorld = "Hello World".getBytes(UTF_8);

Review Comment:
   In general, I would not change the access style in a PR because there is now more to review.



##########
src/main/java/org/apache/commons/io/input/BoundedInputStream.java:
##########
@@ -405,9 +399,9 @@ public synchronized long getCount() {
     }
 
     /**
-     * Gets the max count of bytes to read.
+     * Returns the maximum number of bytes this stream is allowed to read.

Review Comment:
   In general, Commons uses the convention that a getter method "Gets something", a setter method "Sets something", and an is method "Tests something". Please do not change the style here.



##########
src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java:
##########
@@ -80,21 +91,75 @@ void testAfterReadConsumer() throws Exception {
         // @formatter:on
     }
 
-    @SuppressWarnings("resource")
-    @Test
-    void testAvailableAfterClose() throws Exception {
+    static Stream<Arguments> testAvailableAfterClose() throws IOException {
+        // Case 1: behaves like ByteArrayInputStream — close() is a no-op, available() still returns a value (e.g., 42).
+        final InputStream noOpClose = mock(InputStream.class);
+        when(noOpClose.available()).thenReturn(42, 42);
+
+        // Case 2: returns 0 after close (Commons memory-backed streams that ignore close but report 0 when exhausted).
+        final InputStream returnsZeroAfterClose = mock(InputStream.class);
+        when(returnsZeroAfterClose.available()).thenReturn(42, 0);
+
+        // Case 3: throws IOException after close (e.g., FileInputStream-like behavior).
+        final InputStream throwsAfterClose = mock(InputStream.class);
+        when(throwsAfterClose.available()).thenReturn(42).thenThrow(new IOException("Stream closed"));
+
+        return Stream.of(
+                Arguments.of("no-op close → still returns 42", noOpClose, 42),
+                Arguments.of("after close → returns 0", returnsZeroAfterClose, 42),
+                Arguments.of("after close → throws IOException", throwsAfterClose, 42));
+    }
+
+    @DisplayName("available() after close follows underlying stream semantics")

Review Comment:
   In general, we don't use `@DisplayName`; using Javadoc is both richer in formatting and more versatile.



-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory merged PR #779:
URL: https://github.com/apache/commons-io/pull/779


-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #779:
URL: https://github.com/apache/commons-io/pull/779#discussion_r2329731761


##########
src/main/java/org/apache/commons/io/input/BoundedInputStream.java:
##########
@@ -427,13 +421,19 @@ public long getMaxLength() {
     }
 
     /**
-     * Gets how many bytes remain to read.
+     * Returns the bytes remaining before the configured read limit is reached.

Review Comment:
   Fixed in https://github.com/apache/commons-io/pull/779/commits/c831b562f656559e246da43197cf8fe7d5751f45



-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #779:
URL: https://github.com/apache/commons-io/pull/779#discussion_r2329732635


##########
src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java:
##########
@@ -80,21 +91,75 @@ void testAfterReadConsumer() throws Exception {
         // @formatter:on
     }
 
-    @SuppressWarnings("resource")
-    @Test
-    void testAvailableAfterClose() throws Exception {
+    static Stream<Arguments> testAvailableAfterClose() throws IOException {
+        // Case 1: behaves like ByteArrayInputStream — close() is a no-op, available() still returns a value (e.g., 42).
+        final InputStream noOpClose = mock(InputStream.class);
+        when(noOpClose.available()).thenReturn(42, 42);
+
+        // Case 2: returns 0 after close (Commons memory-backed streams that ignore close but report 0 when exhausted).
+        final InputStream returnsZeroAfterClose = mock(InputStream.class);
+        when(returnsZeroAfterClose.available()).thenReturn(42, 0);
+
+        // Case 3: throws IOException after close (e.g., FileInputStream-like behavior).
+        final InputStream throwsAfterClose = mock(InputStream.class);
+        when(throwsAfterClose.available()).thenReturn(42).thenThrow(new IOException("Stream closed"));
+
+        return Stream.of(
+                Arguments.of("no-op close → still returns 42", noOpClose, 42),
+                Arguments.of("after close → returns 0", returnsZeroAfterClose, 42),
+                Arguments.of("after close → throws IOException", throwsAfterClose, 42));
+    }
+
+    @DisplayName("available() after close follows underlying stream semantics")

Review Comment:
   Fixed in https://github.com/apache/commons-io/pull/779/commits/42208f18145e07a8a5b6660b593d1a8d0c4f29dd



-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #779:
URL: https://github.com/apache/commons-io/pull/779#discussion_r2329756637


##########
src/main/java/org/apache/commons/io/input/BoundedInputStream.java:
##########
@@ -370,16 +370,10 @@ protected synchronized void afterRead(final int n) throws IOException {
         super.afterRead(n);
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public int available() throws IOException {
-        if (isMaxCount()) {
-            onMaxLength(maxCount, getCount());
-            return 0;
-        }
-        return in.available();
+        final int remaining = Math.toIntExact(Math.min(getRemaining(), Integer.MAX_VALUE));

Review Comment:
   The value is between `0` (minimum value of `getRemaining()`) and `Integer.MAX_VALUE`, so the call never throws.
   
   In https://github.com/apache/commons-io/pull/779/commits/ac15e4c30c1693df110ea754696ef702b01b59c8 I replaced it with a simple cast.



-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #779:
URL: https://github.com/apache/commons-io/pull/779#discussion_r2329746955


##########
src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java:
##########
@@ -80,21 +91,75 @@ void testAfterReadConsumer() throws Exception {
         // @formatter:on
     }
 
-    @SuppressWarnings("resource")
-    @Test
-    void testAvailableAfterClose() throws Exception {
+    static Stream<Arguments> testAvailableAfterClose() throws IOException {
+        // Case 1: behaves like ByteArrayInputStream — close() is a no-op, available() still returns a value (e.g., 42).
+        final InputStream noOpClose = mock(InputStream.class);
+        when(noOpClose.available()).thenReturn(42, 42);
+
+        // Case 2: returns 0 after close (Commons memory-backed streams that ignore close but report 0 when exhausted).
+        final InputStream returnsZeroAfterClose = mock(InputStream.class);
+        when(returnsZeroAfterClose.available()).thenReturn(42, 0);
+
+        // Case 3: throws IOException after close (e.g., FileInputStream-like behavior).
+        final InputStream throwsAfterClose = mock(InputStream.class);
+        when(throwsAfterClose.available()).thenReturn(42).thenThrow(new IOException("Stream closed"));
+
+        return Stream.of(
+                Arguments.of("no-op close → still returns 42", noOpClose, 42),

Review Comment:
   Fixed in https://github.com/apache/commons-io/pull/779/commits/403000c3087583c673ab5b6d8b56099394edd703



-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #779:
URL: https://github.com/apache/commons-io/pull/779#discussion_r2329730820


##########
src/main/java/org/apache/commons/io/input/BoundedInputStream.java:
##########
@@ -405,9 +399,9 @@ public synchronized long getCount() {
     }
 
     /**
-     * Gets the max count of bytes to read.
+     * Returns the maximum number of bytes this stream is allowed to read.

Review Comment:
   Fixed in https://github.com/apache/commons-io/pull/779/commits/c831b562f656559e246da43197cf8fe7d5751f45



##########
src/main/java/org/apache/commons/io/input/BoundedInputStream.java:
##########
@@ -405,9 +399,9 @@ public synchronized long getCount() {
     }
 
     /**
-     * Gets the max count of bytes to read.
+     * Returns the maximum number of bytes this stream is allowed to read.
      *
-     * @return The max count of bytes to read.
+     * @return the maximum number of bytes permitted, or {@value IOUtils#EOF} if unbounded.

Review Comment:
   Fixed in https://github.com/apache/commons-io/pull/779/commits/c831b562f656559e246da43197cf8fe7d5751f45



-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #779:
URL: https://github.com/apache/commons-io/pull/779#discussion_r2329733267


##########
src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java:
##########
@@ -203,10 +268,10 @@ void testCounts(final long startCount) throws Exception {
 
     @Test
     void testMarkReset() throws Exception {
-        final byte[] helloWorld = "Hello World".getBytes(StandardCharsets.UTF_8);
+        final byte[] helloWorld = "Hello World".getBytes(UTF_8);

Review Comment:
   Fixed in https://github.com/apache/commons-io/pull/779/commits/42208f18145e07a8a5b6660b593d1a8d0c4f29dd



-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix: respect read limit in BoundedInputStream.available() [commons-io]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #779:
URL: https://github.com/apache/commons-io/pull/779#issuecomment-3266746579

   Merged, thank you @ppkarwasz.


-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org