From d63c0834da66cebaf52d318d7024a271d2bf3ec4 Mon Sep 17 00:00:00 2001 From: Amy Regnier Date: Mon, 8 Jun 2020 09:50:40 -0500 Subject: [PATCH 1/3] Initial commit of code to accompany article on copying data from InputStream to OutputStream --- .../InputStreamToOutputStreamUnitTest.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java diff --git a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java new file mode 100644 index 0000000000..e41dbca586 --- /dev/null +++ b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java @@ -0,0 +1,85 @@ +package com.baeldung.java9.inputstream.outputstream; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.junit.Assert.assertEquals; + +import java.io.*; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +import com.google.common.io.ByteStreams; + +public class InputStreamToOutputStreamUnitTest { + + // buffer size used for reading and writing + private static final int BUFFER_SIZE = 8192; + + /** + * Reads all bytes from an input stream and writes them to an output stream. + * @param source - input stream to copy data from + * @param target - output stream to copy data too + */ + private static void copy(InputStream source, OutputStream target) throws IOException { + byte[] buf = new byte[BUFFER_SIZE]; + int length; + while ((length = source.read(buf)) > 0) { + target.write(buf, 0, length); + } + } + + @Test + public final void givenUsingJavaEight_whenConvertingStringToInputStream_thenCorrect() throws IOException { + final String initialString = "Hello World!"; + + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { + copy(inputStream, targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); + } + } + + @Test + public final void givenUsingJavaEight_whenConvertingVeryLongStringToInputStream_thenCorrect() throws IOException { + final String initialString = randomAlphabetic(20480); + + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { + copy(inputStream, targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); + } + } + + @Test + public final void givenUsingJavaNine_whenConvertingStringToInputStream_thenCorrect() throws IOException { + final String initialString = "Hello World!"; + + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { + inputStream.transferTo(targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); + } + } + + @Test + public final void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException { + final String initialString = "Hello World!"; + + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { + ByteStreams.copy(inputStream, targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); + } + } + + @Test + public final void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect() throws IOException { + final String initialString = "Hello World!"; + + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { + IOUtils.copy(inputStream, targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); + } + } +} From 87cc504e9e99e198d8aadc3bf1f1085021317f2e Mon Sep 17 00:00:00 2001 From: Amy Regnier Date: Wed, 17 Jun 2020 09:10:52 -0500 Subject: [PATCH 2/3] fix method names --- .../InputStreamToOutputStreamUnitTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java index e41dbca586..a6a32efe4a 100644 --- a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java +++ b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java @@ -29,7 +29,7 @@ public class InputStreamToOutputStreamUnitTest { } @Test - public final void givenUsingJavaEight_whenConvertingStringToInputStream_thenCorrect() throws IOException { + public final void givenUsingJavaEight_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { final String initialString = "Hello World!"; try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); @@ -40,7 +40,7 @@ public class InputStreamToOutputStreamUnitTest { } @Test - public final void givenUsingJavaEight_whenConvertingVeryLongStringToInputStream_thenCorrect() throws IOException { + public final void givenUsingJavaEight_whenCopyingLongInputStreamToOutputStream_thenCorrect() throws IOException { final String initialString = randomAlphabetic(20480); try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); @@ -51,7 +51,7 @@ public class InputStreamToOutputStreamUnitTest { } @Test - public final void givenUsingJavaNine_whenConvertingStringToInputStream_thenCorrect() throws IOException { + public final void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { final String initialString = "Hello World!"; try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); @@ -62,7 +62,7 @@ public class InputStreamToOutputStreamUnitTest { } @Test - public final void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException { + public final void givenUsingGuava_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { final String initialString = "Hello World!"; try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); @@ -73,7 +73,7 @@ public class InputStreamToOutputStreamUnitTest { } @Test - public final void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect() throws IOException { + public final void givenUsingCommonsIO_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { final String initialString = "Hello World!"; try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); From 0d66be29a7e92c5e5acca5759171cfcac3349f8e Mon Sep 17 00:00:00 2001 From: Amy Regnier Date: Mon, 22 Jun 2020 11:38:08 -0500 Subject: [PATCH 3/3] Updates to resolve issues raised by Kevin Gilmore --- .../InputStreamToOutputStreamUnitTest.java | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java index a6a32efe4a..814824e580 100644 --- a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java +++ b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java @@ -11,17 +11,14 @@ import org.junit.Test; import com.google.common.io.ByteStreams; public class InputStreamToOutputStreamUnitTest { - - // buffer size used for reading and writing - private static final int BUFFER_SIZE = 8192; - + /** * Reads all bytes from an input stream and writes them to an output stream. * @param source - input stream to copy data from * @param target - output stream to copy data too */ - private static void copy(InputStream source, OutputStream target) throws IOException { - byte[] buf = new byte[BUFFER_SIZE]; + void copy(InputStream source, OutputStream target) throws IOException { + byte[] buf = new byte[8192]; int length; while ((length = source.read(buf)) > 0) { target.write(buf, 0, length); @@ -29,56 +26,61 @@ public class InputStreamToOutputStreamUnitTest { } @Test - public final void givenUsingJavaEight_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { - final String initialString = "Hello World!"; + public void givenUsingJavaEight_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { + String initialString = "Hello World!"; try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { copy(inputStream, targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); } } @Test - public final void givenUsingJavaEight_whenCopyingLongInputStreamToOutputStream_thenCorrect() throws IOException { - final String initialString = randomAlphabetic(20480); + public void givenUsingJavaEight_whenCopyingLongInputStreamToOutputStream_thenCorrect() throws IOException { + String initialString = randomAlphabetic(20480); try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { copy(inputStream, targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); } } @Test - public final void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { - final String initialString = "Hello World!"; + public void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { + String initialString = "Hello World!"; try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { inputStream.transferTo(targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); } } @Test - public final void givenUsingGuava_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { - final String initialString = "Hello World!"; + public void givenUsingGuava_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { + String initialString = "Hello World!"; try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { ByteStreams.copy(inputStream, targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); } } @Test - public final void givenUsingCommonsIO_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { - final String initialString = "Hello World!"; + public void givenUsingCommonsIO_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException { + String initialString = "Hello World!"; try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { IOUtils.copy(inputStream, targetStream); + assertEquals(initialString, new String(targetStream.toByteArray())); } }