From c119a51ff995182df4625c0d7fc6357cca959979 Mon Sep 17 00:00:00 2001 From: Roland Weisleder Date: Tue, 19 May 2020 22:24:05 +0200 Subject: [PATCH] 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. --- .../core-java-9-improvements/README.md | 1 + .../core-java-9-improvements/pom.xml | 5 ++ .../InputStreamToByteArrayUnitTest.java | 57 +++++++++++++++++++ .../InputStreamToByteBufferUnitTest.java | 2 +- .../core-java-io-conversions-2/README.md | 1 - .../JavaInputStreamToXUnitTest.java | 37 ------------ 6 files changed, 64 insertions(+), 39 deletions(-) create mode 100644 core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteArrayUnitTest.java rename core-java-modules/{core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtobytes => core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion}/InputStreamToByteBufferUnitTest.java (97%) diff --git a/core-java-modules/core-java-9-improvements/README.md b/core-java-modules/core-java-9-improvements/README.md index c89d0e3c09..3b0bdcd651 100644 --- a/core-java-modules/core-java-9-improvements/README.md +++ b/core-java-modules/core-java-9-improvements/README.md @@ -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) diff --git a/core-java-modules/core-java-9-improvements/pom.xml b/core-java-modules/core-java-9-improvements/pom.xml index d1c6bac9ec..5cc4fce7a9 100644 --- a/core-java-modules/core-java-9-improvements/pom.xml +++ b/core-java-modules/core-java-9-improvements/pom.xml @@ -33,6 +33,11 @@ guava ${guava.version} + + commons-io + commons-io + ${commons-io.version} + org.junit.platform junit-platform-runner diff --git a/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteArrayUnitTest.java b/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteArrayUnitTest.java new file mode 100644 index 0000000000..b64709be09 --- /dev/null +++ b/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteArrayUnitTest.java @@ -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); + } +} diff --git a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtobytes/InputStreamToByteBufferUnitTest.java b/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteBufferUnitTest.java similarity index 97% rename from core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtobytes/InputStreamToByteBufferUnitTest.java rename to core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteBufferUnitTest.java index c10aaae22a..f97352ac27 100644 --- a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtobytes/InputStreamToByteBufferUnitTest.java +++ b/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteBufferUnitTest.java @@ -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; diff --git a/core-java-modules/core-java-io-conversions-2/README.md b/core-java-modules/core-java-io-conversions-2/README.md index 4a28bf37c5..5cb9c21c54 100644 --- a/core-java-modules/core-java-io-conversions-2/README.md +++ b/core-java-modules/core-java-io-conversions-2/README.md @@ -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) diff --git a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtostring/JavaInputStreamToXUnitTest.java b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtostring/JavaInputStreamToXUnitTest.java index c8c711e328..c34c32891f 100644 --- a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtostring/JavaInputStreamToXUnitTest.java +++ b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtostring/JavaInputStreamToXUnitTest.java @@ -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