|
7 | 7 |
|
8 | 8 | import java.io.BufferedReader; |
9 | 9 | import java.io.ByteArrayInputStream; |
| 10 | +import java.io.ByteArrayOutputStream; |
10 | 11 | import java.io.File; |
11 | 12 | import java.io.FileInputStream; |
12 | 13 | import java.io.FileOutputStream; |
@@ -128,12 +129,27 @@ public final void givenUsingCommonsIoWithCopy_whenConvertingAnInputStreamToAStri |
128 | 129 | // tests - InputStream to byte[] |
129 | 130 |
|
130 | 131 | @Test |
131 | | - public final void givenUsingPlainJava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException { |
| 132 | + public final void givenUsingPlainJavaOnFixedSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException { |
132 | 133 | final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 }); |
133 | 134 | final byte[] targetArray = new byte[initialStream.available()]; |
134 | 135 | initialStream.read(targetArray); |
135 | 136 | } |
136 | 137 |
|
| 138 | + @Test |
| 139 | + public final void givenUsingPlainJavaOnUnknownSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException { |
| 140 | + final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 }); |
| 141 | + |
| 142 | + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 143 | + int nRead; |
| 144 | + final byte[] data = new byte[1024]; |
| 145 | + while ((nRead = is.read(data, 0, data.length)) != -1) { |
| 146 | + buffer.write(data, 0, nRead); |
| 147 | + } |
| 148 | + |
| 149 | + buffer.flush(); |
| 150 | + final byte[] byteArray = buffer.toByteArray(); |
| 151 | + } |
| 152 | + |
137 | 153 | @Test |
138 | 154 | public final void givenUsingGuava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException { |
139 | 155 | final InputStream initialStream = ByteSource.wrap(new byte[] { 0, 1, 2 }).openStream(); |
|
0 commit comments