Skip to content

Commit 016caf5

Browse files
committed
BufferedFileChannelInputStream.read() should return -1 after the stream
is closed
1 parent 6a625e9 commit 016caf5

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ The <action> type attribute can be add,update,fix,remove.
6969
<action dev="ggregory" type="add" due-to="Gary Gregory">CircularInputStream.available() should return 0 when the stream is closed.</action>
7070
<action dev="ggregory" type="add" due-to="Gary Gregory">InfiniteCircularInputStream.available() should return 0 when the stream is closed.</action>
7171
<action dev="ggregory" type="add" due-to="Gary Gregory">ChecksumInputStream(InputStream, Checksum, long, long) should fail-fast on null Checksum input.</action>
72+
<action dev="ggregory" type="add" due-to="Gary Gregory">BufferedFileChannelInputStream.read() should return -1 after the stream is closed.</action>
7273
<!-- UPDATE -->
7374
<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>
7475
<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
@@ -243,6 +243,9 @@ public synchronized int read(final byte[] b, final int offset, int len) throws I
243243
* @throws IOException if an I/O error occurs.
244244
*/
245245
private boolean refill() throws IOException {
246+
if (!fileChannel.isOpen()) {
247+
return false;
248+
}
246249
if (!byteBuffer.hasRemaining()) {
247250
byteBuffer.clear();
248251
int nRead = 0;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,13 @@ public void testBuilderGet() {
8383
assertThrows(IllegalStateException.class, () -> BufferedFileChannelInputStream.builder().get());
8484
}
8585

86+
@Test
87+
public void testReadAfterClose() throws Exception {
88+
for (final InputStream inputStream : inputStreams) {
89+
inputStream.close();
90+
assertEquals(IOUtils.EOF, inputStream.read());
91+
}
92+
}
93+
8694

8795
}

0 commit comments

Comments
 (0)