Skip to content

Commit

Permalink
BufferedFileChannelInputStream.available() should return 0 when the
Browse files Browse the repository at this point in the history
stream is closed instead of throwing an exception
  • Loading branch information
garydgregory committed Jul 7, 2024
1 parent 9e14f96 commit e0e69af
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e0e69af

Please sign in to comment.