This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new ccfee1dc ClosedOutputStream.write(byte[], int, int) does not always throw IOException ccfee1dc is described below commit ccfee1dc17b055627e3eec1f5bea185935d0c4a7 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Jan 1 12:45:28 2024 -0500 ClosedOutputStream.write(byte[], int, int) does not always throw IOException --- src/changes/changes.xml | 1 + .../commons/io/output/ClosedOutputStream.java | 19 +++++++++++--- .../commons/io/output/ClosedOutputStreamTest.java | 29 +++++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ae027061..b0670bf4 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -87,6 +87,7 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="fix" due-to="Gary Gregory">Let subclasses of CountingInputStream.afterRead(int) throw IOException.</action> <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> <action dev="ggregory" type="fix" due-to="Gary Gregory">ClosedInputStream.read(byte[], int, int) does not always return -1.</action> + <action dev="ggregory" type="fix" due-to="Gary Gregory">ClosedOutputStream.write(byte[], int, int) does not always throw IOException.</action> <!-- Add --> <action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileName(Path, Function<Path, R>).</action> <action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileNameString().</action> 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 8050e46a..4d1d38a6 100644 --- a/src/main/java/org/apache/commons/io/output/ClosedOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/ClosedOutputStream.java @@ -22,8 +22,8 @@ import java.io.OutputStream; /** * Throws an IOException on all attempts to write to the stream. * <p> - * 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 {@code null} output stream. + * 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 + * {@code null} output stream. * </p> * * @since 1.4 @@ -55,6 +55,19 @@ public class ClosedOutputStream extends OutputStream { throw new IOException("flush() failed: stream is closed"); } + /** + * Throws an {@link IOException} to indicate that the stream is closed. + * + * @param b ignored + * @param off ignored + * @param len ignored + * @throws IOException always thrown + */ + @Override + public void write(final byte b[], final int off, final int len) throws IOException { + throw new IOException("write(byte[], int, int) failed: stream is closed"); + } + /** * Throws an {@link IOException} to indicate that the stream is closed. * @@ -63,6 +76,6 @@ public class ClosedOutputStream extends OutputStream { */ @Override public void write(final int b) throws IOException { - throw new IOException("write(" + b + ") failed: stream is closed"); + throw new IOException("write(int) 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 b1968e89..97abd563 100644 --- a/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java @@ -33,7 +33,18 @@ public class ClosedOutputStreamTest { @Test public void testFlush() throws IOException { try (ClosedOutputStream cos = new ClosedOutputStream()) { - assertThrows(IOException.class, () -> cos.flush()); + assertThrows(IOException.class, cos::flush); + } + } + + @Test + public void testSingleton() throws IOException { + try (@SuppressWarnings("deprecation") + ClosedOutputStream cos = ClosedOutputStream.CLOSED_OUTPUT_STREAM) { + assertThrows(IOException.class, cos::flush); + } + try (ClosedOutputStream cos = ClosedOutputStream.INSTANCE) { + assertThrows(IOException.class, cos::flush); } } @@ -47,4 +58,20 @@ public class ClosedOutputStreamTest { } } + @Test + public void testWriteArray() throws IOException { + try (ClosedOutputStream cos = new ClosedOutputStream()) { + assertThrows(IOException.class, () -> cos.write(new byte[0])); + assertThrows(IOException.class, () -> cos.write(new byte[10])); + } + } + + @Test + public void testWriteArrayIndex() throws IOException { + try (ClosedOutputStream cos = new ClosedOutputStream()) { + assertThrows(IOException.class, () -> cos.write(new byte[0], 0, 0)); + assertThrows(IOException.class, () -> cos.write(new byte[10], 0, 1)); + } + } + }