2121import java .io .File ;
2222import java .io .IOException ;
2323import java .io .InputStream ;
24+ import java .io .InputStreamReader ;
2425import java .io .OutputStream ;
26+ import java .io .OutputStreamWriter ;
2527import java .io .Reader ;
2628import java .io .Writer ;
2729import java .net .URI ;
3234import java .nio .file .Paths ;
3335import java .util .Objects ;
3436
37+ import org .apache .commons .io .IOUtils ;
38+ import org .apache .commons .io .input .ReaderInputStream ;
39+ import org .apache .commons .io .output .WriterOutputStream ;
40+
3541/**
3642 * Abstracts the origin of data for builders like a {@link File}, {@link Path}, {@link Reader}, {@link Writer}, {@link InputStream}, {@link OutputStream}, and
3743 * {@link URI}.
@@ -71,6 +77,11 @@ public InputStream getInputStream(final OpenOption... options) throws IOExceptio
7177 return new ByteArrayInputStream (origin );
7278 }
7379
80+ @ Override
81+ public Reader getReader (final Charset charset ) throws IOException {
82+ return new InputStreamReader (getInputStream (), charset );
83+ }
84+
7485 }
7586
7687 /**
@@ -87,6 +98,12 @@ public CharSequenceOrigin(final CharSequence origin) {
8798 super (origin );
8899 }
89100
101+ @ Override
102+ public byte [] getByteArray () {
103+ // TODO Pass in a Charset? Consider if call sites actually need this.
104+ return origin .toString ().getBytes (Charset .defaultCharset ());
105+ }
106+
90107 @ Override
91108 public CharSequence getCharSequence (final Charset charset ) {
92109 // No conversion
@@ -101,6 +118,11 @@ public InputStream getInputStream(final OpenOption... options) throws IOExceptio
101118 // return CharSequenceInputStream.builder().setCharSequence(getCharSequence(Charset.defaultCharset())).get();
102119 }
103120
121+ @ Override
122+ public Reader getReader (final Charset charset ) throws IOException {
123+ return new InputStreamReader (getInputStream (), charset );
124+ }
125+
104126 }
105127
106128 /**
@@ -136,7 +158,7 @@ public Path getPath() {
136158 /**
137159 * An {@link InputStream} origin.
138160 * <p>
139- * This origin cannot provide other aspects.
161+ * This origin cannot provide some of the other aspects.
140162 * </p>
141163 */
142164 public static class InputStreamOrigin extends AbstractOrigin <InputStream , InputStreamOrigin > {
@@ -150,18 +172,28 @@ public InputStreamOrigin(final InputStream origin) {
150172 super (origin );
151173 }
152174
175+ @ Override
176+ public byte [] getByteArray () throws IOException {
177+ return IOUtils .toByteArray (origin );
178+ }
179+
153180 @ Override
154181 public InputStream getInputStream (final OpenOption ... options ) {
155182 // No conversion
156183 return get ();
157184 }
158185
186+ @ Override
187+ public Reader getReader (final Charset charset ) throws IOException {
188+ return new InputStreamReader (getInputStream (), charset );
189+ }
190+
159191 }
160192
161193 /**
162194 * An {@link OutputStream} origin.
163195 * <p>
164- * This origin cannot provide other aspects.
196+ * This origin cannot provide some of the other aspects.
165197 * </p>
166198 */
167199 public static class OutputStreamOrigin extends AbstractOrigin <OutputStream , OutputStreamOrigin > {
@@ -181,6 +213,10 @@ public OutputStream getOutputStream(final OpenOption... options) {
181213 return get ();
182214 }
183215
216+ @ Override
217+ public Writer getWriter (final Charset charset , final OpenOption ... options ) throws IOException {
218+ return new OutputStreamWriter (origin , charset );
219+ }
184220 }
185221
186222 /**
@@ -230,6 +266,23 @@ public ReaderOrigin(final Reader origin) {
230266 super (origin );
231267 }
232268
269+ @ Override
270+ public byte [] getByteArray () throws IOException {
271+ // TODO Pass in a Charset? Consider if call sites actually need this.
272+ return IOUtils .toByteArray (origin , Charset .defaultCharset ());
273+ }
274+
275+ @ Override
276+ public CharSequence getCharSequence (final Charset charset ) throws IOException {
277+ return IOUtils .toString (origin );
278+ }
279+
280+ @ Override
281+ public InputStream getInputStream (final OpenOption ... options ) throws IOException {
282+ // TODO Pass in a Charset? Consider if call sites actually need this.
283+ return ReaderInputStream .builder ().setReader (origin ).setCharset (Charset .defaultCharset ()).get ();
284+ }
285+
233286 @ Override
234287 public Reader getReader (final Charset charset ) throws IOException {
235288 // No conversion
@@ -280,6 +333,12 @@ public WriterOrigin(final Writer origin) {
280333 super (origin );
281334 }
282335
336+ @ Override
337+ public OutputStream getOutputStream (final OpenOption ... options ) throws IOException {
338+ // TODO Pass in a Charset? Consider if call sites actually need this.
339+ return WriterOutputStream .builder ().setWriter (origin ).setCharset (Charset .defaultCharset ()).get ();
340+ }
341+
283342 @ Override
284343 public Writer getWriter (final Charset charset , final OpenOption ... options ) throws IOException {
285344 // No conversion
@@ -315,7 +374,7 @@ public T get() {
315374 * Gets this origin as a byte array, if possible.
316375 *
317376 * @return this origin as a byte array, if possible.
318- * @throws IOException if an I/O error occurs.
377+ * @throws IOException if an I/O error occurs.
319378 * @throws UnsupportedOperationException if the origin cannot be converted to a Path.
320379 */
321380 public byte [] getByteArray () throws IOException {
@@ -327,7 +386,7 @@ public byte[] getByteArray() throws IOException {
327386 *
328387 * @param charset The charset to use if conversion from bytes is needed.
329388 * @return this origin as a byte array, if possible.
330- * @throws IOException if an I/O error occurs.
389+ * @throws IOException if an I/O error occurs.
331390 * @throws UnsupportedOperationException if the origin cannot be converted to a Path.
332391 */
333392 public CharSequence getCharSequence (final Charset charset ) throws IOException {
@@ -349,7 +408,7 @@ public File getFile() {
349408 *
350409 * @param options options specifying how the file is opened
351410 * @return this origin as an InputStream, if possible.
352- * @throws IOException if an I/O error occurs.
411+ * @throws IOException if an I/O error occurs.
353412 * @throws UnsupportedOperationException if the origin cannot be converted to a Path.
354413 */
355414 public InputStream getInputStream (final OpenOption ... options ) throws IOException {
@@ -361,7 +420,7 @@ public InputStream getInputStream(final OpenOption... options) throws IOExceptio
361420 *
362421 * @param options options specifying how the file is opened
363422 * @return this origin as an OutputStream, if possible.
364- * @throws IOException if an I/O error occurs.
423+ * @throws IOException if an I/O error occurs.
365424 * @throws UnsupportedOperationException if the origin cannot be converted to a Path.
366425 */
367426 public OutputStream getOutputStream (final OpenOption ... options ) throws IOException {
@@ -395,7 +454,7 @@ public Reader getReader(final Charset charset) throws IOException {
395454 * @param charset the charset to use for encoding
396455 * @param options options specifying how the file is opened
397456 * @return a new Writer on the origin.
398- * @throws IOException if an I/O error occurs opening or creating the file.
457+ * @throws IOException if an I/O error occurs opening or creating the file.
399458 * @throws UnsupportedOperationException if the origin cannot be converted to a Path.
400459 */
401460 public Writer getWriter (final Charset charset , final OpenOption ... options ) throws IOException {
0 commit comments