This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit 9e14f9604bd17a061560b30d63571db4420df44b
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sun Jul 7 14:34:57 2024 -0400

    BufferedFileChannelInputStream.available() returns 0 before any reads.
---
 src/changes/changes.xml                               |  1 +
 .../io/input/BufferedFileChannelInputStream.java      |  3 +++
 .../io/input/BufferedFileChannelInputStreamTest.java  | 19 +++++++++++++++++++
 3 files changed, 23 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 56890eead..1057629cc 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -62,6 +62,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix"                due-to="Gary 
Gregory">PathUtils.isPosix(Path, LinkOption...) should return false on null 
input.</action>
       <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>
       <!-- 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>
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 593407230..93ff46b6d 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 final class BufferedFileChannelInputStream extends 
InputStream {
 
     @Override
     public synchronized int available() throws IOException {
+        if (!refill()) {
+            return EOF;
+        }
         return byteBuffer.remaining();
     }
 
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 f05ca23e8..479e336c0 100644
--- 
a/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java
+++ 
b/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java
@@ -16,12 +16,15 @@
  */
 package org.apache.commons.io.input;
 
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.StandardOpenOption;
 
+import org.apache.commons.io.IOUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -50,10 +53,26 @@ public class BufferedFileChannelInputStreamTest extends 
AbstractInputStreamTest
         //@formatter:on
     }
 
+    @Test
+    public void testAvailableAfterRead() throws Exception {
+        for (final InputStream inputStream : inputStreams) {
+            assertNotEquals(IOUtils.EOF, inputStream.read());
+            assertTrue(inputStream.available() > 0);
+        }
+    }
+
+    @Test
+    public void testAvailableFirst() throws Exception {
+        for (final InputStream inputStream : inputStreams) {
+            assertTrue(inputStream.available() > 0);
+        }
+    }
+
     @Test
     public void testBuilderGet() {
         // java.lang.IllegalStateException: origin == null
         assertThrows(IllegalStateException.class, () -> 
BufferedFileChannelInputStream.builder().get());
     }
 
+
 }

Reply via email to