Skip to content

Commit 8cb3a1f

Browse files
committed
Some small refactorings and add more little endian tests
1 parent 09cb2ce commit 8cb3a1f

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ public int unsignedByteAt(int index) {
13241324
*/
13251325
public char charAt(int index) {
13261326
Util.checkIndexBounds(length(), index, 2, "char");
1327-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getChar();
1327+
return ((ByteBuffer) internalBuffer().position(index)).getChar();
13281328
}
13291329

13301330
/**
@@ -1337,7 +1337,7 @@ public char charAt(int index) {
13371337
*/
13381338
public short shortAt(int index) {
13391339
Util.checkIndexBounds(length(), index, 2, "short");
1340-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getShort();
1340+
return ((ByteBuffer) internalBuffer().position(index)).getShort();
13411341
}
13421342

13431343
/**
@@ -1350,7 +1350,7 @@ public short shortAt(int index) {
13501350
*/
13511351
public int intAt(int index) {
13521352
Util.checkIndexBounds(length(), index, 4, "int");
1353-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getInt();
1353+
return ((ByteBuffer) internalBuffer().position(index)).getInt();
13541354
}
13551355

13561356
/**
@@ -1363,7 +1363,7 @@ public int intAt(int index) {
13631363
*/
13641364
public long longAt(int index) {
13651365
Util.checkIndexBounds(length(), index, 8, "long");
1366-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getLong();
1366+
return ((ByteBuffer) internalBuffer().position(index)).getLong();
13671367
}
13681368

13691369
/**
@@ -1927,7 +1927,7 @@ public char[] toCharArray() {
19271927
* @return char array
19281928
*/
19291929
public char[] toCharArray(Charset charset) {
1930-
return Util.byteToCharArray(internalArray(), charset);
1930+
return Util.byteToCharArray(internalArray(), charset, byteOrder);
19311931
}
19321932

19331933
/**
@@ -2009,7 +2009,7 @@ public boolean equals(Byte[] anotherArray) {
20092009
* @return true if both array have same length and every byte element is the same
20102010
*/
20112011
public boolean equals(ByteBuffer buffer) {
2012-
return buffer != null && byteOrder == buffer.order() && ByteBuffer.wrap(internalArray()).order(byteOrder).equals(buffer);
2012+
return buffer != null && byteOrder == buffer.order() && internalBuffer().equals(buffer);
20132013
}
20142014

20152015
/**

src/main/java/at/favre/lib/bytes/Util.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,17 @@ static byte[] charToByteArray(char[] charArray, Charset charset, int offset, int
435435
/**
436436
* Convert given byte array in given encoding to char array
437437
*
438-
* @param bytes as data source
439-
* @param charset of the byte array
438+
* @param bytes as data source
439+
* @param charset of the byte array
440+
* @param byteOrder the order of the bytes array
440441
* @return char array
441442
*/
442-
static char[] byteToCharArray(byte[] bytes, Charset charset) {
443+
static char[] byteToCharArray(byte[] bytes, Charset charset, ByteOrder byteOrder) {
443444
Objects.requireNonNull(bytes, "bytes must not be null");
444445
Objects.requireNonNull(charset, "charset must not be null");
445446

446447
try {
447-
CharBuffer charBuffer = charset.newDecoder().decode(ByteBuffer.wrap(bytes));
448+
CharBuffer charBuffer = charset.newDecoder().decode(ByteBuffer.wrap(bytes).order(byteOrder));
448449
if (charBuffer.capacity() != charBuffer.limit()) {
449450
char[] compacted = new char[charBuffer.remaining()];
450451
charBuffer.get(compacted);

src/test/java/at/favre/lib/bytes/BytesMiscTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void testHashcode() {
6262
assertNotEquals(0, Bytes.wrap(example2_bytes_seven).hashCode());
6363
}
6464

65+
@SuppressWarnings("SimplifiableJUnitAssertion")
6566
@Test
6667
public void testEquals() {
6768
assertTrue(Bytes.wrap(new byte[0]).equals(Bytes.wrap(new byte[0])));
@@ -150,6 +151,7 @@ public void testIsEmpty() {
150151
assertFalse(Bytes.from(example_bytes_seven).isEmpty());
151152
}
152153

154+
@SuppressWarnings("SimplifiableJUnitAssertion")
153155
@Test
154156
public void containsTest() {
155157
assertEquals(false, Bytes.allocate(0).contains((byte) 0xFD));
@@ -372,6 +374,15 @@ public void intAt() {
372374
}
373375
}
374376

377+
@Test
378+
public void intAtLittleEndian() {
379+
assertEquals(16777216, Bytes.wrap(new byte[]{0, 0, 0, 1}, ByteOrder.LITTLE_ENDIAN).intAt(0));
380+
assertEquals(1, Bytes.wrap(new byte[]{0, 0, 0, 1}, ByteOrder.BIG_ENDIAN).intAt(0));
381+
assertEquals(134217728, Bytes.wrap(new byte[]{0, 0, 0, 0b00001000}, ByteOrder.LITTLE_ENDIAN).intAt(0));
382+
assertEquals(524288, Bytes.wrap(new byte[]{0, 0, 0b00001000, 0}, ByteOrder.LITTLE_ENDIAN).intAt(0));
383+
assertEquals(8388608, Bytes.wrap(new byte[]{0, 0, (byte) 0b10000000, 0}, ByteOrder.LITTLE_ENDIAN).intAt(0));
384+
}
385+
375386
@Test
376387
public void longAt() {
377388
assertEquals(0, Bytes.allocate(8).longAt(0));
@@ -395,6 +406,16 @@ public void longAt() {
395406
}
396407
}
397408

409+
@Test
410+
public void longAtLittleEndian() {
411+
assertEquals(72057594037927936L, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 1}, ByteOrder.LITTLE_ENDIAN).longAt(0));
412+
assertEquals(1, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 1}, ByteOrder.BIG_ENDIAN).longAt(0));
413+
assertEquals(576460752303423488L, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 0b00001000}, ByteOrder.LITTLE_ENDIAN).longAt(0));
414+
assertEquals(2251799813685248L, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0b00001000, 0}, ByteOrder.LITTLE_ENDIAN).longAt(0));
415+
assertEquals(36028797018963968L, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, (byte) 0b10000000, 0}, ByteOrder.LITTLE_ENDIAN).longAt(0));
416+
assertEquals(549755813888L, Bytes.wrap(new byte[]{0, 0, 0, 0, (byte) 0b10000000, 0, 0, 0}, ByteOrder.LITTLE_ENDIAN).longAt(0));
417+
}
418+
398419
@Test
399420
public void primitiveAtLittleEndian() {
400421
assertEquals(576460752303423488L, Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 0b00001000}).byteOrder(ByteOrder.LITTLE_ENDIAN).longAt(0)); //2^59

0 commit comments

Comments
 (0)