From 854ed1d0d4495fa400dc4128d0e7374accb3f9c1 Mon Sep 17 00:00:00 2001 From: Tomas Celaya Date: Fri, 18 Aug 2017 11:47:20 -0700 Subject: [PATCH] ClosedOutputStream#flush should throw --- .../commons/io/output/ClosedOutputStream.java | 9 +++++++++ .../commons/io/output/ClosedOutputStreamTest.java | 14 ++++++++++++++ 2 files changed, 23 insertions(+) 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 0dbea9c78d0..90b56db9fe3 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 void write(final int b) throws IOException { 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"); + } } 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 dedba25de19..1c51ce63073 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 void testRead() throws Exception { } } + /** + * Test the flush() method. + * @throws Exception + */ + @Test + public void testFlush() throws Exception { + try (ClosedOutputStream cos = new ClosedOutputStream()) { + cos.flush(); + fail("flush()"); + } catch (final IOException e) { + // expected + } + } + }