From 0b07ef3834fac53c9ff774b68de0a6343a2bad12 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 7 Jul 2024 16:24:10 -0400 Subject: [PATCH] CharSequenceInputStream.available() should return 0 after the stream is closed BoundedInputStream.available() return 0 after the stream is closed --- src/changes/changes.xml | 2 ++ .../commons/io/input/CharSequenceInputStream.java | 2 +- .../commons/io/input/CloseShieldInputStream.java | 2 +- .../org/apache/commons/io/input/ClosedInputStream.java | 2 +- .../commons/io/input/CharSequenceInputStreamTest.java | 10 ++++++++++ 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 96379a3fcc7..a7ab44abcb0 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -64,6 +64,8 @@ The type attribute can be add,update,fix,remove. Avoid NullPointerException in ProxyInputStream.available() when the underlying input stream is null. BufferedFileChannelInputStream.available() returns 0 before any reads. BufferedFileChannelInputStream.available() should return 0 when the stream is closed instead of throwing an exception. + CharSequenceInputStream.available() should return 0 after the stream is closed. + BoundedInputStream.available() should return 0 when the stream is closed. Bump tests commons.bytebuddy.version from 1.14.13 to 1.14.17 #615, #621, #631, #635. Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0. diff --git a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java index c36d0d61670..020424620fc 100644 --- a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java +++ b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java @@ -247,7 +247,7 @@ public int available() throws IOException { @Override public void close() throws IOException { - // noop + bBuf.position(bBuf.limit()); } /** diff --git a/src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java b/src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java index 059e3beba4f..be92f4ed8f2 100644 --- a/src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java +++ b/src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java @@ -34,7 +34,7 @@ public class CloseShieldInputStream extends ProxyInputStream { * Constructs a proxy that only shields {@link System#in} from closing. * * @param inputStream the candidate input stream. - * @return the given stream or a proxy on {@link System#in}. + * @return the given stream or a proxy on {@link System#in}. * @since 2.17.0 */ public static InputStream systemIn(final InputStream inputStream) { 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 00f7e3c7294..561965fdbf2 100644 --- a/src/main/java/org/apache/commons/io/input/ClosedInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ClosedInputStream.java @@ -51,7 +51,7 @@ public class ClosedInputStream extends InputStream { /** * 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. */ diff --git a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java index 7c87f3c26d2..ef7cbc5adec 100644 --- a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java @@ -118,6 +118,16 @@ public void testAvailable(final String csName) throws Exception { } } + @Test + public void testAvailableAfterClose() throws Exception { + final InputStream shadow; + try (InputStream in = CharSequenceInputStream.builder().setCharSequence("Hi").get()) { + assertTrue(in.available() > 0); + shadow = in; + } + assertEquals(0, shadow.available()); + } + private void testAvailableRead(final String csName) throws Exception { final String input = "test"; try (InputStream r = new CharSequenceInputStream(input, csName)) {