Skip to content

Commit e3a960c

Browse files
committed
Add UncheckedBufferedReader.
1 parent 5427920 commit e3a960c

3 files changed

Lines changed: 401 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ The <action> type attribute can be add,update,fix,remove.
5454
<action dev="ggregory" type="add" due-to="Gary Gregory">
5555
Add BrokenReader.INSTANCE.
5656
</action>
57+
<action dev="ggregory" type="add" due-to="Gary Gregory">
58+
Add UncheckedBufferedReader.
59+
</action>
5760
<!-- UPDATE -->
5861
<action dev="ggregory" type="update" due-to="Dependabot">
5962
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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.BufferedReader;
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 BufferedReader} that throws {@link UncheckedIOException} instead of {@link IOException}.
28+
*
29+
* @see BufferedReader
30+
* @see IOException
31+
* @see UncheckedIOException
32+
* @since 2.12.0
33+
*/
34+
public class UncheckedBufferedReader extends BufferedReader {
35+
36+
/**
37+
* Creates a new buffered reader.
38+
*
39+
* @param reader a Reader object providing the underlying stream.
40+
* @return a new UncheckedBufferedReader.
41+
* @throws NullPointerException if {@code reader} is {@code null}.
42+
*/
43+
public static UncheckedBufferedReader on(final Reader reader) {
44+
return new UncheckedBufferedReader(reader);
45+
}
46+
47+
/**
48+
* Creates a buffering character-input stream that uses a default-sized input buffer.
49+
*
50+
* @param reader A Reader
51+
*/
52+
public UncheckedBufferedReader(final Reader reader) {
53+
super(reader);
54+
}
55+
56+
/**
57+
* Creates a buffering character-input stream that uses an input buffer of the specified size.
58+
*
59+
* @param reader A Reader
60+
* @param bufferSize Input-buffer size
61+
*
62+
* @exception IllegalArgumentException If {@code bufferSize <= 0}
63+
*/
64+
public UncheckedBufferedReader(final Reader reader, final int bufferSize) {
65+
super(reader, bufferSize);
66+
}
67+
68+
/**
69+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
70+
*/
71+
@Override
72+
public void close() throws UncheckedIOException {
73+
try {
74+
super.close();
75+
} catch (final IOException e) {
76+
throw new UncheckedIOException(e);
77+
}
78+
}
79+
80+
/**
81+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
82+
*/
83+
@Override
84+
public void mark(final int readAheadLimit) throws UncheckedIOException {
85+
try {
86+
super.mark(readAheadLimit);
87+
} catch (final IOException e) {
88+
throw new UncheckedIOException(e);
89+
}
90+
}
91+
92+
/**
93+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
94+
*/
95+
@Override
96+
public int read() throws UncheckedIOException {
97+
try {
98+
return super.read();
99+
} catch (final IOException e) {
100+
throw new UncheckedIOException(e);
101+
}
102+
}
103+
104+
/**
105+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
106+
*/
107+
@Override
108+
public int read(final char[] cbuf) throws UncheckedIOException {
109+
try {
110+
return super.read(cbuf);
111+
} catch (final IOException e) {
112+
throw new UncheckedIOException(e);
113+
}
114+
}
115+
116+
/**
117+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
118+
*/
119+
@Override
120+
public int read(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
121+
try {
122+
return super.read(cbuf, off, len);
123+
} catch (final IOException e) {
124+
throw new UncheckedIOException(e);
125+
}
126+
}
127+
128+
/**
129+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
130+
*/
131+
@Override
132+
public int read(final CharBuffer target) throws UncheckedIOException {
133+
try {
134+
return super.read(target);
135+
} catch (final IOException e) {
136+
throw new UncheckedIOException(e);
137+
}
138+
}
139+
140+
/**
141+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
142+
*/
143+
@Override
144+
public String readLine() throws UncheckedIOException {
145+
try {
146+
return super.readLine();
147+
} catch (final IOException e) {
148+
throw new UncheckedIOException(e);
149+
}
150+
}
151+
152+
/**
153+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
154+
*/
155+
@Override
156+
public boolean ready() throws UncheckedIOException {
157+
try {
158+
return super.ready();
159+
} catch (final IOException e) {
160+
throw new UncheckedIOException(e);
161+
}
162+
}
163+
164+
/**
165+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
166+
*/
167+
@Override
168+
public void reset() throws UncheckedIOException {
169+
try {
170+
super.reset();
171+
} catch (final IOException e) {
172+
throw new UncheckedIOException(e);
173+
}
174+
}
175+
176+
/**
177+
* Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
178+
*/
179+
@Override
180+
public long skip(final long n) throws UncheckedIOException {
181+
try {
182+
return super.skip(n);
183+
} catch (final IOException e) {
184+
throw new UncheckedIOException(e);
185+
}
186+
}
187+
188+
}

0 commit comments

Comments
 (0)