|
1 | 1 | package org.baeldung.java.io;
|
2 | 2 |
|
| 3 | +import java.io.ByteArrayInputStream; |
3 | 4 | import java.io.File;
|
4 | 5 | import java.io.FileReader;
|
5 | 6 | import java.io.IOException;
|
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.InputStreamReader; |
6 | 9 | import java.io.Reader;
|
7 | 10 | import java.io.StringReader;
|
8 | 11 | import java.nio.charset.Charset;
|
9 | 12 |
|
10 | 13 | import org.apache.commons.io.FileUtils;
|
| 14 | +import org.apache.commons.io.IOUtils; |
11 | 15 | import org.apache.commons.io.input.CharSequenceReader;
|
12 | 16 | import org.junit.Test;
|
13 | 17 | import org.slf4j.Logger;
|
14 | 18 | import org.slf4j.LoggerFactory;
|
15 | 19 |
|
| 20 | +import com.google.common.io.ByteSource; |
| 21 | +import com.google.common.io.ByteStreams; |
16 | 22 | import com.google.common.io.CharSource;
|
17 | 23 |
|
18 | 24 | public class JavaXToReaderUnitTest {
|
@@ -94,4 +100,35 @@ public void givenUsingCommonsIO_whenConvertingFileIntoReader_thenCorrect() throw
|
94 | 100 | targetReader.close();
|
95 | 101 | }
|
96 | 102 |
|
| 103 | + // tests - InputStream to Reader |
| 104 | + |
| 105 | + @Test |
| 106 | + public void givenUsingPlainJava_whenConvertingInputStreamIntoReader_thenCorrect() throws IOException { |
| 107 | + final InputStream initialStream = new ByteArrayInputStream("With Java".getBytes()); |
| 108 | + final Reader targetReader = new InputStreamReader(initialStream); |
| 109 | + |
| 110 | + initialStream.close(); |
| 111 | + targetReader.close(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void givenUsingGuava_whenConvertingInputStreamIntoReader_thenCorrect() throws IOException { |
| 116 | + final InputStream initialStream = ByteSource.wrap("With Guava".getBytes()).openStream(); |
| 117 | + final byte[] buffer = ByteStreams.toByteArray(initialStream); |
| 118 | + final Reader targetReader = CharSource.wrap(new String(buffer)).openStream(); |
| 119 | + |
| 120 | + initialStream.close(); |
| 121 | + targetReader.close(); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void givenUsingCommonsIO_whenConvertingInputStreamIntoReader_thenCorrect() throws IOException { |
| 126 | + final InputStream initialStream = IOUtils.toInputStream("With Commons IO"); |
| 127 | + final byte[] buffer = IOUtils.toByteArray(initialStream); |
| 128 | + final Reader targetReader = new CharSequenceReader(new String(buffer)); |
| 129 | + |
| 130 | + initialStream.close(); |
| 131 | + targetReader.close(); |
| 132 | + } |
| 133 | + |
97 | 134 | }
|
0 commit comments