Skip to content

Commit 46b07ec

Browse files
author
Gary Gregory
committed
Add UncheckedFilterOutputStream.
1 parent b58b082 commit 46b07ec

3 files changed

Lines changed: 234 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ The <action> type attribute can be add,update,fix,remove.
6969
<action dev="ggregory" type="add" due-to="Gary Gregory">
7070
Add UncheckedFilterInputStream.
7171
</action>
72+
<action dev="ggregory" type="add" due-to="Gary Gregory">
73+
Add UncheckedFilterOutputStream.
74+
</action>
7275
<action dev="ggregory" type="add" due-to="Gary Gregory">
7376
Add BrokenInputStream.INSTANCE.
7477
</action>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.output;
19+
20+
import java.io.FilterOutputStream;
21+
import java.io.IOException;
22+
import java.io.OutputStream;
23+
import java.io.UncheckedIOException;
24+
25+
/**
26+
* A {@link FilterOutputStream} that throws {@link UncheckedIOException} instead of {@link UncheckedIOException}.
27+
*
28+
* @see FilterOutputStream
29+
* @see UncheckedIOException
30+
* @see UncheckedIOException
31+
* @since 2.12.0
32+
*/
33+
public class UncheckedFilterOutputStream extends FilterOutputStream {
34+
35+
/**
36+
* Creates a new instance.
37+
*
38+
* @param outputStream an OutputStream object providing the underlying stream.
39+
* @return a new UncheckedFilterOutputStream.
40+
*/
41+
public static UncheckedFilterOutputStream on(final OutputStream outputStream) {
42+
return new UncheckedFilterOutputStream(outputStream);
43+
}
44+
45+
/**
46+
* Creates an output stream filter built on top of the specified underlying output stream.
47+
*
48+
* @param outputStream the underlying output stream, or {@code null} if this instance is to be created without an
49+
* underlying stream.
50+
*/
51+
public UncheckedFilterOutputStream(final OutputStream outputStream) {
52+
super(outputStream);
53+
}
54+
55+
/**
56+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
57+
*/
58+
@Override
59+
public void close() throws UncheckedIOException {
60+
try {
61+
super.close();
62+
} catch (final IOException e) {
63+
uncheck(e);
64+
}
65+
}
66+
67+
private void uncheck(final IOException e) {
68+
throw new UncheckedIOException(e);
69+
}
70+
71+
/**
72+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
73+
*/
74+
@Override
75+
public void flush() throws UncheckedIOException {
76+
try {
77+
super.flush();
78+
} catch (final IOException e) {
79+
uncheck(e);
80+
}
81+
}
82+
83+
/**
84+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
85+
*/
86+
@Override
87+
public void write(final byte[] b) throws UncheckedIOException {
88+
try {
89+
super.write(b);
90+
} catch (final IOException e) {
91+
uncheck(e);
92+
}
93+
}
94+
95+
/**
96+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
97+
*/
98+
@Override
99+
public void write(final byte[] b, final int off, final int len) throws UncheckedIOException {
100+
try {
101+
super.write(b, off, len);
102+
} catch (final IOException e) {
103+
uncheck(e);
104+
}
105+
}
106+
107+
/**
108+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
109+
*/
110+
@Override
111+
public void write(final int b) throws UncheckedIOException {
112+
try {
113+
super.write(b);
114+
} catch (final IOException e) {
115+
uncheck(e);
116+
}
117+
}
118+
119+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.output;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
22+
import java.io.IOException;
23+
import java.io.StringWriter;
24+
import java.io.UncheckedIOException;
25+
import java.nio.charset.Charset;
26+
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Disabled;
29+
import org.junit.jupiter.api.Test;
30+
31+
/**
32+
* JUnit Test Case for {@link BrokenWriter}.
33+
*/
34+
public class UncheckedFilterOutputStreamTest {
35+
36+
private IOException exception;
37+
38+
private UncheckedFilterOutputStream brokenWriter;
39+
private UncheckedFilterOutputStream stringWriter;
40+
41+
@SuppressWarnings("resource")
42+
@BeforeEach
43+
public void setUp() {
44+
exception = new IOException("test exception");
45+
brokenWriter = UncheckedFilterOutputStream.on(new BrokenOutputStream(exception));
46+
stringWriter = UncheckedFilterOutputStream.on(new WriterOutputStream(new StringWriter(), Charset.defaultCharset()));
47+
}
48+
49+
@Test
50+
public void testClose() {
51+
stringWriter.close();
52+
}
53+
54+
@Test
55+
public void testCloseThrows() {
56+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.close()).getCause());
57+
}
58+
59+
@Test
60+
public void testEquals() {
61+
stringWriter.equals(null);
62+
}
63+
64+
@Test
65+
@Disabled("What should happen here?")
66+
public void testEqualsThrows() {
67+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.equals(null)).getCause());
68+
}
69+
70+
@Test
71+
public void testFlush() {
72+
stringWriter.flush();
73+
}
74+
75+
@Test
76+
public void testFlushThrows() {
77+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.flush()).getCause());
78+
}
79+
80+
@Test
81+
public void testHashCode() {
82+
stringWriter.hashCode();
83+
}
84+
85+
@Test
86+
@Disabled("What should happen here?")
87+
public void testHashCodeThrows() {
88+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.hashCode()).getCause());
89+
}
90+
91+
@Test
92+
public void testToString() {
93+
stringWriter.toString();
94+
}
95+
96+
@Test
97+
@Disabled("What should happen here?")
98+
public void testToStringThrows() {
99+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.toString()).getCause());
100+
}
101+
102+
@Test
103+
public void testWriteInt() {
104+
stringWriter.write(1);
105+
}
106+
107+
@Test
108+
public void testWriteIntThrows() {
109+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write(1)).getCause());
110+
}
111+
112+
}

0 commit comments

Comments
 (0)