Added little endian methods to ParsableByteArray.

This commit is contained in:
Oliver Woodman 2015-12-16 20:33:04 +00:00
parent 50d5cbea70
commit 7f8ddeac39
2 changed files with 89 additions and 0 deletions

View file

@ -278,4 +278,48 @@ public class ParsableByteArrayTest extends TestCase {
assertTrue(Arrays.equals(parsableByteArray.data, copy));
}
public void testReadLittleEndianLong() {
ParsableByteArray byteArray = new ParsableByteArray(new byte[]{
0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, (byte) 0xFF
});
assertEquals(0xFF00000000000001L, byteArray.readLittleEndianLong());
}
public void testReadLittleEndianUnsignedInt() {
ParsableByteArray byteArray = new ParsableByteArray(new byte[] {
0x10, 0x00, 0x00, (byte) 0xFF
});
assertEquals(0xFF000010L, byteArray.readLittleEndianUnsignedInt());
}
public void testReadLittleEndianInt() {
ParsableByteArray byteArray = new ParsableByteArray(new byte[]{
0x01, 0x00, 0x00, (byte) 0xFF
});
assertEquals(0xFF000001, byteArray.readLittleEndianInt());
}
public void testReadLittleEndianUnsignedInt24() {
byte[] data = { 0x01, 0x02, (byte) 0xFF };
ParsableByteArray byteArray = new ParsableByteArray(data);
assertEquals(0xFF0201, byteArray.readLittleEndianUnsignedInt24());
}
public void testReadLittleEndianUnsignedShort() {
ParsableByteArray byteArray = new ParsableByteArray(new byte[]{
0x01, (byte) 0xFF, 0x02, (byte) 0xFF
});
assertEquals(0xFF01, byteArray.readLittleEndianUnsignedShort());
assertEquals(0xFF02, byteArray.readLittleEndianUnsignedShort());
}
public void testReadLittleEndianShort() {
ParsableByteArray byteArray = new ParsableByteArray(new byte[]{
0x01, (byte) 0xFF, 0x02, (byte) 0xFF
});
assertEquals((short) 0xFF01, byteArray.readLittleEndianShort());
assertEquals((short) 0xFF02, byteArray.readLittleEndianShort());
}
}

View file

@ -170,12 +170,22 @@ public final class ParsableByteArray {
| (data[position++] & 0xFF);
}
/** Reads the next two bytes as an unsigned value. */
public int readLittleEndianUnsignedShort() {
return (data[position++] & 0xFF) | (data[position++] & 0xFF) << 8;
}
/** Reads the next two bytes as an signed value. */
public short readShort() {
return (short) ((data[position++] & 0xFF) << 8
| (data[position++] & 0xFF));
}
/** Reads the next two bytes as a signed value. */
public short readLittleEndianShort() {
return (short) ((data[position++] & 0xFF) | (data[position++] & 0xFF) << 8);
}
/** Reads the next three bytes as an unsigned value. */
public int readUnsignedInt24() {
return (data[position++] & 0xFF) << 16
@ -183,6 +193,13 @@ public final class ParsableByteArray {
| (data[position++] & 0xFF);
}
/** Reads the next three bytes as an unsigned value in little endian order. */
public int readLittleEndianUnsignedInt24() {
return (data[position++] & 0xFF)
| (data[position++] & 0xFF) << 8
| (data[position++] & 0xFF) << 16;
}
/** Reads the next four bytes as an unsigned value. */
public long readUnsignedInt() {
return (data[position++] & 0xFFL) << 24
@ -191,6 +208,14 @@ public final class ParsableByteArray {
| (data[position++] & 0xFFL);
}
/** Reads the next four bytes as an unsigned value in little endian order. */
public long readLittleEndianUnsignedInt() {
return (data[position++] & 0xFFL)
| (data[position++] & 0xFFL) << 8
| (data[position++] & 0xFFL) << 16
| (data[position++] & 0xFFL) << 24;
}
/** Reads the next four bytes as a signed value. */
public int readInt() {
return (data[position++] & 0xFF) << 24
@ -199,6 +224,14 @@ public final class ParsableByteArray {
| (data[position++] & 0xFF);
}
/** Reads the next four bytes as an signed value in little endian order. */
public int readLittleEndianInt() {
return (data[position++] & 0xFF)
| (data[position++] & 0xFF) << 8
| (data[position++] & 0xFF) << 16
| (data[position++] & 0xFF) << 24;
}
/** Reads the next eight bytes as a signed value. */
public long readLong() {
return (data[position++] & 0xFFL) << 56
@ -211,6 +244,18 @@ public final class ParsableByteArray {
| (data[position++] & 0xFFL);
}
/** Reads the next eight bytes as a signed value in little endian order. */
public long readLittleEndianLong() {
return (data[position++] & 0xFFL)
| (data[position++] & 0xFFL) << 8
| (data[position++] & 0xFFL) << 16
| (data[position++] & 0xFFL) << 24
| (data[position++] & 0xFFL) << 32
| (data[position++] & 0xFFL) << 40
| (data[position++] & 0xFFL) << 48
| (data[position++] & 0xFFL) << 56;
}
/** Reads the next four bytes, returning the integer portion of the fixed point 16.16 integer. */
public int readUnsignedFixedPoint1616() {
int result = (data[position++] & 0xFF) << 8