IO-546: ClosedOutputStream#flush should throw (closes #42)
Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/8d5c46f4 Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/8d5c46f4 Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/8d5c46f4 Branch: refs/heads/release Commit: 8d5c46f420fe3f7365579aabc8b40912a3610957 Parents: 43720d0 Author: Tomas Celaya <tjcel...@gmail.com> Authored: Fri Aug 18 11:47:20 2017 -0700 Committer: pascalschumacher <pascalschumac...@gmx.net> Committed: Fri Oct 13 18:37:50 2017 +0200 ---------------------------------------------------------------------- .../apache/commons/io/output/ClosedOutputStream.java | 9 +++++++++ .../commons/io/output/ClosedOutputStreamTest.java | 14 ++++++++++++++ 2 files changed, 23 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-io/blob/8d5c46f4/src/main/java/org/apache/commons/io/output/ClosedOutputStream.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/io/output/ClosedOutputStream.java b/src/main/java/org/apache/commons/io/output/ClosedOutputStream.java index 0dbea9c..90b56db 100644 --- a/src/main/java/org/apache/commons/io/output/ClosedOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/ClosedOutputStream.java @@ -47,4 +47,13 @@ public class ClosedOutputStream extends OutputStream { throw new IOException("write(" + b + ") failed: stream is closed"); } + /** + * Throws an {@link IOException} to indicate that the stream is closed. + * + * @throws IOException always thrown + */ + @Override + public void flush() throws IOException { + throw new IOException("flush() failed: stream is closed"); + } } http://git-wip-us.apache.org/repos/asf/commons-io/blob/8d5c46f4/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java index 1735461..45e3cab 100644 --- a/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java @@ -41,4 +41,18 @@ public class ClosedOutputStreamTest { } } + /** + * Test the <code>flush()</code> method. + * @throws Exception + */ + @Test + public void testFlush() throws Exception { + try (ClosedOutputStream cos = new ClosedOutputStream()) { + cos.flush(); + fail("flush()"); + } catch (final IOException e) { + // expected + } + } + }