Skip to content

Commit 1040e74

Browse files
committed
AutoCloseInputStream(InputStream) uses ClosedInputStream.INSTANCE when
its input is null
1 parent 91de8ec commit 1040e74

5 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The <action> type attribute can be add,update,fix,remove.
6060
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix PMD UnnecessaryFullyQualifiedName.</action>
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>
63+
<action dev="ggregory" type="fix" due-to="Gary Gregory">AutoCloseInputStream(InputStream) uses ClosedInputStream.INSTANCE when its input is null.</action>
6364
<!-- UPDATE -->
6465
<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>
6566
<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/AutoCloseInputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ public static Builder builder() {
106106
* @param in underlying input stream
107107
* @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
108108
*/
109+
@SuppressWarnings("resource") // ClosedInputStream.nonNull() doesn't allocate
109110
@Deprecated
110111
public AutoCloseInputStream(final InputStream in) {
111-
super(in);
112+
super(ClosedInputStream.ifNull(in));
112113
}
113114

114115
/**

src/main/java/org/apache/commons/io/input/ClosedInputStream.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.apache.commons.io.IOUtils;
2525

2626
/**
27-
* Always returns {@link IOUtils#EOF} to all attempts to read something from the stream.
27+
* Always returns {@link IOUtils#EOF} to all attempts to read something from an input stream.
2828
* <p>
2929
* Typically uses of this class include testing for corner cases in methods that accept input streams and acting as a
3030
* sentinel value instead of a {@code null} input stream.
@@ -49,6 +49,16 @@ public class ClosedInputStream extends InputStream {
4949
@Deprecated
5050
public static final ClosedInputStream CLOSED_INPUT_STREAM = INSTANCE;
5151

52+
/**
53+
* Returns {@link #INSTANCE} if the given InputStream is null, otherwise returns the given input stream.
54+
*
55+
* @param in the InputStream to test.
56+
* @return {@link #INSTANCE} if the given InputStream is null, otherwise returns the given input stream.
57+
*/
58+
static InputStream ifNull(final InputStream in) {
59+
return in != null ? in : INSTANCE;
60+
}
61+
5262
/**
5363
* Returns -1 to indicate that the stream is closed.
5464
*

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import java.io.ByteArrayInputStream;
2525
import java.io.IOException;
26+
import java.io.InputStream;
2627

28+
import org.apache.commons.io.IOUtils;
2729
import org.junit.jupiter.api.BeforeEach;
2830
import org.junit.jupiter.api.Test;
2931

@@ -50,6 +52,23 @@ public void close() {
5052
closed = false;
5153
}
5254

55+
@Test
56+
public void testAvailableAll() throws IOException {
57+
try (InputStream inputStream = new AutoCloseInputStream(new ByteArrayInputStream(data))) {
58+
assertEquals(3, inputStream.available());
59+
IOUtils.toByteArray(inputStream);
60+
assertEquals(0, inputStream.available());
61+
}
62+
}
63+
64+
@Test
65+
public void testAvailableNull() throws IOException {
66+
try (InputStream inputStream = new AutoCloseInputStream(null)) {
67+
assertEquals(0, inputStream.available());
68+
assertEquals(0, inputStream.available());
69+
}
70+
}
71+
5372
@Test
5473
public void testBuilderGet() {
5574
// java.lang.IllegalStateException: origin == null

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.apache.commons.io.IOUtils.EOF;
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertSame;
2122

2223
import org.junit.jupiter.api.Test;
2324

@@ -30,6 +31,14 @@ private void assertEof(final ClosedInputStream cis) {
3031
assertEquals(EOF, cis.read(), "read()");
3132
}
3233

34+
@SuppressWarnings("resource")
35+
@Test
36+
public void testNonNull() throws Exception {
37+
assertSame(ClosedInputStream.INSTANCE, ClosedInputStream.ifNull(null));
38+
assertSame(ClosedInputStream.INSTANCE, ClosedInputStream.ifNull(ClosedInputStream.INSTANCE));
39+
assertSame(System.in, ClosedInputStream.ifNull(System.in));
40+
}
41+
3342
@Test
3443
public void testRead() throws Exception {
3544
try (ClosedInputStream cis = new ClosedInputStream()) {

0 commit comments

Comments
 (0)