Commit 941ca3f3 by Oliver Woodman

Add readShort to ParsableByteArray

parent 0de2d3a8
......@@ -34,6 +34,31 @@ public class ParsableByteArrayTest extends TestCase {
return testArray;
}
public void testReadShort() {
testReadShort((short) -1);
testReadShort((short) 0);
testReadShort((short) 1);
testReadShort(Short.MIN_VALUE);
testReadShort(Short.MAX_VALUE);
}
private static void testReadShort(short testValue) {
ParsableByteArray testArray = new ParsableByteArray(
ByteBuffer.allocate(4).putShort(testValue).array());
int readValue = testArray.readShort();
// Assert that the value we read was the value we wrote.
assertEquals(testValue, readValue);
// And that the position advanced as expected.
assertEquals(2, testArray.getPosition());
// And that skipping back and reading gives the same results.
testArray.skipBytes(-2);
readValue = testArray.readShort();
assertEquals(testValue, readValue);
assertEquals(2, testArray.getPosition());
}
public void testReadInt() {
testReadInt(0);
testReadInt(1);
......
......@@ -170,6 +170,12 @@ public final class ParsableByteArray {
| (data[position++] & 0xFF);
}
/** Reads the next two bytes as an signed value. */
public short readShort() {
return (short) ((data[position++] & 0xFF) << 8
| (data[position++] & 0xFF));
}
/** Reads the next three bytes as an unsigned value. */
public int readUnsignedInt24() {
return (data[position++] & 0xFF) << 16
......
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