Skip to content

Commit c8b3c71

Browse files
committed
BAEL-8276: Convert Between ByteBuffer and Byte Array in Java
1 parent 6c75a16 commit c8b3c71

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.bytebuffer;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
import java.nio.BufferUnderflowException;
8+
import java.nio.ByteBuffer;
9+
import java.nio.ReadOnlyBufferException;
10+
11+
import org.junit.jupiter.api.Test;
12+
13+
class ByteBufferToByteArrayConversionUnitTest {
14+
15+
@Test
16+
void givenByteBuffer_whenUsingArrayMethod_thenConvertToByteArray() {
17+
byte[] givenBytes = {1, 6, 3};
18+
ByteBuffer buffer = ByteBuffer.wrap(givenBytes);
19+
byte[] bytes = buffer.array();
20+
21+
assertArrayEquals(givenBytes, bytes);
22+
}
23+
24+
@Test
25+
void givenBufferWithoutBackingArray_whenCallingArray_thenThrowUnsupportedOperationException() {
26+
ByteBuffer buffer = ByteBuffer.allocateDirect(4);
27+
28+
assertThrows(UnsupportedOperationException.class, buffer::array);
29+
}
30+
31+
@Test
32+
void givenReadOnlyBuffer_whenCallingArray_thenThrowReadOnlyBufferException() {
33+
ByteBuffer buffer = ByteBuffer.wrap(new byte[] {1, 2, 3});
34+
ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
35+
36+
assertThrows(ReadOnlyBufferException.class, readOnlyBuffer::array);
37+
}
38+
39+
@Test
40+
void givenByteBuffer_whenUsingGetMethod_thenConvertToByteArray() {
41+
byte[] givenBytes = {5, 4, 2};
42+
ByteBuffer buffer = ByteBuffer.wrap(givenBytes);
43+
byte[] bytes = new byte[buffer.remaining()];
44+
buffer.get(bytes);
45+
46+
assertArrayEquals(givenBytes, bytes);
47+
}
48+
49+
@Test
50+
void givenByteBuffer_whenUsingGetWithParamsMethod_thenConvertToByteArray() {
51+
byte[] givenBytes = {5, 4, 2, 7};
52+
ByteBuffer buffer = ByteBuffer.wrap(givenBytes);
53+
byte[] bytes = new byte[2];
54+
buffer.get(bytes, 0, 2);
55+
56+
assertArrayEquals(new byte[] {5, 4}, bytes);
57+
}
58+
59+
@Test
60+
void givenByteArray_whenUsingWrapMethod_thenConvertToByteBuffer() {
61+
byte[] givenBytes = {1, 2, 3};
62+
ByteBuffer buffer = ByteBuffer.wrap(givenBytes);
63+
64+
assertArrayEquals(givenBytes, buffer.array());
65+
}
66+
67+
@Test
68+
void givenByteArray_whenUsingWrapWithParamsMethod_thenConvertToByteBuffer() {
69+
byte[] givenBytes = {1, 2, 3, 7, 8};
70+
ByteBuffer buffer = ByteBuffer.wrap(givenBytes, 1, 2);
71+
72+
byte[] actualBytes = new byte[buffer.remaining()];
73+
buffer.get(actualBytes);
74+
75+
assertArrayEquals(new byte[] {2, 3}, actualBytes);
76+
}
77+
78+
@Test
79+
void givenByteArray_whenUsingAllocateAndPutMethods_thenConvertToByteBuffer() {
80+
byte[] givenBytes = {1, 9, 7};
81+
ByteBuffer buffer = ByteBuffer.allocate(givenBytes.length);
82+
buffer.put(givenBytes);
83+
84+
assertArrayEquals(givenBytes, buffer.array());
85+
}
86+
87+
@Test
88+
void givenByteArray_whenUsingPutWithFlip_thenCorrect() {
89+
byte[] givenBytes = {3, 8};
90+
ByteBuffer buffer = ByteBuffer.allocate(givenBytes.length);
91+
buffer.put(givenBytes);
92+
buffer.flip();
93+
94+
assertEquals(3, buffer.get());
95+
}
96+
97+
@Test
98+
void givenByteArray_whenUsingPutWithoutFlip_thenThrowBufferUnderflowException() {
99+
byte[] givenBytes = {1, 5};
100+
ByteBuffer buffer = ByteBuffer.allocate(givenBytes.length);
101+
buffer.put(givenBytes);
102+
103+
assertThrows(BufferUnderflowException.class, buffer::get);
104+
}
105+
}

0 commit comments

Comments
 (0)