Skip to content

Commit 556f2da

Browse files
committed
ByteBufferUtils + JUnit Test
1 parent 17924f8 commit 556f2da

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.java_websocket.util;
2+
3+
import java.nio.ByteBuffer;
4+
5+
/**
6+
* Utility class for ByteBuffers
7+
*/
8+
public class ByteBufferUtils {
9+
10+
/**
11+
* Private constructor for static class
12+
*/
13+
private ByteBufferUtils() {
14+
}
15+
16+
/**
17+
* Transfer from one ByteBuffer to another ByteBuffer
18+
*
19+
* @param source the ByteBuffer to copy from
20+
* @param dest the ByteBuffer to copy to
21+
*/
22+
public static void transferByteBuffer( ByteBuffer source, ByteBuffer dest ) {
23+
if( source == null || dest == null ) {
24+
throw new IllegalArgumentException();
25+
}
26+
int fremain = source.remaining();
27+
int toremain = dest.remaining();
28+
if( fremain > toremain ) {
29+
source.limit( Math.min( fremain, toremain ) );
30+
dest.put( source );
31+
} else {
32+
dest.put( source );
33+
}
34+
35+
}
36+
37+
/**
38+
* Get a ByteBuffer with zero capacity
39+
*
40+
* @return empty ByteBuffer
41+
*/
42+
public static ByteBuffer getEmptyByteBuffer() {
43+
return ByteBuffer.allocate( 0 );
44+
}
45+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.java_websocket.util;
2+
3+
import org.java_websocket.util.ByteBufferUtils;
4+
import org.junit.Test;
5+
6+
import java.nio.ByteBuffer;
7+
8+
import static org.junit.Assert.*;
9+
10+
/**
11+
* JUnit Test for the new ByteBufferUtils class
12+
*/
13+
public class ByteBufferUtilsTest {
14+
15+
private static byte[] smallArray = { 0, -1, -2, -3, -4 };
16+
private static byte[] bigArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
17+
18+
@Test
19+
public void testEmptyByteBufferCapacity() {
20+
ByteBuffer byteBuffer = ByteBufferUtils.getEmptyByteBuffer();
21+
assertEquals( "capacity must be 0", byteBuffer.capacity(), 0 );
22+
}
23+
24+
@Test
25+
public void testEmptyByteBufferLimit() {
26+
ByteBuffer byteBuffer = ByteBufferUtils.getEmptyByteBuffer();
27+
assertEquals( "limit must be 0", byteBuffer.limit(), 0 );
28+
}
29+
30+
@Test
31+
public void testEmptyByteBufferNewObject() {
32+
ByteBuffer byteBuffer0 = ByteBufferUtils.getEmptyByteBuffer();
33+
ByteBuffer byteBuffer1 = ByteBufferUtils.getEmptyByteBuffer();
34+
assertTrue( "Allocated new object", byteBuffer0 != byteBuffer1 );
35+
}
36+
37+
@Test
38+
public void testTransferByteBufferSmallToEmpty() {
39+
ByteBuffer small = ByteBuffer.wrap( smallArray );
40+
ByteBuffer empty = ByteBufferUtils.getEmptyByteBuffer();
41+
ByteBufferUtils.transferByteBuffer( small, empty );
42+
assertArrayEquals( "Small bytebuffer should not change", small.array(), smallArray );
43+
assertEquals( "capacity of the empty bytebuffer should still be 0", empty.capacity(), 0 );
44+
}
45+
46+
@Test
47+
public void testTransferByteBufferSmallToBig() {
48+
ByteBuffer small = ByteBuffer.wrap( smallArray );
49+
ByteBuffer big = ByteBuffer.wrap( bigArray );
50+
ByteBufferUtils.transferByteBuffer( small, big );
51+
assertArrayEquals( "Small bytebuffer should not change", small.array(), smallArray );
52+
assertEquals( big.get( 0 ), smallArray[0] );
53+
assertEquals( big.get( 1 ), smallArray[1] );
54+
assertEquals( big.get( 2 ), smallArray[2] );
55+
assertEquals( big.get( 3 ), smallArray[3] );
56+
assertEquals( big.get( 4 ), smallArray[4] );
57+
assertEquals( big.get( 5 ), bigArray[5] );
58+
assertEquals( big.get( 6 ), bigArray[6] );
59+
assertEquals( big.get( 7 ), bigArray[7] );
60+
assertEquals( big.get( 8 ), bigArray[8] );
61+
}
62+
63+
@Test
64+
public void testTransferByteBufferBigToSmall() {
65+
ByteBuffer small = ByteBuffer.wrap( smallArray );
66+
ByteBuffer big = ByteBuffer.wrap( bigArray );
67+
ByteBufferUtils.transferByteBuffer( big, small );
68+
assert ( small.get( 0 ) == bigArray[0] );
69+
assert ( small.get( 1 ) == bigArray[1] );
70+
assert ( small.get( 2 ) == bigArray[2] );
71+
assert ( small.get( 3 ) == bigArray[3] );
72+
assert ( small.get( 4 ) == bigArray[4] );
73+
assert ( big.array() == bigArray );
74+
}
75+
76+
@Test
77+
public void testTransferByteBufferCheckNull() {
78+
ByteBuffer source = ByteBufferUtils.getEmptyByteBuffer();
79+
ByteBuffer dest = ByteBufferUtils.getEmptyByteBuffer();
80+
ByteBufferUtils.transferByteBuffer( source, null );
81+
}
82+
}

0 commit comments

Comments
 (0)