From af1636fe23675e77fbb914d11c4f745978ed1a4e Mon Sep 17 00:00:00 2001 From: "Erick Audet M.Sc" Date: Thu, 17 Jan 2019 12:21:30 -0500 Subject: [PATCH] BAEL-2509 * New section in InputStream to byte array article and recreating branch. * Minor typo * Code reformat using intellij formatter. * Code reformat using intellij formatter. * guava implementation to be completed * guava implementation * Added assert to guava test * Fix formatting * Formatting using Baeldung format * Based on Josh comments, I removed some code BAEL-2509 * Removed all references to File * Update fork from upstream * Update fork from upstream * Merhe Upstream * Remove all references to FileInputStream (Josh Comments) * Delete CustomBaeldungQueueUnitTest.java --- .../io/InputStreamToByteBufferUnitTest.java | 64 +++++++++---------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/core-java-io/src/test/java/org/baeldung/java/io/InputStreamToByteBufferUnitTest.java b/core-java-io/src/test/java/org/baeldung/java/io/InputStreamToByteBufferUnitTest.java index fedadde04b..37f52fefea 100644 --- a/core-java-io/src/test/java/org/baeldung/java/io/InputStreamToByteBufferUnitTest.java +++ b/core-java-io/src/test/java/org/baeldung/java/io/InputStreamToByteBufferUnitTest.java @@ -1,60 +1,56 @@ package org.baeldung.java.io; - +import com.google.common.io.ByteSource; import com.google.common.io.ByteStreams; import org.apache.commons.io.IOUtils; -import org.junit.jupiter.api.Test; +import org.junit.Test; -import java.io.File; -import java.io.FileInputStream; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static java.nio.channels.Channels.newChannel; +import static org.junit.Assert.assertEquals; -class InputStreamToByteBufferUnitTest { +public class InputStreamToByteBufferUnitTest { @Test - public void givenUsingCoreClasses_whenWritingAFileIntoAByteBuffer_thenBytesLengthMustMatch() throws IOException { - File inputFile = getFile(); - ByteBuffer bufferByte = ByteBuffer.allocate((int) inputFile.length()); - FileInputStream in = new FileInputStream(inputFile); - in.getChannel().read(bufferByte); + public void givenUsingCoreClasses_whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException { + byte[] input = new byte[] { 0, 1, 2 }; + InputStream initialStream = new ByteArrayInputStream(input); + ByteBuffer byteBuffer = ByteBuffer.allocate(3); + while (initialStream.available() > 0) { + byteBuffer.put((byte) initialStream.read()); + } - assertEquals(bufferByte.position(), inputFile.length()); + assertEquals(byteBuffer.position(), input.length); } @Test - public void givenUsingCommonsIo_whenWritingAFileIntoAByteBuffer_thenBytesLengthMustMatch() throws IOException { - File inputFile = getFile(); - ByteBuffer bufferByte = ByteBuffer.allocateDirect((int) inputFile.length()); - ReadableByteChannel readableByteChannel = new FileInputStream(inputFile).getChannel(); - IOUtils.readFully(readableByteChannel, bufferByte); - - assertEquals(bufferByte.position(), inputFile.length()); - } - - @Test - public void givenUsingGuava_whenWritingAFileIntoAByteBuffer_thenBytesLengthMustMatch() throws IOException { - File inputFile = getFile(); - FileInputStream in = new FileInputStream(inputFile); - byte[] targetArray = ByteStreams.toByteArray(in); + public void givenUsingGuava__whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException { + InputStream initialStream = ByteSource + .wrap(new byte[] { 0, 1, 2 }) + .openStream(); + byte[] targetArray = ByteStreams.toByteArray(initialStream); ByteBuffer bufferByte = ByteBuffer.wrap(targetArray); - bufferByte.rewind(); while (bufferByte.hasRemaining()) { bufferByte.get(); } - - assertEquals(bufferByte.position(), inputFile.length()); + + assertEquals(bufferByte.position(), targetArray.length); } - private File getFile() { - ClassLoader classLoader = new InputStreamToByteBufferUnitTest().getClass().getClassLoader(); + @Test + public void givenUsingCommonsIo_whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException { + byte[] input = new byte[] { 0, 1, 2 }; + InputStream initialStream = new ByteArrayInputStream(input); + ByteBuffer byteBuffer = ByteBuffer.allocate(3); + ReadableByteChannel channel = newChannel(initialStream); + IOUtils.readFully(channel, byteBuffer); - String fileName = "frontenac-2257154_960_720.jpg"; - - return new File(classLoader.getResource(fileName).getFile()); + assertEquals(byteBuffer.position(), input.length); } }