diff --git a/library/src/main/java/com/google/android/exoplayer2/extractor/DefaultTrackOutput.java b/library/src/main/java/com/google/android/exoplayer2/extractor/DefaultTrackOutput.java index 847d429090..f7493a8b8a 100644 --- a/library/src/main/java/com/google/android/exoplayer2/extractor/DefaultTrackOutput.java +++ b/library/src/main/java/com/google/android/exoplayer2/extractor/DefaultTrackOutput.java @@ -298,6 +298,7 @@ public final class DefaultTrackOutput implements TrackOutput { long offset = extrasHolder.offset; // Read the signal byte. + scratch.reset(1); readData(offset, scratch.data, 1); offset++; byte signalByte = scratch.data[0]; @@ -314,9 +315,9 @@ public final class DefaultTrackOutput implements TrackOutput { // Read the subsample count, if present. int subsampleCount; if (subsampleEncryption) { + scratch.reset(2); readData(offset, scratch.data, 2); offset += 2; - scratch.setPosition(0); subsampleCount = scratch.readUnsignedShort(); } else { subsampleCount = 1; @@ -333,7 +334,7 @@ public final class DefaultTrackOutput implements TrackOutput { } if (subsampleEncryption) { int subsampleDataLength = 6 * subsampleCount; - ensureCapacity(scratch, subsampleDataLength); + scratch.reset(subsampleDataLength); readData(offset, scratch.data, subsampleDataLength); offset += subsampleDataLength; scratch.setPosition(0); @@ -412,15 +413,6 @@ public final class DefaultTrackOutput implements TrackOutput { } } - /** - * Ensure that the passed {@link ParsableByteArray} is of at least the specified limit. - */ - private static void ensureCapacity(ParsableByteArray byteArray, int limit) { - if (byteArray.limit() < limit) { - byteArray.reset(new byte[limit], limit); - } - } - // Called by the loading thread. /** diff --git a/library/src/main/java/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractor.java b/library/src/main/java/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractor.java index c658ab3bcd..917af9c7e1 100644 --- a/library/src/main/java/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractor.java +++ b/library/src/main/java/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractor.java @@ -950,13 +950,9 @@ public final class MatroskaExtractor implements Extractor { samplePartitionCountRead = true; } int samplePartitionDataSize = samplePartitionCount * 4; - if (scratch.limit() < samplePartitionDataSize) { - scratch.reset(new byte[samplePartitionDataSize], samplePartitionDataSize); - } + scratch.reset(samplePartitionDataSize); input.readFully(scratch.data, 0, samplePartitionDataSize); sampleBytesRead += samplePartitionDataSize; - scratch.setPosition(0); - scratch.setLimit(samplePartitionDataSize); short subsampleCount = (short) (1 + (samplePartitionCount / 2)); int subsampleDataSize = 2 + 6 * subsampleCount; if (encryptionSubsampleDataBuffer == null diff --git a/library/src/main/java/com/google/android/exoplayer2/extractor/mp4/Sniffer.java b/library/src/main/java/com/google/android/exoplayer2/extractor/mp4/Sniffer.java index 842b648ec2..44d5824945 100644 --- a/library/src/main/java/com/google/android/exoplayer2/extractor/mp4/Sniffer.java +++ b/library/src/main/java/com/google/android/exoplayer2/extractor/mp4/Sniffer.java @@ -100,13 +100,14 @@ import java.io.IOException; while (bytesSearched < bytesToSearch) { // Read an atom header. int headerSize = Atom.HEADER_SIZE; + buffer.reset(headerSize); input.peekFully(buffer.data, 0, headerSize); - buffer.setPosition(0); long atomSize = buffer.readUnsignedInt(); int atomType = buffer.readInt(); if (atomSize == Atom.LONG_SIZE_PREFIX) { headerSize = Atom.LONG_HEADER_SIZE; input.peekFully(buffer.data, Atom.HEADER_SIZE, Atom.LONG_HEADER_SIZE - Atom.HEADER_SIZE); + buffer.setLimit(Atom.LONG_HEADER_SIZE); atomSize = buffer.readUnsignedLongToLong(); } @@ -139,9 +140,7 @@ import java.io.IOException; if (atomDataSize < 8) { return false; } - if (buffer.capacity() < atomDataSize) { - buffer.reset(new byte[atomDataSize], atomDataSize); - } + buffer.reset(atomDataSize); input.peekFully(buffer.data, 0, atomDataSize); int brandsCount = atomDataSize / 4; for (int i = 0; i < brandsCount; i++) { diff --git a/library/src/main/java/com/google/android/exoplayer2/extractor/ts/PsExtractor.java b/library/src/main/java/com/google/android/exoplayer2/extractor/ts/PsExtractor.java index ff86be4165..d69a2a1e96 100644 --- a/library/src/main/java/com/google/android/exoplayer2/extractor/ts/PsExtractor.java +++ b/library/src/main/java/com/google/android/exoplayer2/extractor/ts/PsExtractor.java @@ -153,8 +153,7 @@ public final class PsExtractor implements Extractor { input.peekFully(psPacketBuffer.data, 0, 10); // We only care about the pack_stuffing_length in here, skip the first 77 bits. - psPacketBuffer.setPosition(0); - psPacketBuffer.skipBytes(9); + psPacketBuffer.setPosition(9); // Last 3 bits is the length. int packStuffingLength = psPacketBuffer.readUnsignedByte() & 0x07; @@ -209,7 +208,7 @@ public final class PsExtractor implements Extractor { } } - // The next 2 bytes are the length, once we have that we can consume the complete packet. + // The next 2 bytes are the length. Once we have that we can consume the complete packet. input.peekFully(psPacketBuffer.data, 0, 2); psPacketBuffer.setPosition(0); int payloadLength = psPacketBuffer.readUnsignedShort(); @@ -219,14 +218,10 @@ public final class PsExtractor implements Extractor { // Just skip this data. input.skipFully(pesLength); } else { - if (psPacketBuffer.capacity() < pesLength) { - // Reallocate for this and future packets. - psPacketBuffer.reset(new byte[pesLength], pesLength); - } + psPacketBuffer.reset(pesLength); // Read the whole packet and the header for consumption. input.readFully(psPacketBuffer.data, 0, pesLength); psPacketBuffer.setPosition(6); - psPacketBuffer.setLimit(pesLength); payloadReader.consume(psPacketBuffer); psPacketBuffer.setLimit(psPacketBuffer.capacity()); } diff --git a/library/src/main/java/com/google/android/exoplayer2/util/ParsableByteArray.java b/library/src/main/java/com/google/android/exoplayer2/util/ParsableByteArray.java index a499dc8012..e140484531 100644 --- a/library/src/main/java/com/google/android/exoplayer2/util/ParsableByteArray.java +++ b/library/src/main/java/com/google/android/exoplayer2/util/ParsableByteArray.java @@ -35,17 +35,17 @@ public final class ParsableByteArray { public ParsableByteArray() {} /** - * Creates a new instance with {@code length} bytes. + * Creates a new instance with {@code limit} bytes and sets the limit. * - * @param length The length of the array. + * @param limit The limit to set. */ - public ParsableByteArray(int length) { - this.data = new byte[length]; - limit = data.length; + public ParsableByteArray(int limit) { + this.data = new byte[limit]; + this.limit = limit; } /** - * Creates a new instance wrapping {@code data}. + * Creates a new instance wrapping {@code data}, and sets the limit to {@code data.length}. * * @param data The array to wrap. */ @@ -58,7 +58,7 @@ public final class ParsableByteArray { * Creates a new instance that wraps an existing array. * * @param data The data to wrap. - * @param limit The limit. + * @param limit The limit to set. */ public ParsableByteArray(byte[] data, int limit) { this.data = data; @@ -79,7 +79,7 @@ public final class ParsableByteArray { * Updates the instance to wrap {@code data}, and resets the position to zero. * * @param data The array to wrap. - * @param limit The limit. + * @param limit The limit to set. */ public void reset(byte[] data, int limit) { this.data = data;