|
1 | 1 | package org.baeldung.java.io; |
2 | 2 |
|
3 | | - |
| 3 | +import com.google.common.io.ByteSource; |
4 | 4 | import com.google.common.io.ByteStreams; |
5 | 5 | import org.apache.commons.io.IOUtils; |
6 | | -import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.Test; |
7 | 7 |
|
8 | | -import java.io.File; |
9 | | -import java.io.FileInputStream; |
| 8 | +import java.io.ByteArrayInputStream; |
10 | 9 | import java.io.IOException; |
| 10 | +import java.io.InputStream; |
11 | 11 | import java.nio.ByteBuffer; |
12 | 12 | import java.nio.channels.ReadableByteChannel; |
13 | 13 |
|
14 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
15 | | - |
16 | | -class InputStreamToByteBufferUnitTest { |
| 14 | +import static java.nio.channels.Channels.newChannel; |
| 15 | +import static org.junit.Assert.assertEquals; |
17 | 16 |
|
18 | | - @Test |
19 | | - public void givenUsingCoreClasses_whenWritingAFileIntoAByteBuffer_thenBytesLengthMustMatch() throws IOException { |
20 | | - File inputFile = getFile(); |
21 | | - ByteBuffer bufferByte = ByteBuffer.allocate((int) inputFile.length()); |
22 | | - FileInputStream in = new FileInputStream(inputFile); |
23 | | - in.getChannel().read(bufferByte); |
24 | | - |
25 | | - assertEquals(bufferByte.position(), inputFile.length()); |
26 | | - } |
| 17 | +public class InputStreamToByteBufferUnitTest { |
27 | 18 |
|
28 | 19 | @Test |
29 | | - public void givenUsingCommonsIo_whenWritingAFileIntoAByteBuffer_thenBytesLengthMustMatch() throws IOException { |
30 | | - File inputFile = getFile(); |
31 | | - ByteBuffer bufferByte = ByteBuffer.allocateDirect((int) inputFile.length()); |
32 | | - ReadableByteChannel readableByteChannel = new FileInputStream(inputFile).getChannel(); |
33 | | - IOUtils.readFully(readableByteChannel, bufferByte); |
| 20 | + public void givenUsingCoreClasses_whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException { |
| 21 | + byte[] input = new byte[] { 0, 1, 2 }; |
| 22 | + InputStream initialStream = new ByteArrayInputStream(input); |
| 23 | + ByteBuffer byteBuffer = ByteBuffer.allocate(3); |
| 24 | + while (initialStream.available() > 0) { |
| 25 | + byteBuffer.put((byte) initialStream.read()); |
| 26 | + } |
34 | 27 |
|
35 | | - assertEquals(bufferByte.position(), inputFile.length()); |
| 28 | + assertEquals(byteBuffer.position(), input.length); |
36 | 29 | } |
37 | 30 |
|
38 | 31 | @Test |
39 | | - public void givenUsingGuava_whenWritingAFileIntoAByteBuffer_thenBytesLengthMustMatch() throws IOException { |
40 | | - File inputFile = getFile(); |
41 | | - FileInputStream in = new FileInputStream(inputFile); |
42 | | - byte[] targetArray = ByteStreams.toByteArray(in); |
| 32 | + public void givenUsingGuava__whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException { |
| 33 | + InputStream initialStream = ByteSource |
| 34 | + .wrap(new byte[] { 0, 1, 2 }) |
| 35 | + .openStream(); |
| 36 | + byte[] targetArray = ByteStreams.toByteArray(initialStream); |
43 | 37 | ByteBuffer bufferByte = ByteBuffer.wrap(targetArray); |
44 | | - bufferByte.rewind(); |
45 | 38 | while (bufferByte.hasRemaining()) { |
46 | 39 | bufferByte.get(); |
47 | 40 | } |
48 | | - |
49 | | - assertEquals(bufferByte.position(), inputFile.length()); |
50 | | - } |
51 | | - |
52 | | - private File getFile() { |
53 | | - ClassLoader classLoader = new InputStreamToByteBufferUnitTest().getClass().getClassLoader(); |
54 | 41 |
|
55 | | - String fileName = "frontenac-2257154_960_720.jpg"; |
| 42 | + assertEquals(bufferByte.position(), targetArray.length); |
| 43 | + } |
56 | 44 |
|
57 | | - return new File(classLoader.getResource(fileName).getFile()); |
| 45 | + @Test |
| 46 | + public void givenUsingCommonsIo_whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException { |
| 47 | + byte[] input = new byte[] { 0, 1, 2 }; |
| 48 | + InputStream initialStream = new ByteArrayInputStream(input); |
| 49 | + ByteBuffer byteBuffer = ByteBuffer.allocate(3); |
| 50 | + ReadableByteChannel channel = newChannel(initialStream); |
| 51 | + IOUtils.readFully(channel, byteBuffer); |
| 52 | + |
| 53 | + assertEquals(byteBuffer.position(), input.length); |
58 | 54 | } |
59 | 55 |
|
60 | 56 | } |
0 commit comments