|
1 | 1 | package org.baeldung.java.io;
|
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.io.FileReader; |
3 | 5 | import java.io.IOException;
|
4 | 6 | import java.io.Reader;
|
5 | 7 | import java.io.StringReader;
|
| 8 | +import java.nio.charset.Charset; |
6 | 9 |
|
| 10 | +import org.apache.commons.io.FileUtils; |
7 | 11 | import org.apache.commons.io.input.CharSequenceReader;
|
8 | 12 | import org.junit.Test;
|
9 | 13 | import org.slf4j.Logger;
|
@@ -37,4 +41,57 @@ public void givenUsingCommonsIO_whenConvertingStringIntoReader_thenCorrect() thr
|
37 | 41 | targetReader.close();
|
38 | 42 | }
|
39 | 43 |
|
| 44 | + // tests - byte array to Reader |
| 45 | + |
| 46 | + @Test |
| 47 | + public void givenUsingPlainJava_whenConvertingByteArrayIntoReader_thenCorrect() throws IOException { |
| 48 | + final byte[] initialArray = "Hello world!".getBytes(); |
| 49 | + final Reader targetReader = new StringReader(new String(initialArray)); |
| 50 | + targetReader.close(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void givenUsingGuava_whenConvertingByteArrayIntoReader_thenCorrect() throws IOException { |
| 55 | + final byte[] initialArray = "With Guava".getBytes(); |
| 56 | + final String bufferString = new String(initialArray); |
| 57 | + final Reader targetReader = CharSource.wrap(bufferString).openStream(); |
| 58 | + |
| 59 | + targetReader.close(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenUsingCommonsIO_whenConvertingByteArrayIntoReader_thenCorrect() throws IOException { |
| 64 | + final byte[] initialArray = "With Commons IO".getBytes(); |
| 65 | + final Reader targetReader = new CharSequenceReader(new String(initialArray)); |
| 66 | + targetReader.close(); |
| 67 | + } |
| 68 | + |
| 69 | + // tests - File to Reader |
| 70 | + |
| 71 | + @Test |
| 72 | + public void givenUsingPlainJava_whenConvertingFileIntoReader_thenCorrect() throws IOException { |
| 73 | + final File initialFile = new File("src/test/resources/initialFile.txt"); |
| 74 | + initialFile.createNewFile(); |
| 75 | + final Reader targetReader = new FileReader(initialFile); |
| 76 | + targetReader.close(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void givenUsingGuava_whenConvertingFileIntoReader_thenCorrect() throws IOException { |
| 81 | + final File initialFile = new File("src/test/resources/initialFile.txt"); |
| 82 | + com.google.common.io.Files.touch(initialFile); |
| 83 | + final Reader targetReader = com.google.common.io.Files.newReader(initialFile, Charset.defaultCharset()); |
| 84 | + targetReader.close(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void givenUsingCommonsIO_whenConvertingFileIntoReader_thenCorrect() throws IOException { |
| 89 | + final File initialFile = new File("src/test/resources/initialFile.txt"); |
| 90 | + FileUtils.touch(initialFile); |
| 91 | + FileUtils.write(initialFile, "With Commons IO"); |
| 92 | + final byte[] buffer = FileUtils.readFileToByteArray(initialFile); |
| 93 | + final Reader targetReader = new CharSequenceReader(new String(buffer)); |
| 94 | + targetReader.close(); |
| 95 | + } |
| 96 | + |
40 | 97 | }
|
0 commit comments