Add readShort to ParsableByteArray

This commit is contained in:
Oliver Woodman 2015-10-26 15:25:46 +00:00
parent 0de2d3a863
commit 941ca3f304
2 changed files with 31 additions and 0 deletions

View file

@ -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);

View file

@ -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