Skip to content

Commit ccfee1d

Browse files
committed
ClosedOutputStream.write(byte[], int, int) does not always throw
IOException
1 parent 653fd91 commit ccfee1d

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

src/changes/changes.xml

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

src/main/java/org/apache/commons/io/output/ClosedOutputStream.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
/**
2323
* Throws an IOException on all attempts to write to the stream.
2424
* <p>
25-
* Typically uses of this class include testing for corner cases in methods that accept an output stream and acting as a
26-
* sentinel value instead of a {@code null} output stream.
25+
* Typically uses of this class include testing for corner cases in methods that accept an output stream and acting as a sentinel value instead of a
26+
* {@code null} output stream.
2727
* </p>
2828
*
2929
* @since 1.4
@@ -55,6 +55,19 @@ public void flush() throws IOException {
5555
throw new IOException("flush() failed: stream is closed");
5656
}
5757

58+
/**
59+
* Throws an {@link IOException} to indicate that the stream is closed.
60+
*
61+
* @param b ignored
62+
* @param off ignored
63+
* @param len ignored
64+
* @throws IOException always thrown
65+
*/
66+
@Override
67+
public void write(final byte b[], final int off, final int len) throws IOException {
68+
throw new IOException("write(byte[], int, int) failed: stream is closed");
69+
}
70+
5871
/**
5972
* Throws an {@link IOException} to indicate that the stream is closed.
6073
*
@@ -63,6 +76,6 @@ public void flush() throws IOException {
6376
*/
6477
@Override
6578
public void write(final int b) throws IOException {
66-
throw new IOException("write(" + b + ") failed: stream is closed");
79+
throw new IOException("write(int) failed: stream is closed");
6780
}
6881
}

src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ public class ClosedOutputStreamTest {
3333
@Test
3434
public void testFlush() throws IOException {
3535
try (ClosedOutputStream cos = new ClosedOutputStream()) {
36-
assertThrows(IOException.class, () -> cos.flush());
36+
assertThrows(IOException.class, cos::flush);
37+
}
38+
}
39+
40+
@Test
41+
public void testSingleton() throws IOException {
42+
try (@SuppressWarnings("deprecation")
43+
ClosedOutputStream cos = ClosedOutputStream.CLOSED_OUTPUT_STREAM) {
44+
assertThrows(IOException.class, cos::flush);
45+
}
46+
try (ClosedOutputStream cos = ClosedOutputStream.INSTANCE) {
47+
assertThrows(IOException.class, cos::flush);
3748
}
3849
}
3950

@@ -47,4 +58,20 @@ public void testWrite() throws IOException {
4758
}
4859
}
4960

61+
@Test
62+
public void testWriteArray() throws IOException {
63+
try (ClosedOutputStream cos = new ClosedOutputStream()) {
64+
assertThrows(IOException.class, () -> cos.write(new byte[0]));
65+
assertThrows(IOException.class, () -> cos.write(new byte[10]));
66+
}
67+
}
68+
69+
@Test
70+
public void testWriteArrayIndex() throws IOException {
71+
try (ClosedOutputStream cos = new ClosedOutputStream()) {
72+
assertThrows(IOException.class, () -> cos.write(new byte[0], 0, 0));
73+
assertThrows(IOException.class, () -> cos.write(new byte[10], 0, 1));
74+
}
75+
}
76+
5077
}

0 commit comments

Comments
 (0)