Skip to content

Commit 4d7841f

Browse files
author
lsieun
authored
BAEL-4151: Guide to ByteBuffer (eugenp#11859)
* Convert Byte Array to its Numeric Representation * Remove Redundant Getter Method * BAEL-4286 How to get the value of a bit at a certain position from a byte * BAEL-4286(update): remove redundant test methods * BAEL-4151: Guide to ByteBuffer
1 parent c9411a6 commit 4d7841f

1 file changed

Lines changed: 339 additions & 0 deletions

File tree

Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
package com.baeldung.bytebuffer;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.Field;
6+
import java.nio.*;
7+
import java.nio.charset.StandardCharsets;
8+
9+
import static org.junit.Assert.*;
10+
11+
public class ByteBufferUnitTest {
12+
@Test
13+
public void givenBufferCreation_whenUsingAllocate_thenSuccess() {
14+
ByteBuffer buffer = ByteBuffer.allocate(10);
15+
assertNotNull(buffer);
16+
}
17+
18+
@Test
19+
public void givenBufferCreation_whenUsingAllocateDirect_thenSuccess() {
20+
ByteBuffer buffer = ByteBuffer.allocateDirect(10);
21+
assertNotNull(buffer);
22+
}
23+
24+
@Test
25+
public void givenBufferCreation_whenUsingWrap_thenSuccess() {
26+
byte[] bytes = new byte[10];
27+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
28+
assertNotNull(buffer);
29+
}
30+
31+
@Test
32+
public void givenBufferIndex_whenUsingAllocate_thenInitialIndices() {
33+
// create instance using allocate
34+
ByteBuffer buffer = ByteBuffer.allocate(10);
35+
36+
// get index
37+
int position = buffer.position();
38+
int limit = buffer.limit();
39+
int capacity = buffer.capacity();
40+
41+
// assert
42+
assertEquals(0, position);
43+
assertEquals(10, limit);
44+
assertEquals(10, capacity);
45+
}
46+
47+
@Test
48+
public void givenBufferIndex_whenChangingPositionAndLimit_thenSuccess() {
49+
// create instance
50+
ByteBuffer buffer = ByteBuffer.allocate(10);
51+
52+
// change index
53+
buffer.position(2);
54+
buffer.limit(5);
55+
56+
// assert
57+
assertIndex(buffer, -1, 2, 5, 10);
58+
}
59+
60+
@Test
61+
public void givenBufferIndex_whenUsingWrap_thenInitialIndices() {
62+
// create instance
63+
byte[] bytes = new byte[10];
64+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
65+
66+
// assert
67+
assertIndex(buffer, -1, 0, 10, 10);
68+
}
69+
70+
@Test
71+
public void givenBufferIndex_whenUsingWrapWithOffsetAndLength_thenInitialIndices() {
72+
// create instance
73+
byte[] bytes = new byte[10];
74+
ByteBuffer buffer = ByteBuffer.wrap(bytes, 2, 6);
75+
76+
// assert
77+
assertIndex(buffer, -1, 2, 8, 10);
78+
}
79+
80+
@Test
81+
public void givenBufferIndex_whenUsingMarkAndReset_thenOK() {
82+
ByteBuffer buffer = ByteBuffer.allocate(10);
83+
assertIndex(buffer, -1, 0, 10, 10);
84+
85+
buffer.position(2);
86+
assertIndex(buffer, -1, 2, 10, 10);
87+
88+
buffer.mark();
89+
assertIndex(buffer, 2, 2, 10, 10);
90+
91+
buffer.position(5);
92+
assertIndex(buffer, 2, 5, 10, 10);
93+
94+
buffer.reset();
95+
assertIndex(buffer, 2, 2, 10, 10);
96+
}
97+
98+
@Test
99+
public void givenBufferIndex_whenUsingClear_thenOK() {
100+
// create instance
101+
ByteBuffer buffer = ByteBuffer.allocate(10);
102+
assertIndex(buffer, -1, 0, 10, 10);
103+
104+
// change index
105+
buffer.position(2);
106+
buffer.mark();
107+
buffer.position(5);
108+
buffer.limit(8);
109+
assertIndex(buffer, 2, 5, 8, 10);
110+
111+
// clear
112+
buffer.clear();
113+
assertIndex(buffer, -1, 0, 10, 10);
114+
}
115+
116+
@Test
117+
public void givenBufferIndex_whenUsingFlip_thenOK() {
118+
// create instance
119+
ByteBuffer buffer = ByteBuffer.allocate(10);
120+
assertIndex(buffer, -1, 0, 10, 10);
121+
122+
// change index
123+
buffer.position(2);
124+
buffer.mark();
125+
buffer.position(5);
126+
buffer.limit(8);
127+
assertIndex(buffer, 2, 5, 8, 10);
128+
129+
// flip
130+
buffer.flip();
131+
assertIndex(buffer, -1, 0, 5, 10);
132+
}
133+
134+
@Test
135+
public void givenBufferIndex_whenUsingRewind_thenOK() {
136+
// create instance
137+
ByteBuffer buffer = ByteBuffer.allocate(10);
138+
assertIndex(buffer, -1, 0, 10, 10);
139+
140+
// change index
141+
buffer.position(2);
142+
buffer.mark();
143+
buffer.position(5);
144+
buffer.limit(8);
145+
assertIndex(buffer, 2, 5, 8, 10);
146+
147+
// rewind
148+
buffer.rewind();
149+
assertIndex(buffer, -1, 0, 8, 10);
150+
}
151+
152+
@Test
153+
public void givenBufferIndex_whenUsingCompact_thenOK() {
154+
// create instance
155+
ByteBuffer buffer = ByteBuffer.allocate(10);
156+
assertIndex(buffer, -1, 0, 10, 10);
157+
158+
// change index
159+
buffer.position(2);
160+
buffer.mark();
161+
buffer.position(5);
162+
buffer.limit(8);
163+
assertIndex(buffer, 2, 5, 8, 10);
164+
165+
// compact
166+
buffer.compact();
167+
assertIndex(buffer, -1, 3, 10, 10);
168+
}
169+
170+
@Test
171+
public void givenBufferIndex_whenUsingRemain_thenOK() {
172+
// create instance
173+
ByteBuffer buffer = ByteBuffer.allocate(10);
174+
175+
// change index
176+
buffer.position(2);
177+
buffer.limit(8);
178+
179+
// remain
180+
boolean flag = buffer.hasRemaining();
181+
int remaining = buffer.remaining();
182+
183+
// assert
184+
assertTrue(flag);
185+
assertEquals(6, remaining);
186+
}
187+
188+
@Test(expected = BufferUnderflowException.class)
189+
public void givenNotEnoughRemaining_WhenCallingGetInt_thenBufferUnderflowException() {
190+
ByteBuffer buffer = ByteBuffer.allocate(2);
191+
buffer.getInt();
192+
}
193+
194+
@Test(expected = BufferOverflowException.class)
195+
public void givenNotEnoughRemaining_WhenCallingPutInt_thenBufferOverflowException() {
196+
ByteBuffer buffer = ByteBuffer.allocate(2);
197+
buffer.putInt(10);
198+
}
199+
200+
@Test
201+
public void givenBufferView_whenUsingDuplicate_thenOK() {
202+
// create instance
203+
ByteBuffer buffer = ByteBuffer.allocate(10);
204+
assertIndex(buffer, -1, 0, 10, 10);
205+
206+
// change index
207+
buffer.position(2);
208+
buffer.mark();
209+
buffer.position(5);
210+
buffer.limit(8);
211+
assertIndex(buffer, 2, 5, 8, 10);
212+
213+
// view
214+
ByteBuffer view = buffer.duplicate();
215+
assertIndex(view, 2, 5, 8, 10);
216+
}
217+
218+
@Test
219+
public void givenBufferView_whenUsingSlice_thenOK() {
220+
// create instance
221+
ByteBuffer buffer = ByteBuffer.allocate(10);
222+
assertIndex(buffer, -1, 0, 10, 10);
223+
224+
// change index
225+
buffer.position(2);
226+
buffer.mark();
227+
buffer.position(5);
228+
buffer.limit(8);
229+
assertIndex(buffer, 2, 5, 8, 10);
230+
231+
// view
232+
ByteBuffer view = buffer.slice();
233+
assertIndex(view, -1, 0, 3, 3);
234+
}
235+
236+
@Test
237+
public void givenBufferView_whenUsingAsReaOnlyBuffer_thenOK() {
238+
// create instance
239+
ByteBuffer buffer = ByteBuffer.allocate(10);
240+
assertIndex(buffer, -1, 0, 10, 10);
241+
242+
// change index
243+
buffer.position(2);
244+
buffer.mark();
245+
buffer.position(5);
246+
buffer.limit(8);
247+
assertIndex(buffer, 2, 5, 8, 10);
248+
249+
// view
250+
ByteBuffer view = buffer.asReadOnlyBuffer();
251+
assertIndex(view, 2, 5, 8, 10);
252+
}
253+
254+
@Test
255+
public void givenBufferView_whenUsingAsIntBuffer_thenOK() {
256+
// create instance
257+
byte[] bytes = new byte[]{
258+
(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE, // CAFEBABE ---> cafebabe
259+
(byte) 0xF0, (byte) 0x07, (byte) 0xBA, (byte) 0x11, // F007BA11 ---> football
260+
(byte) 0x0F, (byte) 0xF1, (byte) 0xCE // 0FF1CE ---> office
261+
};
262+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
263+
assertIndex(buffer, -1, 0, 11, 11);
264+
265+
// view
266+
IntBuffer intBuffer = buffer.asIntBuffer();
267+
int capacity = intBuffer.capacity();
268+
assertEquals(2, capacity);
269+
assertIndex(intBuffer, -1, 0, 2, 2);
270+
}
271+
272+
@Test
273+
public void givenByteOrder_whenUsingBigEndian_thenOK() {
274+
// create instance
275+
byte[] bytes = new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
276+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
277+
278+
// change byte order
279+
buffer.order(ByteOrder.BIG_ENDIAN);
280+
int val = buffer.getInt();
281+
282+
// assert
283+
assertEquals(0xCAFEBABE, val);
284+
}
285+
286+
@Test
287+
public void givenByteOrder_whenUsingLittleEndian_thenOK() {
288+
// create instance
289+
byte[] bytes = new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
290+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
291+
292+
// change byte order
293+
buffer.order(ByteOrder.LITTLE_ENDIAN);
294+
int val = buffer.getInt();
295+
296+
// assert
297+
assertEquals(0xBEBAFECA, val);
298+
}
299+
300+
@Test
301+
public void givenComparing_whenUsingEqualsAndCompareTo_thenOK() {
302+
// create instance
303+
byte[] bytes1 = "World".getBytes(StandardCharsets.UTF_8);
304+
byte[] bytes2 = "HelloWorld".getBytes(StandardCharsets.UTF_8);
305+
ByteBuffer buffer1 = ByteBuffer.wrap(bytes1);
306+
ByteBuffer buffer2 = ByteBuffer.wrap(bytes2);
307+
308+
// change index
309+
buffer2.position(5);
310+
311+
// equals and compareTo
312+
boolean equal = buffer1.equals(buffer2);
313+
int result = buffer1.compareTo(buffer2);
314+
315+
// assert
316+
assertTrue(equal);
317+
assertEquals(0, result);
318+
}
319+
320+
private void assertIndex(Buffer buffer, int mark, int position, int limit, int capacity) {
321+
assertEquals(mark, getMark(buffer));
322+
assertEquals(position, buffer.position());
323+
assertEquals(limit, buffer.limit());
324+
assertEquals(capacity, buffer.capacity());
325+
}
326+
327+
private int getMark(Buffer buffer) {
328+
try {
329+
Class<?> clazz = Buffer.class;
330+
Field f = clazz.getDeclaredField("mark");
331+
f.setAccessible(true);
332+
Object result = f.get(buffer);
333+
return (int) result;
334+
} catch (NoSuchFieldException | IllegalAccessException e) {
335+
e.printStackTrace();
336+
}
337+
return -1;
338+
}
339+
}

0 commit comments

Comments
 (0)