Skip to content

Commit 3b95c8c

Browse files
committed
Add and use AbstractStreamBuilder.getReader()
Update Javadoc
1 parent 5e3c7e8 commit 3b95c8c

5 files changed

Lines changed: 32 additions & 22 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ The <action> type attribute can be add,update,fix,remove.
123123
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ThrottledInputStream.</action>
124124
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IORunnable.noop().</action>
125125
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ChecksumInputStream and test #548.</action>
126+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractStreamBuilder.getReader().</action>
126127
<!-- UPDATE -->
127128
<action dev="ggregory" type="fix" due-to="Gary Gregory">Bump commons.bytebuddy.version from 1.14.10 to 1.14.11 #534.</action>
128129
</release>

src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.io.OutputStream;
23+
import java.io.Reader;
2324
import java.io.Writer;
2425
import java.nio.charset.Charset;
2526
import java.nio.file.OpenOption;
@@ -193,7 +194,22 @@ protected Path getPath() {
193194
}
194195

195196
/**
196-
* Gets a Writer from the origin with OpenOption[].
197+
* Gets a Reader from the origin with a Charset.
198+
*
199+
* @return A Reader
200+
* @throws IllegalStateException if the {@code origin} is {@code null}.
201+
* @throws UnsupportedOperationException if the origin cannot be converted to a {@link Reader}.
202+
* @throws IOException if an I/O error occurs.
203+
* @see AbstractOrigin#getReader(Charset)
204+
* @see #getCharset()
205+
* @since 2.16.0
206+
*/
207+
protected Reader getReader() throws IOException {
208+
return checkOrigin().getReader(getCharset());
209+
}
210+
211+
/**
212+
* Gets a Writer from the origin with an OpenOption[].
197213
*
198214
* @return An writer.
199215
* @throws IllegalStateException if the {@code origin} is {@code null}.

src/main/java/org/apache/commons/io/input/ReaderInputStream.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import org.apache.commons.io.Charsets;
3333
import org.apache.commons.io.IOUtils;
34-
import org.apache.commons.io.build.AbstractOrigin;
3534
import org.apache.commons.io.build.AbstractStreamBuilder;
3635
import org.apache.commons.io.charset.CharsetEncoders;
3736

@@ -111,13 +110,13 @@ public static class Builder extends AbstractStreamBuilder<ReaderInputStream, Bui
111110
* Builds a new {@link ReaderInputStream}.
112111
*
113112
* <p>
114-
* You must set input that supports {@link Reader}, otherwise, this method throws an exception.
113+
* You must set input that supports {@link #getReader()}, otherwise, this method throws an exception.
115114
* </p>
116115
* <p>
117116
* This builder use the following aspects:
118117
* </p>
119118
* <ul>
120-
* <li>{@link Reader}</li>
119+
* <li>{@link #getReader()}</li>
121120
* <li>{@link #getBufferSize()}</li>
122121
* <li>{@link #getCharset()}</li>
123122
* <li>{@link CharsetEncoder}</li>
@@ -126,14 +125,14 @@ public static class Builder extends AbstractStreamBuilder<ReaderInputStream, Bui
126125
* @return a new instance.
127126
* @throws UnsupportedOperationException if the origin cannot provide a Reader.
128127
* @throws IllegalStateException if the {@code origin} is {@code null}.
129-
* @see AbstractOrigin#getReader(Charset)
128+
* @see #getReader()
130129
* @see CharsetEncoder
131130
* @see #getBufferSize()
132131
*/
133132
@SuppressWarnings("resource")
134133
@Override
135134
public ReaderInputStream get() throws IOException {
136-
return new ReaderInputStream(checkOrigin().getReader(getCharset()), charsetEncoder, getBufferSize());
135+
return new ReaderInputStream(getReader(), charsetEncoder, getBufferSize());
137136
}
138137

139138
CharsetEncoder getCharsetEncoder() {

src/main/java/org/apache/commons/io/input/UncheckedBufferedReader.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import java.io.Reader;
2323
import java.io.UncheckedIOException;
2424
import java.nio.CharBuffer;
25-
import java.nio.charset.Charset;
2625

27-
import org.apache.commons.io.build.AbstractOrigin;
2826
import org.apache.commons.io.build.AbstractStreamBuilder;
2927
import org.apache.commons.io.function.Uncheck;
3028

@@ -76,27 +74,26 @@ public static class Builder extends AbstractStreamBuilder<UncheckedBufferedReade
7674
* Builds a new {@link UncheckedBufferedReader}.
7775
*
7876
* <p>
77+
* You must set input that supports {@link #getReader()} on this builder, otherwise, this method throws an exception.
78+
* </p>
79+
* <p>
7980
* This builder use the following aspects:
8081
* </p>
8182
* <ul>
82-
* <li>{@link Reader}</li>
83+
* <li>{@link #getReader()}</li>
8384
* <li>{@link #getBufferSize()}</li>
8485
* </ul>
85-
* <p>
86-
* You must provide an origin that can be converted to a Reader by this builder, otherwise, this call will throw an
87-
* {@link UnsupportedOperationException}.
88-
* </p>
8986
*
9087
* @return a new instance.
9188
* @throws UnsupportedOperationException if the origin cannot provide a Reader.
9289
* @throws IllegalStateException if the {@code origin} is {@code null}.
93-
* @see AbstractOrigin#getReader(Charset)
90+
* @see #getReader()
9491
* @see #getBufferSize()
9592
*/
9693
@Override
9794
public UncheckedBufferedReader get() {
9895
// This an unchecked class, so this method is as well.
99-
return Uncheck.get(() -> new UncheckedBufferedReader(checkOrigin().getReader(getCharset()), getBufferSize()));
96+
return Uncheck.get(() -> new UncheckedBufferedReader(getReader(), getBufferSize()));
10097
}
10198

10299
}

src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import java.io.Reader;
2323
import java.io.UncheckedIOException;
2424
import java.nio.CharBuffer;
25-
import java.nio.charset.Charset;
2625

27-
import org.apache.commons.io.build.AbstractOrigin;
2826
import org.apache.commons.io.build.AbstractStreamBuilder;
2927
import org.apache.commons.io.function.Uncheck;
3028

@@ -71,25 +69,24 @@ public static class Builder extends AbstractStreamBuilder<UncheckedFilterReader,
7169
/**
7270
* Builds a new {@link UncheckedFilterReader}.
7371
* <p>
74-
* You must set input that supports {@link Reader} on this builder, otherwise, this method throws an exception.
72+
* You must set input that supports {@link #getReader()} on this builder, otherwise, this method throws an exception.
7573
* </p>
7674
* <p>
7775
* This builder use the following aspects:
7876
* </p>
7977
* <ul>
80-
* <li>{@link Reader}</li>
81-
* <li>{@link #getCharset()}</li>
78+
* <li>{@link #getReader()}</li>
8279
* </ul>
8380
*
8481
* @return a new instance.
8582
* @throws UnsupportedOperationException if the origin cannot provide a Reader.
8683
* @throws IllegalStateException if the {@code origin} is {@code null}.
87-
* @see AbstractOrigin#getReader(Charset)
84+
* @see #getReader()
8885
*/
8986
@Override
9087
public UncheckedFilterReader get() {
9188
// This an unchecked class, so this method is as well.
92-
return Uncheck.get(() -> new UncheckedFilterReader(checkOrigin().getReader(getCharset())));
89+
return Uncheck.get(() -> new UncheckedFilterReader(getReader()));
9390
}
9491

9592
}

0 commit comments

Comments
 (0)