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 bdd804d2eebaff9b9e42d698fd0c0831e6d16333 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Aug 7 11:33:59 2024 -0400 NullInputStream.read(*) should throw IOException when it is closed - NullInputStream.read(byte[]) should return 0 when the input byte array in length 0 - NullInputStream.read(byte[], int, int) should return 0 when the input byte array in length 0 or requested length is 0 --- src/changes/changes.xml | 4 ++- .../apache/commons/io/input/NullInputStream.java | 15 ++++----- .../commons/io/input/NullInputStreamTest.java | 37 +++++++++++++++++++--- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b07c67c63..99684ab39 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -81,7 +81,9 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="fix" due-to="Gary Gregory">ReaderInputStream.available() should return 0 after the stream is closed.</action> <action dev="ggregory" type="fix" due-to="Gary Gregory">AutoCloseInputStream does not call handleIOException() on close() when the proxied stream throws an IOException.</action> <action dev="ggregory" type="fix" due-to="Gary Gregory">BoundedInputStream does not call handleIOException() on close() when the proxied stream throws an IOException.</action> - <action dev="ggregory" type="fix" due-to="Gary Gregory">NullInputStream.read() can be configured to return -1 (EOF) after the stream is closed.</action> + <action dev="ggregory" type="fix" due-to="Gary Gregory">NullInputStream.read(*) should throw IOException when it is closed.</action> + <action dev="ggregory" type="fix" due-to="Gary Gregory">NullInputStream.read(byte[]) should return 0 when the input byte array in length 0.</action> + <action dev="ggregory" type="fix" due-to="Gary Gregory">NullInputStream.read(byte[], int, int) should return 0 when the input byte array in length 0 or requested length is 0.</action> <!-- UPDATE --> <action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons.bytebuddy.version from 1.14.13 to 1.14.18 #615, #621, #631, #635, #642.</action> <action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.1 #644.</action> diff --git a/src/main/java/org/apache/commons/io/input/NullInputStream.java b/src/main/java/org/apache/commons/io/input/NullInputStream.java index bf3f3d6b2..741b39a64 100644 --- a/src/main/java/org/apache/commons/io/input/NullInputStream.java +++ b/src/main/java/org/apache/commons/io/input/NullInputStream.java @@ -81,6 +81,9 @@ public class NullInputStream extends AbstractInputStream { /** * Constructs an {@link InputStream} that emulates a size 0 stream which supports marking and does not throw EOFException. + * <p> + * This is an "empty" input stream. + * </p> * * @since 2.7 */ @@ -173,7 +176,6 @@ public class NullInputStream extends AbstractInputStream { * @throws IOException if {@code throwEofException} is set to {@code true}. */ private int handleEof() throws IOException { - super.close(); checkThrowEof("handleEof()"); return EOF; } @@ -253,10 +255,7 @@ public class NullInputStream extends AbstractInputStream { */ @Override public int read() throws IOException { - if (isClosed()) { - checkThrowEof("read()"); - return EOF; - } + checkOpen(); if (position == size) { return handleEof(); } @@ -289,10 +288,10 @@ public class NullInputStream extends AbstractInputStream { */ @Override public int read(final byte[] bytes, final int offset, final int length) throws IOException { - if (isClosed()) { - checkThrowEof("read(byte[], int, int)"); - return EOF; + if (bytes.length == 0 || length == 0) { + return 0; } + checkOpen(); if (position == size) { return handleEof(); } diff --git a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java index 552befde3..ce55398c1 100644 --- a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java @@ -26,7 +26,6 @@ import java.io.EOFException; import java.io.IOException; import java.io.InputStream; -import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -175,16 +174,23 @@ public class NullInputStreamTest { } } - @SuppressWarnings("resource") + @Test + public void testReadAfterClose() throws Exception { + try (InputStream in = new NullInputStream()) { + assertEquals(0, in.available()); + in.close(); + assertThrows(IOException.class, in::read); + } + } + @ParameterizedTest @MethodSource(AbstractInputStreamTest.ARRAY_LENGTHS_NAME) public void testReadAfterClose(final int len) throws Exception { - final InputStream shadow; try (InputStream in = new TestNullInputStream(len, false, false)) { assertEquals(len, in.available()); - shadow = in; + in.close(); + assertThrows(IOException.class, in::read); } - assertEquals(IOUtils.EOF, shadow.read()); } @Test @@ -228,6 +234,27 @@ public class NullInputStreamTest { } } + @Test + public void testReadByteArrayAfterClose() throws Exception { + try (InputStream in = new NullInputStream()) { + assertEquals(0, in.available()); + in.close(); + assertEquals(0, in.read(new byte[0])); + assertThrows(IOException.class, () -> in.read(new byte[2])); + } + } + + @Test + public void testReadByteArrayIndexAfterClose() throws Exception { + try (InputStream in = new NullInputStream()) { + assertEquals(0, in.available()); + in.close(); + assertEquals(0, in.read(new byte[0], 0, 1)); + assertEquals(0, in.read(new byte[1], 0, 0)); + assertThrows(IOException.class, () -> in.read(new byte[2], 0, 1)); + } + } + @Test public void testReadByteArrayThrowAtEof() throws Exception { final byte[] bytes = new byte[10];