Skip to content

Commit 8d5c46f

Browse files
tjcelayaPascalSchumacher
authored andcommitted
IO-546: ClosedOutputStream#flush should throw (closes #42)
1 parent 43720d0 commit 8d5c46f

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,13 @@ public void write(final int b) throws IOException {
4747
throw new IOException("write(" + b + ") failed: stream is closed");
4848
}
4949

50+
/**
51+
* Throws an {@link IOException} to indicate that the stream is closed.
52+
*
53+
* @throws IOException always thrown
54+
*/
55+
@Override
56+
public void flush() throws IOException {
57+
throw new IOException("flush() failed: stream is closed");
58+
}
5059
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,18 @@ public void testRead() throws Exception {
4141
}
4242
}
4343

44+
/**
45+
* Test the <code>flush()</code> method.
46+
* @throws Exception
47+
*/
48+
@Test
49+
public void testFlush() throws Exception {
50+
try (ClosedOutputStream cos = new ClosedOutputStream()) {
51+
cos.flush();
52+
fail("flush()");
53+
} catch (final IOException e) {
54+
// expected
55+
}
56+
}
57+
4458
}

0 commit comments

Comments
 (0)