Commit 7f8ddeac by Oliver Woodman

Added little endian methods to ParsableByteArray.

parent 50d5cbea
...@@ -278,4 +278,48 @@ public class ParsableByteArrayTest extends TestCase { ...@@ -278,4 +278,48 @@ public class ParsableByteArrayTest extends TestCase {
assertTrue(Arrays.equals(parsableByteArray.data, copy)); 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());
}
} }
...@@ -170,12 +170,22 @@ public final class ParsableByteArray { ...@@ -170,12 +170,22 @@ public final class ParsableByteArray {
| (data[position++] & 0xFF); | (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. */ /** Reads the next two bytes as an signed value. */
public short readShort() { public short readShort() {
return (short) ((data[position++] & 0xFF) << 8 return (short) ((data[position++] & 0xFF) << 8
| (data[position++] & 0xFF)); | (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. */ /** Reads the next three bytes as an unsigned value. */
public int readUnsignedInt24() { public int readUnsignedInt24() {
return (data[position++] & 0xFF) << 16 return (data[position++] & 0xFF) << 16
...@@ -183,6 +193,13 @@ public final class ParsableByteArray { ...@@ -183,6 +193,13 @@ public final class ParsableByteArray {
| (data[position++] & 0xFF); | (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. */ /** Reads the next four bytes as an unsigned value. */
public long readUnsignedInt() { public long readUnsignedInt() {
return (data[position++] & 0xFFL) << 24 return (data[position++] & 0xFFL) << 24
...@@ -191,6 +208,14 @@ public final class ParsableByteArray { ...@@ -191,6 +208,14 @@ public final class ParsableByteArray {
| (data[position++] & 0xFFL); | (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. */ /** Reads the next four bytes as a signed value. */
public int readInt() { public int readInt() {
return (data[position++] & 0xFF) << 24 return (data[position++] & 0xFF) << 24
...@@ -199,6 +224,14 @@ public final class ParsableByteArray { ...@@ -199,6 +224,14 @@ public final class ParsableByteArray {
| (data[position++] & 0xFF); | (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. */ /** Reads the next eight bytes as a signed value. */
public long readLong() { public long readLong() {
return (data[position++] & 0xFFL) << 56 return (data[position++] & 0xFFL) << 56
...@@ -211,6 +244,18 @@ public final class ParsableByteArray { ...@@ -211,6 +244,18 @@ public final class ParsableByteArray {
| (data[position++] & 0xFFL); | (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. */ /** Reads the next four bytes, returning the integer portion of the fixed point 16.16 integer. */
public int readUnsignedFixedPoint1616() { public int readUnsignedFixedPoint1616() {
int result = (data[position++] & 0xFF) << 8 int result = (data[position++] & 0xFF) << 8
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment