mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Add readShort to ParsableByteArray
This commit is contained in:
parent
0de2d3a863
commit
941ca3f304
2 changed files with 31 additions and 0 deletions
|
|
@ -34,6 +34,31 @@ public class ParsableByteArrayTest extends TestCase {
|
||||||
return testArray;
|
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() {
|
public void testReadInt() {
|
||||||
testReadInt(0);
|
testReadInt(0);
|
||||||
testReadInt(1);
|
testReadInt(1);
|
||||||
|
|
|
||||||
|
|
@ -170,6 +170,12 @@ public final class ParsableByteArray {
|
||||||
| (data[position++] & 0xFF);
|
| (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. */
|
/** 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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue