|
11 | 11 | import com.google.common.io.ByteStreams; |
12 | 12 |
|
13 | 13 | public class InputStreamToOutputStreamUnitTest { |
14 | | - |
15 | | - // buffer size used for reading and writing |
16 | | - private static final int BUFFER_SIZE = 8192; |
17 | | - |
| 14 | + |
18 | 15 | /** |
19 | 16 | * Reads all bytes from an input stream and writes them to an output stream. |
20 | 17 | * @param source - input stream to copy data from |
21 | 18 | * @param target - output stream to copy data too |
22 | 19 | */ |
23 | | - private static void copy(InputStream source, OutputStream target) throws IOException { |
24 | | - byte[] buf = new byte[BUFFER_SIZE]; |
| 20 | + void copy(InputStream source, OutputStream target) throws IOException { |
| 21 | + byte[] buf = new byte[8192]; |
25 | 22 | int length; |
26 | 23 | while ((length = source.read(buf)) > 0) { |
27 | 24 | target.write(buf, 0, length); |
28 | 25 | } |
29 | 26 | } |
30 | 27 |
|
31 | 28 | @Test |
32 | | - public final void givenUsingJavaEight_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { |
33 | | - final String initialString = "Hello World!"; |
| 29 | + public void givenUsingJavaEight_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { |
| 30 | + String initialString = "Hello World!"; |
34 | 31 |
|
35 | 32 | try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
36 | 33 | ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
37 | 34 | copy(inputStream, targetStream); |
| 35 | + |
38 | 36 | assertEquals(initialString, new String(targetStream.toByteArray())); |
39 | 37 | } |
40 | 38 | } |
41 | 39 |
|
42 | 40 | @Test |
43 | | - public final void givenUsingJavaEight_whenCopyingLongInputStreamToOutputStream_thenCorrect() throws IOException { |
44 | | - final String initialString = randomAlphabetic(20480); |
| 41 | + public void givenUsingJavaEight_whenCopyingLongInputStreamToOutputStream_thenCorrect() throws IOException { |
| 42 | + String initialString = randomAlphabetic(20480); |
45 | 43 |
|
46 | 44 | try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
47 | 45 | ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
48 | 46 | copy(inputStream, targetStream); |
| 47 | + |
49 | 48 | assertEquals(initialString, new String(targetStream.toByteArray())); |
50 | 49 | } |
51 | 50 | } |
52 | 51 |
|
53 | 52 | @Test |
54 | | - public final void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { |
55 | | - final String initialString = "Hello World!"; |
| 53 | + public void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { |
| 54 | + String initialString = "Hello World!"; |
56 | 55 |
|
57 | 56 | try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
58 | 57 | ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
59 | 58 | inputStream.transferTo(targetStream); |
| 59 | + |
60 | 60 | assertEquals(initialString, new String(targetStream.toByteArray())); |
61 | 61 | } |
62 | 62 | } |
63 | 63 |
|
64 | 64 | @Test |
65 | | - public final void givenUsingGuava_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { |
66 | | - final String initialString = "Hello World!"; |
| 65 | + public void givenUsingGuava_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { |
| 66 | + String initialString = "Hello World!"; |
67 | 67 |
|
68 | 68 | try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
69 | 69 | ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
70 | 70 | ByteStreams.copy(inputStream, targetStream); |
| 71 | + |
71 | 72 | assertEquals(initialString, new String(targetStream.toByteArray())); |
72 | 73 | } |
73 | 74 | } |
74 | 75 |
|
75 | 76 | @Test |
76 | | - public final void givenUsingCommonsIO_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { |
77 | | - final String initialString = "Hello World!"; |
| 77 | + public void givenUsingCommonsIO_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { |
| 78 | + String initialString = "Hello World!"; |
78 | 79 |
|
79 | 80 | try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
80 | 81 | ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
81 | 82 | IOUtils.copy(inputStream, targetStream); |
| 83 | + |
82 | 84 | assertEquals(initialString, new String(targetStream.toByteArray())); |
83 | 85 | } |
84 | 86 | } |
|
0 commit comments