diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1057629cc7e..96379a3fcc7 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -63,6 +63,7 @@ The type attribute can be add,update,fix,remove. AutoCloseInputStream(InputStream) uses ClosedInputStream.INSTANCE when its input is null. Avoid NullPointerException in ProxyInputStream.available() when the underlying input stream is null. BufferedFileChannelInputStream.available() returns 0 before any reads. + BufferedFileChannelInputStream.available() should return 0 when the stream is closed instead of throwing an exception. Bump tests commons.bytebuddy.version from 1.14.13 to 1.14.17 #615, #621, #631, #635. Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0. 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 93ff46b6d26..bffb89d7880 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 BufferedFileChannelInputStream(final Path path, final int bufferSize) thr @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 479e336c07e..8046a32490d 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 void setUp() throws IOException { //@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) {