Skip to content

Commit af1636f

Browse files
eaudetecheyne
authored andcommitted
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
1 parent 212ea5d commit af1636f

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed
Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,56 @@
11
package org.baeldung.java.io;
22

3-
3+
import com.google.common.io.ByteSource;
44
import com.google.common.io.ByteStreams;
55
import org.apache.commons.io.IOUtils;
6-
import org.junit.jupiter.api.Test;
6+
import org.junit.Test;
77

8-
import java.io.File;
9-
import java.io.FileInputStream;
8+
import java.io.ByteArrayInputStream;
109
import java.io.IOException;
10+
import java.io.InputStream;
1111
import java.nio.ByteBuffer;
1212
import java.nio.channels.ReadableByteChannel;
1313

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;
1716

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 {
2718

2819
@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+
}
3427

35-
assertEquals(bufferByte.position(), inputFile.length());
28+
assertEquals(byteBuffer.position(), input.length);
3629
}
3730

3831
@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);
4337
ByteBuffer bufferByte = ByteBuffer.wrap(targetArray);
44-
bufferByte.rewind();
4538
while (bufferByte.hasRemaining()) {
4639
bufferByte.get();
4740
}
48-
49-
assertEquals(bufferByte.position(), inputFile.length());
50-
}
51-
52-
private File getFile() {
53-
ClassLoader classLoader = new InputStreamToByteBufferUnitTest().getClass().getClassLoader();
5441

55-
String fileName = "frontenac-2257154_960_720.jpg";
42+
assertEquals(bufferByte.position(), targetArray.length);
43+
}
5644

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);
5854
}
5955

6056
}

0 commit comments

Comments
 (0)