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-io.git
commit e0e69af621294fb0d9c665666d20ad6f421cdf06 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Jul 7 14:39:36 2024 -0400 BufferedFileChannelInputStream.available() should return 0 when the stream is closed instead of throwing an exception --- src/changes/changes.xml | 1 + .../apache/commons/io/input/BufferedFileChannelInputStream.java | 3 +++ .../commons/io/input/BufferedFileChannelInputStreamTest.java | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1057629cc..96379a3fc 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -63,6 +63,7 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="fix" due-to="Gary Gregory">AutoCloseInputStream(InputStream) uses ClosedInputStream.INSTANCE when its input is null.</action> <action dev="ggregory" type="add" due-to="Gary Gregory">Avoid NullPointerException in ProxyInputStream.available() when the underlying input stream is null.</action> <action dev="ggregory" type="add" due-to="Gary Gregory">BufferedFileChannelInputStream.available() returns 0 before any reads.</action> + <action dev="ggregory" type="add" due-to="Gary Gregory">BufferedFileChannelInputStream.available() should return 0 when the stream is closed instead of throwing an exception.</action> <!-- UPDATE --> <action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons.bytebuddy.version from 1.14.13 to 1.14.17 #615, #621, #631, #635.</action> <action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0.</action> diff --git a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java index 93ff46b6d..bffb89d78 100644 --- a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java +++ b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java @@ -169,6 +169,9 @@ public final class BufferedFileChannelInputStream extends InputStream { @Override public synchronized int available() throws IOException { + if (!fileChannel.isOpen()) { + return 0; + } if (!refill()) { return EOF; } diff --git a/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java b/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java index 479e336c0..8046a3249 100644 --- a/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.io.input; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -53,6 +54,14 @@ public class BufferedFileChannelInputStreamTest extends AbstractInputStreamTest //@formatter:on } + @Test + public void testAvailableAfterClose() throws Exception { + for (final InputStream inputStream : inputStreams) { + inputStream.close(); + assertEquals(0, inputStream.available()); + } + } + @Test public void testAvailableAfterRead() throws Exception { for (final InputStream inputStream : inputStreams) {