diff --git a/library/src/androidTest/java/com/google/android/exoplayer/util/ParsableByteArrayTest.java b/library/src/androidTest/java/com/google/android/exoplayer/util/ParsableByteArrayTest.java index ac3590cd24..235a1a53a3 100644 --- a/library/src/androidTest/java/com/google/android/exoplayer/util/ParsableByteArrayTest.java +++ b/library/src/androidTest/java/com/google/android/exoplayer/util/ParsableByteArrayTest.java @@ -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); diff --git a/library/src/main/java/com/google/android/exoplayer/util/ParsableByteArray.java b/library/src/main/java/com/google/android/exoplayer/util/ParsableByteArray.java index 563b9ebe13..4bc8313dfe 100644 --- a/library/src/main/java/com/google/android/exoplayer/util/ParsableByteArray.java +++ b/library/src/main/java/com/google/android/exoplayer/util/ParsableByteArray.java @@ -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