Skip to content

Commit 10d74fc

Browse files
committed
Avoid NullPointerException in ProxyInputStream.available() when the
underlying input stream is null
1 parent 2432b1b commit 10d74fc

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ The <action> type attribute can be add,update,fix,remove.
6161
<action dev="ggregory" type="fix" due-to="sullis">Add test for CircularByteBuffer clear() #620.</action>
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>
64+
<action dev="ggregory" type="add" due-to="Gary Gregory">Avoid NullPointerException in ProxyInputStream.available() when the underlying input stream is null.</action>
6465
<!-- UPDATE -->
6566
<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>
6667
<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/ProxyInputStream.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ protected void afterRead(final int n) throws IOException {
8787
*/
8888
@Override
8989
public int available() throws IOException {
90-
try {
91-
return super.available();
92-
} catch (final IOException e) {
93-
handleIOException(e);
94-
return 0;
90+
if (in != null) {
91+
try {
92+
return in.available();
93+
} catch (final IOException e) {
94+
handleIOException(e);
95+
}
9596
}
97+
return 0;
9698
}
9799

98100
/**

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.InputStream;
2727
import java.util.Arrays;
2828

29+
import org.apache.commons.io.IOUtils;
2930
import org.junit.jupiter.api.Test;
3031

3132
/**
@@ -37,10 +38,13 @@ public class ProxyInputStreamTest<T extends ProxyInputStream> {
3738

3839
private static final class ProxyInputStreamFixture extends ProxyInputStream {
3940

40-
public ProxyInputStreamFixture(final InputStream proxy) {
41+
ProxyInputStreamFixture(final InputStream proxy) {
4142
super(proxy);
4243
}
4344

45+
void setIn(final InputStream proxy) {
46+
in = proxy;
47+
}
4448
}
4549

4650
@SuppressWarnings({ "resource", "unused" }) // For subclasses
@@ -56,6 +60,28 @@ protected void testEos(final T inputStream) {
5660
// empty
5761
}
5862

63+
@Test
64+
public void testAvailableAll() throws IOException {
65+
try (T inputStream = createFixture()) {
66+
assertEquals(3, inputStream.available());
67+
IOUtils.toByteArray(inputStream);
68+
assertEquals(0, inputStream.available());
69+
}
70+
}
71+
72+
@Test
73+
public void testAvailableNull() throws IOException {
74+
try (ProxyInputStreamFixture inputStream = new ProxyInputStreamFixture(null)) {
75+
assertEquals(0, inputStream.available());
76+
inputStream.setIn(createFixture());
77+
assertEquals(3, inputStream.available());
78+
IOUtils.toByteArray(inputStream);
79+
assertEquals(0, inputStream.available());
80+
inputStream.setIn(null);
81+
assertEquals(0, inputStream.available());
82+
}
83+
}
84+
5985
@Test
6086
public void testRead() throws IOException {
6187
try (T inputStream = createFixture()) {

0 commit comments

Comments
 (0)