|
5 | 5 | import static org.junit.Assert.assertThat; |
6 | 6 |
|
7 | 7 | import java.io.ByteArrayInputStream; |
8 | | -import java.io.File; |
9 | | -import java.io.FileInputStream; |
10 | 8 | import java.io.IOException; |
11 | 9 | import java.io.InputStream; |
12 | 10 | import java.io.Reader; |
13 | 11 | import java.io.StringReader; |
| 12 | +import java.io.StringWriter; |
14 | 13 | import java.nio.charset.StandardCharsets; |
15 | 14 | import java.util.Scanner; |
16 | 15 |
|
17 | | -import org.apache.commons.io.FileUtils; |
| 16 | +import org.apache.commons.io.IOUtils; |
18 | 17 | import org.junit.Test; |
19 | 18 | import org.slf4j.Logger; |
20 | 19 | import org.slf4j.LoggerFactory; |
21 | 20 |
|
22 | 21 | import com.google.common.base.Charsets; |
23 | 22 | import com.google.common.io.ByteSource; |
24 | 23 | import com.google.common.io.CharStreams; |
25 | | -import com.google.common.io.Files; |
26 | 24 | import com.google.common.io.InputSupplier; |
27 | 25 |
|
28 | 26 | public class CoreJavaIoUnitTest { |
29 | 27 | protected final Logger logger = LoggerFactory.getLogger(getClass()); |
30 | 28 |
|
31 | | - // tests - iterate lines in a file |
32 | | - |
33 | | - @Test |
34 | | - public final void givenUsingGuava_whenIteratingAFile_thenCorrect() throws IOException { |
35 | | - final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv"; |
36 | | - // final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv"; |
37 | | - |
38 | | - logMemory(); |
39 | | - Files.readLines(new File(path), Charsets.UTF_8); |
40 | | - logMemory(); |
41 | | - } |
42 | | - |
43 | | - @Test |
44 | | - public final void givenUsingCommonsIo_whenIteratingAFile_thenCorrect() throws IOException { |
45 | | - final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv"; |
46 | | - // final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv"; |
47 | | - |
48 | | - logMemory(); |
49 | | - FileUtils.readLines(new File(path)); |
50 | | - logMemory(); |
51 | | - } |
52 | | - |
53 | | - @Test |
54 | | - public final void whenStreamingThroughAFile_thenCorrect() throws IOException { |
55 | | - final String path = "G:\\full\\train\\input\\" + "trainDataNegative.csv"; |
56 | | - // final String path = "G:\\full\\train\\input\\" + "trainDataPositive.csv"; |
57 | | - |
58 | | - logMemory(); |
59 | | - |
60 | | - FileInputStream inputStream = null; |
61 | | - Scanner sc = null; |
62 | | - try { |
63 | | - inputStream = new FileInputStream(path); |
64 | | - sc = new Scanner(inputStream, "UTF-8"); |
65 | | - while (sc.hasNextLine()) { |
66 | | - final String line = sc.nextLine(); |
67 | | - // System.out.println(line); |
68 | | - } |
69 | | - // note that Scanner suppresses exceptions |
70 | | - if (sc.ioException() != null) { |
71 | | - throw sc.ioException(); |
72 | | - } |
73 | | - } finally { |
74 | | - if (inputStream != null) { |
75 | | - inputStream.close(); |
76 | | - } |
77 | | - if (sc != null) { |
78 | | - sc.close(); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - logMemory(); |
83 | | - } |
84 | | - |
85 | 29 | // tests - InputStream to String |
86 | 30 |
|
87 | 31 | @Test |
@@ -120,12 +64,27 @@ public final void givenUsingGuavaWithEncoding_whenConvertingAnInputStreamToAStri |
120 | 64 | assertThat(text, equalTo(originalString)); |
121 | 65 | } |
122 | 66 |
|
123 | | - // utils |
| 67 | + @Test |
| 68 | + public final void givenUsingCommonsIoWithEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { |
| 69 | + final String originalString = randomAlphabetic(8); |
| 70 | + final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); |
| 71 | + |
| 72 | + // When |
| 73 | + final String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name()); |
| 74 | + assertThat(text, equalTo(originalString)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public final void givenUsingCommonsIoWithEncoding2_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { |
| 79 | + final String originalString = randomAlphabetic(8); |
| 80 | + final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); |
| 81 | + |
| 82 | + // When |
| 83 | + final StringWriter writer = new StringWriter(); |
| 84 | + final String encoding = StandardCharsets.UTF_8.name(); |
| 85 | + IOUtils.copy(inputStream, writer, encoding); |
124 | 86 |
|
125 | | - private final void logMemory() { |
126 | | - logger.info("Max Memory: {} Mb", Runtime.getRuntime().maxMemory() / 1048576); |
127 | | - logger.info("Total Memory: {} Mb", Runtime.getRuntime().totalMemory() / 1048576); |
128 | | - logger.info("Free Memory: {} Mb", Runtime.getRuntime().freeMemory() / 1048576); |
| 87 | + assertThat(writer.toString(), equalTo(originalString)); |
129 | 88 | } |
130 | 89 |
|
131 | 90 | } |
0 commit comments