Skip to content

Commit df33a95

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents b7d82d0 + 8cb3a1f commit df33a95

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
/**
@@ -1939,7 +1939,7 @@ public char[] toCharArray() {
19391939
* @return char array
19401940
*/
19411941
public char[] toCharArray(Charset charset) {
1942-
return Util.byteToCharArray(internalArray(), charset);
1942+
return Util.byteToCharArray(internalArray(), charset, byteOrder);
19431943
}
19441944

19451945
/**
@@ -2021,7 +2021,7 @@ public boolean equals(Byte[] anotherArray) {
20212021
* @return true if both array have same length and every byte element is the same
20222022
*/
20232023
public boolean equals(ByteBuffer buffer) {
2024-
return buffer != null && byteOrder == buffer.order() && ByteBuffer.wrap(internalArray()).order(byteOrder).equals(buffer);
2024+
return buffer != null && byteOrder == buffer.order() && internalBuffer().equals(buffer);
20252025
}
20262026

20272027
/**

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

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

447448
try {
448-
CharBuffer charBuffer = charset.newDecoder().decode(ByteBuffer.wrap(bytes));
449+
CharBuffer charBuffer = charset.newDecoder().decode(ByteBuffer.wrap(bytes).order(byteOrder));
449450
if (charBuffer.capacity() != charBuffer.limit()) {
450451
char[] compacted = new char[charBuffer.remaining()];
451452
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)