Skip to content

Commit bcda39d

Browse files
committed
Add ByteBuffersTest
Javadoc
1 parent 662ec39 commit bcda39d

2 files changed

Lines changed: 171 additions & 1 deletion

File tree

src/main/java/org/apache/commons/io/ByteBuffers.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ public static ByteBuffer littleEndian(final int capacity) {
6868
return littleEndian(ByteBuffer.allocate(capacity));
6969
}
7070

71+
/**
72+
* No instances.
73+
*/
7174
private ByteBuffers() {
72-
// empty, no instance.
75+
// empty.
7376
}
7477

7578
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io;
19+
20+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.junit.jupiter.api.Assertions.assertSame;
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
26+
import java.nio.ByteBuffer;
27+
import java.nio.ByteOrder;
28+
29+
import org.junit.jupiter.api.Test;
30+
31+
/**
32+
* Tests {@link ByteBuffers}.
33+
*/
34+
public class ByteBuffersTest {
35+
36+
/**
37+
* Tests {@link ByteBuffers#littleEndian(byte[])} with a non-empty array.
38+
*/
39+
@Test
40+
void testLittleEndianByteArray() {
41+
final byte[] array = { 1, 2, 3, 4 };
42+
final ByteBuffer buffer = ByteBuffers.littleEndian(array);
43+
assertNotNull(buffer);
44+
assertEquals(ByteOrder.LITTLE_ENDIAN, buffer.order());
45+
assertEquals(0, buffer.position());
46+
assertEquals(array.length, buffer.limit());
47+
assertEquals(array.length, buffer.capacity());
48+
assertArrayEquals(array, buffer.array());
49+
assertEquals(0, buffer.arrayOffset());
50+
}
51+
52+
/**
53+
* Tests {@link ByteBuffers#littleEndian(byte[])} with an empty array.
54+
*/
55+
@Test
56+
void testLittleEndianByteArrayEmpty() {
57+
final byte[] array = {};
58+
final ByteBuffer buffer = ByteBuffers.littleEndian(array);
59+
assertNotNull(buffer);
60+
assertEquals(ByteOrder.LITTLE_ENDIAN, buffer.order());
61+
assertEquals(0, buffer.capacity());
62+
assertArrayEquals(array, buffer.array());
63+
}
64+
65+
/**
66+
* Tests that {@link ByteBuffers#littleEndian(byte[])} wraps the given array (same backing array).
67+
*/
68+
@Test
69+
void testLittleEndianByteArrayIsSameBackingArray() {
70+
final byte[] array = { 10, 20, 30 };
71+
final ByteBuffer buffer = ByteBuffers.littleEndian(array);
72+
assertSame(array, buffer.array());
73+
}
74+
75+
/**
76+
* Tests that a multi-byte value written to a little-endian buffer from a byte array has correct byte order.
77+
*/
78+
@Test
79+
void testLittleEndianByteArrayMultibyteValue() {
80+
final byte[] array = new byte[4];
81+
final ByteBuffer buffer = ByteBuffers.littleEndian(array);
82+
buffer.putInt(0x01020304);
83+
// In little-endian, least significant byte is first
84+
assertEquals((byte) 0x04, array[0]);
85+
assertEquals((byte) 0x03, array[1]);
86+
assertEquals((byte) 0x02, array[2]);
87+
assertEquals((byte) 0x01, array[3]);
88+
}
89+
90+
/**
91+
* Tests {@link ByteBuffers#littleEndian(ByteBuffer)} sets order to little-endian and returns the same buffer.
92+
*/
93+
@Test
94+
void testLittleEndianByteBuffer() {
95+
final ByteBuffer buffer = ByteBuffer.allocate(8);
96+
// Default order is BIG_ENDIAN
97+
assertEquals(ByteOrder.BIG_ENDIAN, buffer.order());
98+
final ByteBuffer result = ByteBuffers.littleEndian(buffer);
99+
assertSame(buffer, result);
100+
assertEquals(ByteOrder.LITTLE_ENDIAN, result.order());
101+
}
102+
103+
/**
104+
* Tests {@link ByteBuffers#littleEndian(ByteBuffer)} when the buffer is already little-endian.
105+
*/
106+
@Test
107+
void testLittleEndianByteBufferAlreadyLittleEndian() {
108+
final ByteBuffer buffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
109+
final ByteBuffer result = ByteBuffers.littleEndian(buffer);
110+
assertSame(buffer, result);
111+
assertEquals(ByteOrder.LITTLE_ENDIAN, result.order());
112+
}
113+
114+
/**
115+
* Tests {@link ByteBuffers#littleEndian(int)} with a positive capacity.
116+
*/
117+
@Test
118+
void testLittleEndianInt() {
119+
final int capacity = 16;
120+
final ByteBuffer buffer = ByteBuffers.littleEndian(capacity);
121+
assertNotNull(buffer);
122+
assertEquals(ByteOrder.LITTLE_ENDIAN, buffer.order());
123+
assertEquals(capacity, buffer.capacity());
124+
assertEquals(0, buffer.position());
125+
assertEquals(capacity, buffer.limit());
126+
assertEquals(0, buffer.arrayOffset());
127+
// Each element should be initialized to zero
128+
for (int i = 0; i < capacity; i++) {
129+
assertEquals(0, buffer.get(i));
130+
}
131+
}
132+
133+
/**
134+
* Tests that a multi-byte value written to a little-endian buffer has correct byte order.
135+
*/
136+
@Test
137+
void testLittleEndianIntMultibyteValue() {
138+
final ByteBuffer buffer = ByteBuffers.littleEndian(4);
139+
buffer.putInt(0x01020304);
140+
buffer.flip();
141+
// In little-endian, least significant byte is first
142+
assertEquals((byte) 0x04, buffer.get(0));
143+
assertEquals((byte) 0x03, buffer.get(1));
144+
assertEquals((byte) 0x02, buffer.get(2));
145+
assertEquals((byte) 0x01, buffer.get(3));
146+
}
147+
148+
/**
149+
* Tests {@link ByteBuffers#littleEndian(int)} with a negative capacity throws {@link IllegalArgumentException}.
150+
*/
151+
@Test
152+
void testLittleEndianIntNegativeCapacity() {
153+
assertThrows(IllegalArgumentException.class, () -> ByteBuffers.littleEndian(-1));
154+
}
155+
156+
/**
157+
* Tests {@link ByteBuffers#littleEndian(int)} with zero capacity.
158+
*/
159+
@Test
160+
void testLittleEndianIntZeroCapacity() {
161+
final ByteBuffer buffer = ByteBuffers.littleEndian(0);
162+
assertNotNull(buffer);
163+
assertEquals(ByteOrder.LITTLE_ENDIAN, buffer.order());
164+
assertEquals(0, buffer.capacity());
165+
}
166+
167+
}

0 commit comments

Comments
 (0)