Skip to content

Commit 9268f57

Browse files
committed
Add UncheckedFilterWriter.
1 parent d269f16 commit 9268f57

3 files changed

Lines changed: 366 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ The <action> type attribute can be add,update,fix,remove.
6060
<action dev="ggregory" type="add" due-to="Gary Gregory">
6161
Add UncheckedFilterReader.
6262
</action>
63+
<action dev="ggregory" type="add" due-to="Gary Gregory">
64+
Add UncheckedFilterWriter.
65+
</action>
6366
<!-- UPDATE -->
6467
<action dev="ggregory" type="update" due-to="Dependabot">
6568
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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.FilterWriter;
21+
import java.io.IOException;
22+
import java.io.UncheckedIOException;
23+
import java.io.Writer;
24+
25+
/**
26+
* A {@link FilterWriter} that throws {@link UncheckedIOException} instead of {@link IOException}.
27+
*
28+
* @see FilterWriter
29+
* @see IOException
30+
* @see UncheckedIOException
31+
* @since 2.12.0
32+
*/
33+
public class UncheckedFilterWriter extends FilterWriter {
34+
35+
/**
36+
* Creates a new filtered writer.
37+
*
38+
* @param writer a Writer object providing the underlying stream.
39+
* @return a new UncheckedFilterReader.
40+
* @throws NullPointerException if {@code writer} is {@code null}.
41+
*/
42+
public static UncheckedFilterWriter on(final Writer writer) {
43+
return new UncheckedFilterWriter(writer);
44+
}
45+
46+
/**
47+
* Creates a new filtered writer.
48+
*
49+
* @param writer a Writer object providing the underlying stream.
50+
* @throws NullPointerException if {@code writer} is {@code null}.
51+
*/
52+
protected UncheckedFilterWriter(Writer writer) {
53+
super(writer);
54+
}
55+
56+
/**
57+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
58+
*/
59+
@Override
60+
public Writer append(char c) throws UncheckedIOException {
61+
try {
62+
return super.append(c);
63+
} catch (IOException e) {
64+
throw uncheck(e);
65+
}
66+
}
67+
68+
/**
69+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
70+
*/
71+
@Override
72+
public Writer append(CharSequence csq) throws UncheckedIOException {
73+
try {
74+
return super.append(csq);
75+
} catch (IOException e) {
76+
throw uncheck(e);
77+
}
78+
}
79+
80+
/**
81+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
82+
*/
83+
@Override
84+
public Writer append(CharSequence csq, int start, int end) throws UncheckedIOException {
85+
try {
86+
return super.append(csq, start, end);
87+
} catch (IOException e) {
88+
throw uncheck(e);
89+
}
90+
}
91+
92+
/**
93+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
94+
*/
95+
@Override
96+
public void close() throws UncheckedIOException {
97+
try {
98+
super.close();
99+
} catch (IOException e) {
100+
throw uncheck(e);
101+
}
102+
}
103+
104+
/**
105+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
106+
*/
107+
@Override
108+
public void flush() throws UncheckedIOException {
109+
try {
110+
super.flush();
111+
} catch (IOException e) {
112+
throw uncheck(e);
113+
}
114+
}
115+
116+
private UncheckedIOException uncheck(IOException e) {
117+
return new UncheckedIOException(e);
118+
}
119+
120+
/**
121+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
122+
*/
123+
@Override
124+
public void write(char[] cbuf) throws UncheckedIOException {
125+
try {
126+
super.write(cbuf);
127+
} catch (IOException e) {
128+
throw uncheck(e);
129+
}
130+
}
131+
132+
/**
133+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
134+
*/
135+
@Override
136+
public void write(char[] cbuf, int off, int len) throws UncheckedIOException {
137+
try {
138+
super.write(cbuf, off, len);
139+
} catch (IOException e) {
140+
throw uncheck(e);
141+
}
142+
}
143+
144+
/**
145+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
146+
*/
147+
@Override
148+
public void write(int c) throws UncheckedIOException {
149+
try {
150+
super.write(c);
151+
} catch (IOException e) {
152+
throw uncheck(e);
153+
}
154+
}
155+
156+
/**
157+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
158+
*/
159+
@Override
160+
public void write(String str) throws UncheckedIOException {
161+
try {
162+
super.write(str);
163+
} catch (IOException e) {
164+
throw uncheck(e);
165+
}
166+
}
167+
168+
/**
169+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
170+
*/
171+
@Override
172+
public void write(String str, int off, int len) throws UncheckedIOException {
173+
try {
174+
super.write(str, off, len);
175+
} catch (IOException e) {
176+
throw uncheck(e);
177+
}
178+
}
179+
180+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Disabled;
28+
import org.junit.jupiter.api.Test;
29+
30+
/**
31+
* JUnit Test Case for {@link BrokenWriter}.
32+
*/
33+
public class UncheckedFilterWriterTest {
34+
35+
private IOException exception;
36+
37+
private UncheckedFilterWriter brokenWriter;
38+
private UncheckedFilterWriter stringWriter;
39+
40+
@SuppressWarnings("resource")
41+
@BeforeEach
42+
public void setUp() {
43+
exception = new IOException("test exception");
44+
brokenWriter = UncheckedFilterWriter.on(new BrokenWriter(exception));
45+
stringWriter = UncheckedFilterWriter.on(new StringWriter());
46+
}
47+
48+
@SuppressWarnings("resource")
49+
@Test
50+
public void testAppendChar() {
51+
stringWriter.append('1');
52+
}
53+
54+
@SuppressWarnings("resource")
55+
@Test
56+
public void testAppendCharSequence() {
57+
stringWriter.append("01");
58+
}
59+
60+
@SuppressWarnings("resource")
61+
@Test
62+
public void testAppendCharSequenceIndexed() {
63+
stringWriter.append("01", 0, 1);
64+
}
65+
66+
@Test
67+
public void testAppendCharSequenceIndexedThrows() {
68+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.append("01", 0, 1)).getCause());
69+
}
70+
71+
@Test
72+
public void testAppendCharSequenceThrows() {
73+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.append("01")).getCause());
74+
}
75+
76+
@Test
77+
public void testAppendCharThrows() {
78+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.append('1')).getCause());
79+
}
80+
81+
@Test
82+
public void testClose() {
83+
stringWriter.close();
84+
}
85+
86+
@Test
87+
public void testCloseThrows() {
88+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.close()).getCause());
89+
}
90+
91+
@Test
92+
public void testEquals() {
93+
stringWriter.equals(null);
94+
}
95+
96+
@Test
97+
@Disabled("What should happen here?")
98+
public void testEqualsThrows() {
99+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.equals(null)).getCause());
100+
}
101+
102+
@Test
103+
public void testFlush() {
104+
stringWriter.flush();
105+
}
106+
107+
@Test
108+
public void testFlushThrows() {
109+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.flush()).getCause());
110+
}
111+
112+
@Test
113+
public void testHashCode() {
114+
stringWriter.hashCode();
115+
}
116+
117+
@Test
118+
@Disabled("What should happen here?")
119+
public void testHashCodeThrows() {
120+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.hashCode()).getCause());
121+
}
122+
123+
@Test
124+
public void testToString() {
125+
stringWriter.toString();
126+
}
127+
128+
@Test
129+
@Disabled("What should happen here?")
130+
public void testToStringThrows() {
131+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.toString()).getCause());
132+
}
133+
134+
@Test
135+
public void testWriteCharArray() {
136+
stringWriter.write(new char[1]);
137+
}
138+
139+
@Test
140+
public void testWriteCharArrayIndexed() {
141+
stringWriter.write(new char[1], 0, 1);
142+
}
143+
144+
@Test
145+
public void testWriteCharArrayIndexedThrows() {
146+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write(new char[1], 0, 1)).getCause());
147+
}
148+
149+
@Test
150+
public void testWriteCharArrayThrows() {
151+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write(new char[1])).getCause());
152+
}
153+
154+
@Test
155+
public void testWriteInt() {
156+
stringWriter.write(1);
157+
}
158+
159+
@Test
160+
public void testWriteIntThrows() {
161+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write(1)).getCause());
162+
}
163+
164+
@Test
165+
public void testWriteString() {
166+
stringWriter.write("01");
167+
}
168+
169+
@Test
170+
public void testWriteStringIndexed() {
171+
stringWriter.write("01", 0, 1);
172+
}
173+
174+
@Test
175+
public void testWriteStringIndexedThrows() {
176+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write("01", 0, 1)).getCause());
177+
}
178+
179+
@Test
180+
public void testWriteStringThrows() {
181+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> brokenWriter.write("01")).getCause());
182+
}
183+
}

0 commit comments

Comments
 (0)