Skip to content

Commit 864fb60

Browse files
committed
Add UncheckedFilterReader.
1 parent e3a960c commit 864fb60

3 files changed

Lines changed: 357 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ The <action> type attribute can be add,update,fix,remove.
5757
<action dev="ggregory" type="add" due-to="Gary Gregory">
5858
Add UncheckedBufferedReader.
5959
</action>
60+
<action dev="ggregory" type="add" due-to="Gary Gregory">
61+
Add UncheckedFilterReader.
62+
</action>
6063
<!-- UPDATE -->
6164
<action dev="ggregory" type="update" due-to="Dependabot">
6265
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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.input;
19+
20+
import java.io.FilterReader;
21+
import java.io.IOException;
22+
import java.io.Reader;
23+
import java.io.UncheckedIOException;
24+
import java.nio.CharBuffer;
25+
26+
/**
27+
* A {@link FilterReader} that throws {@link UncheckedIOException} instead of {@link IOException}.
28+
*
29+
* @see FilterReader
30+
* @see IOException
31+
* @see UncheckedIOException
32+
* @since 2.12.0
33+
*/
34+
public class UncheckedFilterReader extends FilterReader {
35+
36+
/**
37+
* Creates a new filtered reader.
38+
*
39+
* @param reader a Reader object providing the underlying stream.
40+
* @return a new UncheckedFilterReader.
41+
* @throws NullPointerException if {@code reader} is {@code null}.
42+
*/
43+
public static UncheckedFilterReader on(final Reader reader) {
44+
return new UncheckedFilterReader(reader);
45+
}
46+
47+
/**
48+
* Creates a new filtered reader.
49+
*
50+
* @param reader a Reader object providing the underlying stream.
51+
* @throws NullPointerException if {@code reader} is {@code null}.
52+
*/
53+
public UncheckedFilterReader(final Reader reader) {
54+
super(reader);
55+
}
56+
57+
/**
58+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
59+
*/
60+
@Override
61+
public void close() throws UncheckedIOException {
62+
try {
63+
super.close();
64+
} catch (final IOException e) {
65+
throw new UncheckedIOException(e);
66+
}
67+
}
68+
69+
/**
70+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
71+
*/
72+
@Override
73+
public void mark(final int readAheadLimit) throws UncheckedIOException {
74+
try {
75+
super.mark(readAheadLimit);
76+
} catch (final IOException e) {
77+
throw new UncheckedIOException(e);
78+
}
79+
}
80+
81+
/**
82+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
83+
*/
84+
@Override
85+
public int read() throws UncheckedIOException {
86+
try {
87+
return super.read();
88+
} catch (final IOException e) {
89+
throw new UncheckedIOException(e);
90+
}
91+
}
92+
93+
/**
94+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
95+
*/
96+
@Override
97+
public int read(final char[] cbuf) throws UncheckedIOException {
98+
try {
99+
return super.read(cbuf);
100+
} catch (final IOException e) {
101+
throw new UncheckedIOException(e);
102+
}
103+
}
104+
105+
/**
106+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
107+
*/
108+
@Override
109+
public int read(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
110+
try {
111+
return super.read(cbuf, off, len);
112+
} catch (final IOException e) {
113+
throw new UncheckedIOException(e);
114+
}
115+
}
116+
117+
/**
118+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
119+
*/
120+
@Override
121+
public int read(final CharBuffer target) throws UncheckedIOException {
122+
try {
123+
return super.read(target);
124+
} catch (final IOException e) {
125+
throw new UncheckedIOException(e);
126+
}
127+
}
128+
129+
/**
130+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
131+
*/
132+
@Override
133+
public boolean ready() throws UncheckedIOException {
134+
try {
135+
return super.ready();
136+
} catch (final IOException e) {
137+
throw new UncheckedIOException(e);
138+
}
139+
}
140+
141+
/**
142+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
143+
*/
144+
@Override
145+
public void reset() throws UncheckedIOException {
146+
try {
147+
super.reset();
148+
} catch (final IOException e) {
149+
throw new UncheckedIOException(e);
150+
}
151+
}
152+
153+
/**
154+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
155+
*/
156+
@Override
157+
public long skip(final long n) throws UncheckedIOException {
158+
try {
159+
return super.skip(n);
160+
} catch (final IOException e) {
161+
throw new UncheckedIOException(e);
162+
}
163+
}
164+
165+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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.input;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import java.io.IOException;
24+
import java.io.StringReader;
25+
import java.io.UncheckedIOException;
26+
import java.nio.CharBuffer;
27+
28+
import org.apache.commons.io.IOUtils;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
32+
/**
33+
* Tests {@link UncheckedFilterReader}.
34+
*/
35+
public class UncheckedFilterReaderTest {
36+
37+
private UncheckedFilterReader ucStringReader;
38+
private UncheckedFilterReader ucBrokenReader;
39+
private IOException exception = new IOException("test exception");
40+
41+
@SuppressWarnings("resource")
42+
@BeforeEach
43+
public void beforeEach() {
44+
ucStringReader = UncheckedFilterReader.on(new StringReader("01"));
45+
exception = new IOException("test exception");
46+
ucBrokenReader = UncheckedFilterReader.on(new BrokenReader(exception));
47+
}
48+
49+
@Test
50+
public void testClose() {
51+
ucStringReader.close();
52+
assertThrows(UncheckedIOException.class, () -> ucBrokenReader.read());
53+
}
54+
55+
@Test
56+
public void testCloseThrows() {
57+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> ucBrokenReader.close()).getCause());
58+
}
59+
60+
@Test
61+
public void testMarkReset() {
62+
ucStringReader.mark(10);
63+
final int c = ucStringReader.read();
64+
ucStringReader.reset();
65+
assertEquals(c, ucStringReader.read());
66+
}
67+
68+
@Test
69+
public void testMarkThrows() {
70+
try (UncheckedFilterReader closedReader = UncheckedFilterReader.on(ClosedReader.CLOSED_READER)) {
71+
closedReader.close();
72+
assertThrows(UncheckedIOException.class, () -> closedReader.mark(1));
73+
}
74+
}
75+
76+
@Test
77+
public void testRead() {
78+
try (final UncheckedFilterReader uncheckedReader = UncheckedFilterReader.on(ucStringReader)) {
79+
assertEquals('0', uncheckedReader.read());
80+
assertEquals('1', uncheckedReader.read());
81+
assertEquals(IOUtils.EOF, uncheckedReader.read());
82+
assertEquals(IOUtils.EOF, uncheckedReader.read());
83+
}
84+
}
85+
86+
@Test
87+
public void testReadCharArray() {
88+
try (final UncheckedFilterReader uncheckedReader = UncheckedFilterReader.on(ucStringReader)) {
89+
final char[] array = new char[1];
90+
assertEquals(1, uncheckedReader.read(array));
91+
assertEquals('0', array[0]);
92+
array[0] = 0;
93+
assertEquals(1, uncheckedReader.read(array));
94+
assertEquals('1', array[0]);
95+
array[0] = 0;
96+
assertEquals(IOUtils.EOF, uncheckedReader.read(array));
97+
assertEquals(0, array[0]);
98+
assertEquals(IOUtils.EOF, uncheckedReader.read(array));
99+
assertEquals(0, array[0]);
100+
}
101+
}
102+
103+
@Test
104+
public void testReadCharArrayIndexed() {
105+
try (final UncheckedFilterReader uncheckedReader = UncheckedFilterReader.on(ucStringReader)) {
106+
final char[] array = new char[1];
107+
assertEquals(1, uncheckedReader.read(array, 0, 1));
108+
assertEquals('0', array[0]);
109+
array[0] = 0;
110+
assertEquals(1, uncheckedReader.read(array, 0, 1));
111+
assertEquals('1', array[0]);
112+
array[0] = 0;
113+
assertEquals(IOUtils.EOF, uncheckedReader.read(array, 0, 1));
114+
assertEquals(0, array[0]);
115+
assertEquals(IOUtils.EOF, uncheckedReader.read(array, 0, 1));
116+
assertEquals(0, array[0]);
117+
}
118+
}
119+
120+
@Test
121+
public void testReadCharArrayIndexedThrows() {
122+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> ucBrokenReader.read(new char[1], 0, 1)).getCause());
123+
}
124+
125+
@Test
126+
public void testReadCharArrayThrows() {
127+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> ucBrokenReader.read(new char[1])).getCause());
128+
}
129+
130+
@Test
131+
public void testReadCharBuffer() {
132+
try (final UncheckedFilterReader uncheckedReader = UncheckedFilterReader.on(ucStringReader)) {
133+
final CharBuffer buffer = CharBuffer.wrap(new char[1]);
134+
assertEquals(1, uncheckedReader.read(buffer));
135+
buffer.flip();
136+
assertEquals('0', buffer.charAt(0));
137+
buffer.put(0, (char) 0);
138+
assertEquals(1, uncheckedReader.read(buffer));
139+
buffer.flip();
140+
assertEquals('1', buffer.charAt(0));
141+
buffer.put(0, (char) 0);
142+
assertEquals(IOUtils.EOF, uncheckedReader.read(buffer));
143+
buffer.flip();
144+
assertEquals(0, buffer.length());
145+
assertEquals(0, uncheckedReader.read(buffer));
146+
buffer.flip();
147+
assertEquals(0, buffer.length());
148+
}
149+
}
150+
151+
@Test
152+
public void testReadCharBufferThrows() {
153+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> ucBrokenReader.read(CharBuffer.wrap(new char[1]))).getCause());
154+
}
155+
156+
@Test
157+
public void testReadThrows() {
158+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> ucBrokenReader.read()).getCause());
159+
}
160+
161+
@Test
162+
public void testReady() {
163+
assertTrue(ucStringReader.ready());
164+
}
165+
166+
@Test
167+
public void testReadyThrows() {
168+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> ucBrokenReader.ready()).getCause());
169+
}
170+
171+
@Test
172+
public void testResetThrows() {
173+
try (UncheckedFilterReader closedReader = UncheckedFilterReader.on(ClosedReader.CLOSED_READER)) {
174+
closedReader.close();
175+
assertThrows(UncheckedIOException.class, () -> ucBrokenReader.reset());
176+
}
177+
}
178+
179+
@Test
180+
public void testSkip() {
181+
assertEquals(1, ucStringReader.skip(1));
182+
}
183+
184+
@Test
185+
public void testSkipThrows() {
186+
assertEquals(exception, assertThrows(UncheckedIOException.class, () -> ucBrokenReader.skip(1)).getCause());
187+
}
188+
189+
}

0 commit comments

Comments
 (0)