From 7f8ddeac39269744c720104e93deea668ee4bcf6 Mon Sep 17 00:00:00 2001 From: Oliver Woodman Date: Wed, 16 Dec 2015 20:33:04 +0000 Subject: [PATCH] Added little endian methods to ParsableByteArray. --- .../exoplayer/util/ParsableByteArrayTest.java | 44 ++++++++++++++++++ .../exoplayer/util/ParsableByteArray.java | 45 +++++++++++++++++++ 2 files changed, 89 insertions(+) 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 235a1a53a3..c406b27930 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 @@ -278,4 +278,48 @@ public class ParsableByteArrayTest extends TestCase { 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()); + } + } 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 4bc8313dfe..294db67c42 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,12 +170,22 @@ public final class ParsableByteArray { | (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. */ public short readShort() { return (short) ((data[position++] & 0xFF) << 8 | (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. */ public int readUnsignedInt24() { return (data[position++] & 0xFF) << 16 @@ -183,6 +193,13 @@ public final class ParsableByteArray { | (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. */ public long readUnsignedInt() { return (data[position++] & 0xFFL) << 24 @@ -191,6 +208,14 @@ public final class ParsableByteArray { | (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. */ public int readInt() { return (data[position++] & 0xFF) << 24 @@ -199,6 +224,14 @@ public final class ParsableByteArray { | (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. */ public long readLong() { return (data[position++] & 0xFFL) << 56 @@ -211,6 +244,18 @@ public final class ParsableByteArray { | (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. */ public int readUnsignedFixedPoint1616() { int result = (data[position++] & 0xFF) << 8