Skip to content

Commit e0e69af

Browse files
committed
BufferedFileChannelInputStream.available() should return 0 when the
stream is closed instead of throwing an exception
1 parent 9e14f96 commit e0e69af

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ The <action> type attribute can be add,update,fix,remove.
6363
<action dev="ggregory" type="fix" due-to="Gary Gregory">AutoCloseInputStream(InputStream) uses ClosedInputStream.INSTANCE when its input is null.</action>
6464
<action dev="ggregory" type="add" due-to="Gary Gregory">Avoid NullPointerException in ProxyInputStream.available() when the underlying input stream is null.</action>
6565
<action dev="ggregory" type="add" due-to="Gary Gregory">BufferedFileChannelInputStream.available() returns 0 before any reads.</action>
66+
<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>
6667
<!-- UPDATE -->
6768
<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>
6869
<action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0.</action>

src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ public BufferedFileChannelInputStream(final Path path, final int bufferSize) thr
169169

170170
@Override
171171
public synchronized int available() throws IOException {
172+
if (!fileChannel.isOpen()) {
173+
return 0;
174+
}
172175
if (!refill()) {
173176
return EOF;
174177
}

src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19+
import static org.junit.jupiter.api.Assertions.assertEquals;
1920
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2021
import static org.junit.jupiter.api.Assertions.assertThrows;
2122
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -53,6 +54,14 @@ public void setUp() throws IOException {
5354
//@formatter:on
5455
}
5556

57+
@Test
58+
public void testAvailableAfterClose() throws Exception {
59+
for (final InputStream inputStream : inputStreams) {
60+
inputStream.close();
61+
assertEquals(0, inputStream.available());
62+
}
63+
}
64+
5665
@Test
5766
public void testAvailableAfterRead() throws Exception {
5867
for (final InputStream inputStream : inputStreams) {

0 commit comments

Comments
 (0)