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 46b07ec Add UncheckedFilterOutputStream. 46b07ec is described below commit 46b07ec45ef7e6753625f08af73472204fa54d19 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Mon Jul 12 19:30:35 2021 -0400 Add UncheckedFilterOutputStream. --- src/changes/changes.xml | 3 + .../io/output/UncheckedFilterOutputStream.java | 119 +++++++++++++++++++++ .../io/output/UncheckedFilterOutputStreamTest.java | 112 +++++++++++++++++++ 3 files changed, 234 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e97443f..0f2251e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -70,6 +70,9 @@ The <action> type attribute can be add,update,fix,remove. Add UncheckedFilterInputStream. </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add UncheckedFilterOutputStream. + </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> Add BrokenInputStream.INSTANCE. </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> diff --git a/src/main/java/org/apache/commons/io/output/UncheckedFilterOutputStream.java b/src/main/java/org/apache/commons/io/output/UncheckedFilterOutputStream.java new file mode 100644 index 0000000..9a2eb1d --- /dev/null +++ b/src/main/java/org/apache/commons/io/output/UncheckedFilterOutputStream.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.io.output; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UncheckedIOException; + +/** + * A {@link FilterOutputStream} that throws {@link UncheckedIOException} instead of {@link UncheckedIOException}. + * + * @see FilterOutputStream + * @see UncheckedIOException + * @see UncheckedIOException + * @since 2.12.0 + */ +public class UncheckedFilterOutputStream extends FilterOutputStream { + + /** + * Creates a new instance. + * + * @param outputStream an OutputStream object providing the underlying stream. + * @return a new UncheckedFilterOutputStream. + */ + public static UncheckedFilterOutputStream on(final OutputStream outputStream) { + return new UncheckedFilterOutputStream(outputStream); + } + + /** + * Creates an output stream filter built on top of the specified underlying output stream. + * + * @param outputStream the underlying output stream, or {@code null} if this instance is to be created without an + * underlying stream. + */ + public UncheckedFilterOutputStream(final OutputStream outputStream) { + super(outputStream); + } + + /** + * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. + */ + @Override + public void close() throws UncheckedIOException { + try { + super.close(); + } catch (final IOException e) { + uncheck(e); + } + } + + private void uncheck(final IOException e) { + throw new UncheckedIOException(e); + } + + /** + * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. + */ + @Override + public void flush() throws UncheckedIOException { + try { + super.flush(); + } catch (final IOException e) { + uncheck(e); + } + } + + /** + * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. + */ + @Override + public void write(final byte[] b) throws UncheckedIOException { + try { + super.write(b); + } catch (final IOException e) { + uncheck(e); + } + } + + /** + * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. + */ + @Override + public void write(final byte[] b, final int off, final int len) throws UncheckedIOException { + try { + super.write(b, off, len); + } catch (final IOException e) { + uncheck(e); + } + } + + /** + * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. + */ + @Override + public void write(final int b) throws UncheckedIOException { + try { + super.write(b); + } catch (final IOException e) { + uncheck(e); + } + } + +} diff --git a/src/test/java/org/apache/commons/io/output/UncheckedFilterOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/UncheckedFilterOutputStreamTest.java new file mode 100644 index 0000000..8f86db7 --- /dev/null +++ b/src/test/java/org/apache/commons/io/output/UncheckedFilterOutputStreamTest.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.output; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.UncheckedIOException; +import java.nio.charset.Charset; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * JUnit Test Case for {@link BrokenWriter}. + */ +public class UncheckedFilterOutputStreamTest { + + private IOException exception; + + private UncheckedFilterOutputStream brokenWriter; + private UncheckedFilterOutputStream stringWriter; + + @SuppressWarnings("resource") + @BeforeEach + public void setUp() { + exception = new IOException("test exception"); + brokenWriter = UncheckedFilterOutputStream.on(new BrokenOutputStream(exception)); + stringWriter = UncheckedFilterOutputStream.on(new WriterOutputStream(new StringWriter(), Charset.defaultCharset())); + } + + @Test + public void testClose() { + stringWriter.close(); + } + + @Test + public void testCloseThrows() { + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.close()).getCause()); + } + + @Test + public void testEquals() { + stringWriter.equals(null); + } + + @Test + @Disabled("What should happen here?") + public void testEqualsThrows() { + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.equals(null)).getCause()); + } + + @Test + public void testFlush() { + stringWriter.flush(); + } + + @Test + public void testFlushThrows() { + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.flush()).getCause()); + } + + @Test + public void testHashCode() { + stringWriter.hashCode(); + } + + @Test + @Disabled("What should happen here?") + public void testHashCodeThrows() { + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.hashCode()).getCause()); + } + + @Test + public void testToString() { + stringWriter.toString(); + } + + @Test + @Disabled("What should happen here?") + public void testToStringThrows() { + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.toString()).getCause()); + } + + @Test + public void testWriteInt() { + stringWriter.write(1); + } + + @Test + public void testWriteIntThrows() { + assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write(1)).getCause()); + } + +}