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


The following commit(s) were added to refs/heads/master by this push:
     new 33551deb ClosedInputStream.read(byte[], int, int) does not always 
return -1
33551deb is described below

commit 33551deb15a3fe312bb75ce991275d1683bbb48d
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Jan 1 11:32:54 2024 -0500

    ClosedInputStream.read(byte[], int, int) does not always return -1
---
 src/changes/changes.xml                            |  1 +
 .../apache/commons/io/input/ClosedInputStream.java | 14 +++++++++
 .../commons/io/input/ClosedInputStreamTest.java    | 36 +++++++++++++++++++++-
 3 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 96015cc5..ae027061 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -86,6 +86,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix" issue="IO-829" due-to="Elliotte Rusty 
Harold, Gary Gregory">Don't decode and reencode characters in a potentially 
different charset in 
AbstractOrigin.CharSequenceOrigin.getReader(Charset).</action>
       <action dev="ggregory" type="fix" due-to="Gary Gregory">Let subclasses 
of CountingInputStream.afterRead(int) throw IOException.</action>
       <action dev="ggregory" type="fix" issue="IO-807" due-to="Elliotte Rusty 
Harold, Gary Gregory">Characterization test for broken symlinks when copying 
directories #547.</action>
+      <action dev="ggregory" type="fix" due-to="Gary 
Gregory">ClosedInputStream.read(byte[], int, int) does not always return 
-1.</action>
       <!-- Add -->
       <action dev="ggregory" type="add"                due-to="Gary 
Gregory">Add and use PathUtils.getFileName(Path, Function&lt;Path, 
R&gt;).</action>
       <action dev="ggregory" type="add"                due-to="Gary 
Gregory">Add and use PathUtils.getFileNameString().</action>
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 fc372e34..7bcad5c9 100644
--- a/src/main/java/org/apache/commons/io/input/ClosedInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ClosedInputStream.java
@@ -18,6 +18,7 @@ package org.apache.commons.io.input;
 
 import static org.apache.commons.io.IOUtils.EOF;
 
+import java.io.IOException;
 import java.io.InputStream;
 
 import org.apache.commons.io.IOUtils;
@@ -58,4 +59,17 @@ public class ClosedInputStream extends InputStream {
         return EOF;
     }
 
+    /**
+     * Returns -1 to indicate that the stream is closed.
+     *
+     * @param b ignored.
+     * @param off ignored.
+     * @param len ignored.
+     * @return always -1
+     */
+    @Override
+    public int read(final byte[] b, final int off, final int len) throws 
IOException {
+        return EOF;
+    }
+
 }
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 a8ff62aa..5e71367b 100644
--- a/src/test/java/org/apache/commons/io/input/ClosedInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ClosedInputStreamTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.io.input;
 
+import static org.apache.commons.io.IOUtils.EOF;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import org.junit.jupiter.api.Test;
@@ -25,10 +26,43 @@ import org.junit.jupiter.api.Test;
  */
 public class ClosedInputStreamTest {
 
+    private void assertEof(final ClosedInputStream cis) {
+        assertEquals(EOF, cis.read(), "read()");
+    }
+
     @Test
     public void testRead() throws Exception {
         try (ClosedInputStream cis = new ClosedInputStream()) {
-            assertEquals(-1, cis.read(), "read()");
+            assertEof(cis);
+        }
+    }
+
+    @Test
+    public void testReadArray() throws Exception {
+        try (ClosedInputStream cis = new ClosedInputStream()) {
+            assertEquals(EOF, cis.read(new byte[4096]));
+            assertEquals(EOF, cis.read(new byte[1]));
+            assertEquals(EOF, cis.read(new byte[0]));
+        }
+    }
+
+    @Test
+    public void testReadArrayIndex() throws Exception {
+        try (ClosedInputStream cis = new ClosedInputStream()) {
+            assertEquals(EOF, cis.read(new byte[4096], 0, 1));
+            assertEquals(EOF, cis.read(new byte[1], 0, 1));
+            assertEquals(EOF, cis.read(new byte[0], 0, 0));
+        }
+    }
+
+    @Test
+    public void testSingleton() throws Exception {
+        try (@SuppressWarnings("deprecation")
+        ClosedInputStream cis = ClosedInputStream.CLOSED_INPUT_STREAM) {
+            assertEof(cis);
+        }
+        try (ClosedInputStream cis = ClosedInputStream.INSTANCE) {
+            assertEof(cis);
         }
     }
 

Reply via email to