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
This commit is contained in:
Erick Audet M.Sc 2019-01-17 12:21:30 -05:00 committed by Emily Cheyne
parent 212ea5d83b
commit af1636fe23

View File

@ -1,60 +1,56 @@
package org.baeldung.java.io; package org.baeldung.java.io;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test; import org.junit.Test;
import java.io.File; import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel; 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 @Test
public void givenUsingCoreClasses_whenWritingAFileIntoAByteBuffer_thenBytesLengthMustMatch() throws IOException { public void givenUsingCoreClasses_whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException {
File inputFile = getFile(); byte[] input = new byte[] { 0, 1, 2 };
ByteBuffer bufferByte = ByteBuffer.allocate((int) inputFile.length()); InputStream initialStream = new ByteArrayInputStream(input);
FileInputStream in = new FileInputStream(inputFile); ByteBuffer byteBuffer = ByteBuffer.allocate(3);
in.getChannel().read(bufferByte); while (initialStream.available() > 0) {
byteBuffer.put((byte) initialStream.read());
}
assertEquals(bufferByte.position(), inputFile.length()); assertEquals(byteBuffer.position(), input.length);
} }
@Test @Test
public void givenUsingCommonsIo_whenWritingAFileIntoAByteBuffer_thenBytesLengthMustMatch() throws IOException { public void givenUsingGuava__whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException {
File inputFile = getFile(); InputStream initialStream = ByteSource
ByteBuffer bufferByte = ByteBuffer.allocateDirect((int) inputFile.length()); .wrap(new byte[] { 0, 1, 2 })
ReadableByteChannel readableByteChannel = new FileInputStream(inputFile).getChannel(); .openStream();
IOUtils.readFully(readableByteChannel, bufferByte); byte[] targetArray = ByteStreams.toByteArray(initialStream);
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);
ByteBuffer bufferByte = ByteBuffer.wrap(targetArray); ByteBuffer bufferByte = ByteBuffer.wrap(targetArray);
bufferByte.rewind();
while (bufferByte.hasRemaining()) { while (bufferByte.hasRemaining()) {
bufferByte.get(); bufferByte.get();
} }
assertEquals(bufferByte.position(), inputFile.length()); assertEquals(bufferByte.position(), targetArray.length);
} }
private File getFile() { @Test
ClassLoader classLoader = new InputStreamToByteBufferUnitTest().getClass().getClassLoader(); 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"; assertEquals(byteBuffer.position(), input.length);
return new File(classLoader.getResource(fileName).getFile());
} }
} }