|
| 1 | +package com.baeldung.java9.inputstream.outputstream; |
| 2 | + |
| 3 | +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | + |
| 8 | +import org.apache.commons.io.IOUtils; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.google.common.io.ByteStreams; |
| 12 | + |
| 13 | +public class InputStreamToOutputStreamUnitTest { |
| 14 | + |
| 15 | + // buffer size used for reading and writing |
| 16 | + private static final int BUFFER_SIZE = 8192; |
| 17 | + |
| 18 | + /** |
| 19 | + * Reads all bytes from an input stream and writes them to an output stream. |
| 20 | + * @param source - input stream to copy data from |
| 21 | + * @param target - output stream to copy data too |
| 22 | + */ |
| 23 | + private static void copy(InputStream source, OutputStream target) throws IOException { |
| 24 | + byte[] buf = new byte[BUFFER_SIZE]; |
| 25 | + int length; |
| 26 | + while ((length = source.read(buf)) > 0) { |
| 27 | + target.write(buf, 0, length); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public final void givenUsingJavaEight_whenConvertingStringToInputStream_thenCorrect() throws IOException { |
| 33 | + final String initialString = "Hello World!"; |
| 34 | + |
| 35 | + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
| 36 | + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
| 37 | + copy(inputStream, targetStream); |
| 38 | + assertEquals(initialString, new String(targetStream.toByteArray())); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public final void givenUsingJavaEight_whenConvertingVeryLongStringToInputStream_thenCorrect() throws IOException { |
| 44 | + final String initialString = randomAlphabetic(20480); |
| 45 | + |
| 46 | + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
| 47 | + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
| 48 | + copy(inputStream, targetStream); |
| 49 | + assertEquals(initialString, new String(targetStream.toByteArray())); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public final void givenUsingJavaNine_whenConvertingStringToInputStream_thenCorrect() throws IOException { |
| 55 | + final String initialString = "Hello World!"; |
| 56 | + |
| 57 | + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
| 58 | + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
| 59 | + inputStream.transferTo(targetStream); |
| 60 | + assertEquals(initialString, new String(targetStream.toByteArray())); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public final void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException { |
| 66 | + final String initialString = "Hello World!"; |
| 67 | + |
| 68 | + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
| 69 | + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
| 70 | + ByteStreams.copy(inputStream, targetStream); |
| 71 | + assertEquals(initialString, new String(targetStream.toByteArray())); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public final void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect() throws IOException { |
| 77 | + final String initialString = "Hello World!"; |
| 78 | + |
| 79 | + try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes()); |
| 80 | + ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) { |
| 81 | + IOUtils.copy(inputStream, targetStream); |
| 82 | + assertEquals(initialString, new String(targetStream.toByteArray())); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments