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 10d74fc18a076ecbfe7bac6e3b5bd46d504ea75c
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sun Jul 7 14:17:01 2024 -0400

    Avoid NullPointerException in ProxyInputStream.available() when the
    underlying input stream is null
---
 src/changes/changes.xml                            |  1 +
 .../apache/commons/io/input/ProxyInputStream.java  | 12 ++++++----
 .../commons/io/input/ProxyInputStreamTest.java     | 28 +++++++++++++++++++++-
 3 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index bf43e4bde..56890eead 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -61,6 +61,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix"                due-to="sullis">Add 
test for CircularByteBuffer clear() #620.</action>
       <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>
       <!-- 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/ProxyInputStream.java 
b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java
index c412d04fd..806c058de 100644
--- a/src/main/java/org/apache/commons/io/input/ProxyInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java
@@ -87,12 +87,14 @@ public abstract class ProxyInputStream extends 
FilterInputStream {
      */
     @Override
     public int available() throws IOException {
-        try {
-            return super.available();
-        } catch (final IOException e) {
-            handleIOException(e);
-            return 0;
+        if (in != null) {
+            try {
+                return in.available();
+            } catch (final IOException e) {
+                handleIOException(e);
+            }
         }
+        return 0;
     }
 
     /**
diff --git 
a/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java 
b/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java
index c8e73d7fa..61f284515 100644
--- a/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ProxyInputStreamTest.java
@@ -26,6 +26,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
 
+import org.apache.commons.io.IOUtils;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -37,10 +38,13 @@ public class ProxyInputStreamTest<T extends 
ProxyInputStream> {
 
     private static final class ProxyInputStreamFixture extends 
ProxyInputStream {
 
-        public ProxyInputStreamFixture(final InputStream proxy) {
+        ProxyInputStreamFixture(final InputStream proxy) {
             super(proxy);
         }
 
+        void setIn(final InputStream proxy) {
+            in = proxy;
+        }
     }
 
     @SuppressWarnings({ "resource", "unused" }) // For subclasses
@@ -56,6 +60,28 @@ public class ProxyInputStreamTest<T extends 
ProxyInputStream> {
         // empty
     }
 
+    @Test
+    public void testAvailableAll() throws IOException {
+        try (T inputStream = createFixture()) {
+            assertEquals(3, inputStream.available());
+            IOUtils.toByteArray(inputStream);
+            assertEquals(0, inputStream.available());
+        }
+    }
+
+    @Test
+    public void testAvailableNull() throws IOException {
+        try (ProxyInputStreamFixture inputStream = new 
ProxyInputStreamFixture(null)) {
+            assertEquals(0, inputStream.available());
+            inputStream.setIn(createFixture());
+            assertEquals(3, inputStream.available());
+            IOUtils.toByteArray(inputStream);
+            assertEquals(0, inputStream.available());
+            inputStream.setIn(null);
+            assertEquals(0, inputStream.available());
+        }
+    }
+
     @Test
     public void testRead() throws IOException {
         try (T inputStream = createFixture()) {

Reply via email to