Skip to content

Commit 9e14f96

Browse files
committed
BufferedFileChannelInputStream.available() returns 0 before any reads.
1 parent 0fd437d commit 9e14f96

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The <action> type attribute can be add,update,fix,remove.
6262
<action dev="ggregory" type="fix" due-to="Gary Gregory">PathUtils.isPosix(Path, LinkOption...) should return false on null input.</action>
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>
65+
<action dev="ggregory" type="add" due-to="Gary Gregory">BufferedFileChannelInputStream.available() returns 0 before any reads.</action>
6566
<!-- UPDATE -->
6667
<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>
6768
<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 (!refill()) {
173+
return EOF;
174+
}
172175
return byteBuffer.remaining();
173176
}
174177

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

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

19+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1920
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2022

2123
import java.io.IOException;
2224
import java.io.InputStream;
2325
import java.nio.file.StandardOpenOption;
2426

27+
import org.apache.commons.io.IOUtils;
2528
import org.junit.jupiter.api.BeforeEach;
2629
import org.junit.jupiter.api.Test;
2730

@@ -50,10 +53,26 @@ public void setUp() throws IOException {
5053
//@formatter:on
5154
}
5255

56+
@Test
57+
public void testAvailableAfterRead() throws Exception {
58+
for (final InputStream inputStream : inputStreams) {
59+
assertNotEquals(IOUtils.EOF, inputStream.read());
60+
assertTrue(inputStream.available() > 0);
61+
}
62+
}
63+
64+
@Test
65+
public void testAvailableFirst() throws Exception {
66+
for (final InputStream inputStream : inputStreams) {
67+
assertTrue(inputStream.available() > 0);
68+
}
69+
}
70+
5371
@Test
5472
public void testBuilderGet() {
5573
// java.lang.IllegalStateException: origin == null
5674
assertThrows(IllegalStateException.class, () -> BufferedFileChannelInputStream.builder().get());
5775
}
5876

77+
5978
}

0 commit comments

Comments
 (0)