BAEL-4010 Add test for InputStream.readAllBytes() (#9265)

* BAEL-4010 Add test for InputStream.readAllBytes()

* BAEL-4010 Refactor: Move "InputStream to Bytes" to Java 9 specific module

The issue BAEL-4010 introduces a new example to the article "Java
InputStream to Byte Array and ByteBuffer". The example is about
  byte[] InputStream.readAllBytes()
which was added with Java 9.

To be consistent, all examples for this article were moved to a module
which is compiled with at least Java 9.
This commit is contained in:
Roland Weisleder 2020-05-19 22:24:05 +02:00 committed by GitHub
parent 7bc07fdac9
commit c119a51ff9
6 changed files with 64 additions and 39 deletions

View File

@ -9,3 +9,4 @@ This module contains articles about the improvements to core Java features intro
- [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api)
- [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new)
- [Java 9 CompletableFuture API Improvements](https://www.baeldung.com/java-9-completablefuture)
- [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes)

View File

@ -33,6 +33,11 @@
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>

View File

@ -0,0 +1,57 @@
package com.baeldung.java9.io.conversion;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
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);
}
@Test
public final void givenUsingPlainJavaOnUnknownSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
final byte[] data = new byte[1024];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
final byte[] byteArray = buffer.toByteArray();
}
@Test
public void givenUsingPlainJava9_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
byte[] data = is.readAllBytes();
}
@Test
public final void givenUsingGuava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream initialStream = ByteSource.wrap(new byte[] { 0, 1, 2 })
.openStream();
final byte[] targetArray = ByteStreams.toByteArray(initialStream);
}
@Test
public final void givenUsingCommonsIO_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
final byte[] targetArray = IOUtils.toByteArray(initialStream);
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.inputstreamtobytes;
package com.baeldung.java9.io.conversion;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;

View File

@ -4,6 +4,5 @@ This module contains articles about core Java input/output(IO) conversions.
### Relevant Articles:
- [Java InputStream to String](https://www.baeldung.com/convert-input-stream-to-string)
- [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes)
- [Java Write an InputStream to a File](https://www.baeldung.com/convert-input-stream-to-a-file)
- More articles: [[<-- prev]](/core-java-modules/core-java-io-conversions)

View File

@ -2,7 +2,6 @@ package com.baeldung.inputstreamtostring;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
@ -152,42 +151,6 @@ public class JavaInputStreamToXUnitTest {
assertThat(result, equalTo(originalString));
}
// tests - InputStream to byte[]
@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);
}
@Test
public final void givenUsingPlainJavaOnUnknownSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
final byte[] data = new byte[1024];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
final byte[] byteArray = buffer.toByteArray();
}
@Test
public final void givenUsingGuava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream initialStream = ByteSource.wrap(new byte[] { 0, 1, 2 }).openStream();
final byte[] targetArray = ByteStreams.toByteArray(initialStream);
}
@Test
public final void givenUsingCommonsIO_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
final byte[] targetArray = IOUtils.toByteArray(initialStream);
}
// tests - InputStream to File
@Test