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 1040e74d4c05930a7ed8864c87a5578c10bb9e99 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Jul 7 14:12:24 2024 -0400 AutoCloseInputStream(InputStream) uses ClosedInputStream.INSTANCE when its input is null --- src/changes/changes.xml | 1 + .../apache/commons/io/input/AutoCloseInputStream.java | 3 ++- .../apache/commons/io/input/ClosedInputStream.java | 12 +++++++++++- .../commons/io/input/AutoCloseInputStreamTest.java | 19 +++++++++++++++++++ .../commons/io/input/ClosedInputStreamTest.java | 9 +++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5e1b6fe8c..bf43e4bde 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -60,6 +60,7 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="fix" due-to="Gary Gregory">Fix PMD UnnecessaryFullyQualifiedName.</action> <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> <!-- 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/AutoCloseInputStream.java b/src/main/java/org/apache/commons/io/input/AutoCloseInputStream.java index d7c705f48..df6da7795 100644 --- a/src/main/java/org/apache/commons/io/input/AutoCloseInputStream.java +++ b/src/main/java/org/apache/commons/io/input/AutoCloseInputStream.java @@ -106,9 +106,10 @@ public class AutoCloseInputStream extends ProxyInputStream { * @param in underlying input stream * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()} */ + @SuppressWarnings("resource") // ClosedInputStream.nonNull() doesn't allocate @Deprecated public AutoCloseInputStream(final InputStream in) { - super(in); + super(ClosedInputStream.ifNull(in)); } /** diff --git a/src/main/java/org/apache/commons/io/input/ClosedInputStream.java b/src/main/java/org/apache/commons/io/input/ClosedInputStream.java index 7bcad5c98..00f7e3c72 100644 --- a/src/main/java/org/apache/commons/io/input/ClosedInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ClosedInputStream.java @@ -24,7 +24,7 @@ import java.io.InputStream; import org.apache.commons.io.IOUtils; /** - * Always returns {@link IOUtils#EOF} to all attempts to read something from the stream. + * Always returns {@link IOUtils#EOF} to all attempts to read something from an input stream. * <p> * Typically uses of this class include testing for corner cases in methods that accept input streams and acting as a * sentinel value instead of a {@code null} input stream. @@ -49,6 +49,16 @@ public class ClosedInputStream extends InputStream { @Deprecated public static final ClosedInputStream CLOSED_INPUT_STREAM = INSTANCE; + /** + * Returns {@link #INSTANCE} if the given InputStream is null, otherwise returns the given input stream. + * + * @param in the InputStream to test. + * @return {@link #INSTANCE} if the given InputStream is null, otherwise returns the given input stream. + */ + static InputStream ifNull(final InputStream in) { + return in != null ? in : INSTANCE; + } + /** * Returns -1 to indicate that the stream is closed. * diff --git a/src/test/java/org/apache/commons/io/input/AutoCloseInputStreamTest.java b/src/test/java/org/apache/commons/io/input/AutoCloseInputStreamTest.java index c713784e6..dca30eb4e 100644 --- a/src/test/java/org/apache/commons/io/input/AutoCloseInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/AutoCloseInputStreamTest.java @@ -23,7 +23,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; +import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -50,6 +52,23 @@ public class AutoCloseInputStreamTest { closed = false; } + @Test + public void testAvailableAll() throws IOException { + try (InputStream inputStream = new AutoCloseInputStream(new ByteArrayInputStream(data))) { + assertEquals(3, inputStream.available()); + IOUtils.toByteArray(inputStream); + assertEquals(0, inputStream.available()); + } + } + + @Test + public void testAvailableNull() throws IOException { + try (InputStream inputStream = new AutoCloseInputStream(null)) { + assertEquals(0, inputStream.available()); + assertEquals(0, inputStream.available()); + } + } + @Test public void testBuilderGet() { // java.lang.IllegalStateException: origin == null diff --git a/src/test/java/org/apache/commons/io/input/ClosedInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ClosedInputStreamTest.java index 5e71367b5..6ad00f7d2 100644 --- a/src/test/java/org/apache/commons/io/input/ClosedInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/ClosedInputStreamTest.java @@ -18,6 +18,7 @@ package org.apache.commons.io.input; import static org.apache.commons.io.IOUtils.EOF; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import org.junit.jupiter.api.Test; @@ -30,6 +31,14 @@ public class ClosedInputStreamTest { assertEquals(EOF, cis.read(), "read()"); } + @SuppressWarnings("resource") + @Test + public void testNonNull() throws Exception { + assertSame(ClosedInputStream.INSTANCE, ClosedInputStream.ifNull(null)); + assertSame(ClosedInputStream.INSTANCE, ClosedInputStream.ifNull(ClosedInputStream.INSTANCE)); + assertSame(System.in, ClosedInputStream.ifNull(System.in)); + } + @Test public void testRead() throws Exception { try (ClosedInputStream cis = new ClosedInputStream()) {