BAEL-4585: Add readNbytes code sample (#10814)

* BAEL-4585: Add readNbytes code example

* BAEL-4585: Fix code samples

Co-authored-by: Krzysztof Woyke <krzysztof.woyke.sp@lhsystems.com>
This commit is contained in:
kwoyke 2021-05-28 12:51:10 +02:00 committed by GitHub
parent c5582303e8
commit 805e96f784
1 changed files with 26 additions and 7 deletions

View File

@ -15,24 +15,43 @@ public class InputStreamToByteArrayUnitTest {
@Test
public final void givenUsingPlainJavaOnFixedSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
final byte[] targetArray = new byte[initialStream.available()];
initialStream.read(targetArray);
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
final byte[] targetArray = new byte[is.available()];
is.read(targetArray);
}
@Test
public final void givenUsingPlainJavaOnUnknownSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5, 6 });
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
final byte[] data = new byte[1024];
final byte[] data = new byte[4];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
final byte[] byteArray = buffer.toByteArray();
final byte[] targetArray = buffer.toByteArray();
}
@Test
public final void givenUsingPlainJava9OnUnknownSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5, 6 });
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
final byte[] data = new byte[4];
while ((nRead = is.readNBytes(data, 0, data.length)) != 0) {
System.out.println("here " + nRead);
buffer.write(data, 0, nRead);
}
buffer.flush();
final byte[] targetArray = buffer.toByteArray();
}
@Test