Skip to content

Commit 33551de

Browse files
committed
ClosedInputStream.read(byte[], int, int) does not always return -1
1 parent 8e37c65 commit 33551de

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ The <action> type attribute can be add,update,fix,remove.
8686
<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>
8787
<action dev="ggregory" type="fix" due-to="Gary Gregory">Let subclasses of CountingInputStream.afterRead(int) throw IOException.</action>
8888
<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>
89+
<action dev="ggregory" type="fix" due-to="Gary Gregory">ClosedInputStream.read(byte[], int, int) does not always return -1.</action>
8990
<!-- Add -->
9091
<action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileName(Path, Function&lt;Path, R&gt;).</action>
9192
<action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileNameString().</action>

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

Lines changed: 14 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

21+
import java.io.IOException;
2122
import java.io.InputStream;
2223

2324
import org.apache.commons.io.IOUtils;
@@ -58,4 +59,17 @@ public int read() {
5859
return EOF;
5960
}
6061

62+
/**
63+
* Returns -1 to indicate that the stream is closed.
64+
*
65+
* @param b ignored.
66+
* @param off ignored.
67+
* @param len ignored.
68+
* @return always -1
69+
*/
70+
@Override
71+
public int read(final byte[] b, final int off, final int len) throws IOException {
72+
return EOF;
73+
}
74+
6175
}

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19+
import static org.apache.commons.io.IOUtils.EOF;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021

2122
import org.junit.jupiter.api.Test;
@@ -25,10 +26,43 @@
2526
*/
2627
public class ClosedInputStreamTest {
2728

29+
private void assertEof(final ClosedInputStream cis) {
30+
assertEquals(EOF, cis.read(), "read()");
31+
}
32+
2833
@Test
2934
public void testRead() throws Exception {
3035
try (ClosedInputStream cis = new ClosedInputStream()) {
31-
assertEquals(-1, cis.read(), "read()");
36+
assertEof(cis);
37+
}
38+
}
39+
40+
@Test
41+
public void testReadArray() throws Exception {
42+
try (ClosedInputStream cis = new ClosedInputStream()) {
43+
assertEquals(EOF, cis.read(new byte[4096]));
44+
assertEquals(EOF, cis.read(new byte[1]));
45+
assertEquals(EOF, cis.read(new byte[0]));
46+
}
47+
}
48+
49+
@Test
50+
public void testReadArrayIndex() throws Exception {
51+
try (ClosedInputStream cis = new ClosedInputStream()) {
52+
assertEquals(EOF, cis.read(new byte[4096], 0, 1));
53+
assertEquals(EOF, cis.read(new byte[1], 0, 1));
54+
assertEquals(EOF, cis.read(new byte[0], 0, 0));
55+
}
56+
}
57+
58+
@Test
59+
public void testSingleton() throws Exception {
60+
try (@SuppressWarnings("deprecation")
61+
ClosedInputStream cis = ClosedInputStream.CLOSED_INPUT_STREAM) {
62+
assertEof(cis);
63+
}
64+
try (ClosedInputStream cis = ClosedInputStream.INSTANCE) {
65+
assertEof(cis);
3266
}
3367
}
3468

0 commit comments

Comments
 (0)