Use new ParsableByteArray.reset where possible.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133384105
This commit is contained in:
andrewlewis 2016-09-16 07:55:34 -07:00 committed by Oliver Woodman
parent c17190319b
commit 22728d9ae4
5 changed files with 18 additions and 36 deletions

View File

@ -298,6 +298,7 @@ public final class DefaultTrackOutput implements TrackOutput {
long offset = extrasHolder.offset; long offset = extrasHolder.offset;
// Read the signal byte. // Read the signal byte.
scratch.reset(1);
readData(offset, scratch.data, 1); readData(offset, scratch.data, 1);
offset++; offset++;
byte signalByte = scratch.data[0]; byte signalByte = scratch.data[0];
@ -314,9 +315,9 @@ public final class DefaultTrackOutput implements TrackOutput {
// Read the subsample count, if present. // Read the subsample count, if present.
int subsampleCount; int subsampleCount;
if (subsampleEncryption) { if (subsampleEncryption) {
scratch.reset(2);
readData(offset, scratch.data, 2); readData(offset, scratch.data, 2);
offset += 2; offset += 2;
scratch.setPosition(0);
subsampleCount = scratch.readUnsignedShort(); subsampleCount = scratch.readUnsignedShort();
} else { } else {
subsampleCount = 1; subsampleCount = 1;
@ -333,7 +334,7 @@ public final class DefaultTrackOutput implements TrackOutput {
} }
if (subsampleEncryption) { if (subsampleEncryption) {
int subsampleDataLength = 6 * subsampleCount; int subsampleDataLength = 6 * subsampleCount;
ensureCapacity(scratch, subsampleDataLength); scratch.reset(subsampleDataLength);
readData(offset, scratch.data, subsampleDataLength); readData(offset, scratch.data, subsampleDataLength);
offset += subsampleDataLength; offset += subsampleDataLength;
scratch.setPosition(0); 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. // Called by the loading thread.
/** /**

View File

@ -950,13 +950,9 @@ public final class MatroskaExtractor implements Extractor {
samplePartitionCountRead = true; samplePartitionCountRead = true;
} }
int samplePartitionDataSize = samplePartitionCount * 4; int samplePartitionDataSize = samplePartitionCount * 4;
if (scratch.limit() < samplePartitionDataSize) { scratch.reset(samplePartitionDataSize);
scratch.reset(new byte[samplePartitionDataSize], samplePartitionDataSize);
}
input.readFully(scratch.data, 0, samplePartitionDataSize); input.readFully(scratch.data, 0, samplePartitionDataSize);
sampleBytesRead += samplePartitionDataSize; sampleBytesRead += samplePartitionDataSize;
scratch.setPosition(0);
scratch.setLimit(samplePartitionDataSize);
short subsampleCount = (short) (1 + (samplePartitionCount / 2)); short subsampleCount = (short) (1 + (samplePartitionCount / 2));
int subsampleDataSize = 2 + 6 * subsampleCount; int subsampleDataSize = 2 + 6 * subsampleCount;
if (encryptionSubsampleDataBuffer == null if (encryptionSubsampleDataBuffer == null

View File

@ -100,13 +100,14 @@ import java.io.IOException;
while (bytesSearched < bytesToSearch) { while (bytesSearched < bytesToSearch) {
// Read an atom header. // Read an atom header.
int headerSize = Atom.HEADER_SIZE; int headerSize = Atom.HEADER_SIZE;
buffer.reset(headerSize);
input.peekFully(buffer.data, 0, headerSize); input.peekFully(buffer.data, 0, headerSize);
buffer.setPosition(0);
long atomSize = buffer.readUnsignedInt(); long atomSize = buffer.readUnsignedInt();
int atomType = buffer.readInt(); int atomType = buffer.readInt();
if (atomSize == Atom.LONG_SIZE_PREFIX) { if (atomSize == Atom.LONG_SIZE_PREFIX) {
headerSize = Atom.LONG_HEADER_SIZE; headerSize = Atom.LONG_HEADER_SIZE;
input.peekFully(buffer.data, Atom.HEADER_SIZE, Atom.LONG_HEADER_SIZE - Atom.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(); atomSize = buffer.readUnsignedLongToLong();
} }
@ -139,9 +140,7 @@ import java.io.IOException;
if (atomDataSize < 8) { if (atomDataSize < 8) {
return false; return false;
} }
if (buffer.capacity() < atomDataSize) { buffer.reset(atomDataSize);
buffer.reset(new byte[atomDataSize], atomDataSize);
}
input.peekFully(buffer.data, 0, atomDataSize); input.peekFully(buffer.data, 0, atomDataSize);
int brandsCount = atomDataSize / 4; int brandsCount = atomDataSize / 4;
for (int i = 0; i < brandsCount; i++) { for (int i = 0; i < brandsCount; i++) {

View File

@ -153,8 +153,7 @@ public final class PsExtractor implements Extractor {
input.peekFully(psPacketBuffer.data, 0, 10); input.peekFully(psPacketBuffer.data, 0, 10);
// We only care about the pack_stuffing_length in here, skip the first 77 bits. // We only care about the pack_stuffing_length in here, skip the first 77 bits.
psPacketBuffer.setPosition(0); psPacketBuffer.setPosition(9);
psPacketBuffer.skipBytes(9);
// Last 3 bits is the length. // Last 3 bits is the length.
int packStuffingLength = psPacketBuffer.readUnsignedByte() & 0x07; 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); input.peekFully(psPacketBuffer.data, 0, 2);
psPacketBuffer.setPosition(0); psPacketBuffer.setPosition(0);
int payloadLength = psPacketBuffer.readUnsignedShort(); int payloadLength = psPacketBuffer.readUnsignedShort();
@ -219,14 +218,10 @@ public final class PsExtractor implements Extractor {
// Just skip this data. // Just skip this data.
input.skipFully(pesLength); input.skipFully(pesLength);
} else { } else {
if (psPacketBuffer.capacity() < pesLength) { psPacketBuffer.reset(pesLength);
// Reallocate for this and future packets.
psPacketBuffer.reset(new byte[pesLength], pesLength);
}
// Read the whole packet and the header for consumption. // Read the whole packet and the header for consumption.
input.readFully(psPacketBuffer.data, 0, pesLength); input.readFully(psPacketBuffer.data, 0, pesLength);
psPacketBuffer.setPosition(6); psPacketBuffer.setPosition(6);
psPacketBuffer.setLimit(pesLength);
payloadReader.consume(psPacketBuffer); payloadReader.consume(psPacketBuffer);
psPacketBuffer.setLimit(psPacketBuffer.capacity()); psPacketBuffer.setLimit(psPacketBuffer.capacity());
} }

View File

@ -35,17 +35,17 @@ public final class ParsableByteArray {
public 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) { public ParsableByteArray(int limit) {
this.data = new byte[length]; this.data = new byte[limit];
limit = data.length; 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. * @param data The array to wrap.
*/ */
@ -58,7 +58,7 @@ public final class ParsableByteArray {
* Creates a new instance that wraps an existing array. * Creates a new instance that wraps an existing array.
* *
* @param data The data to wrap. * @param data The data to wrap.
* @param limit The limit. * @param limit The limit to set.
*/ */
public ParsableByteArray(byte[] data, int limit) { public ParsableByteArray(byte[] data, int limit) {
this.data = data; this.data = data;
@ -79,7 +79,7 @@ public final class ParsableByteArray {
* Updates the instance to wrap {@code data}, and resets the position to zero. * Updates the instance to wrap {@code data}, and resets the position to zero.
* *
* @param data The array to wrap. * @param data The array to wrap.
* @param limit The limit. * @param limit The limit to set.
*/ */
public void reset(byte[] data, int limit) { public void reset(byte[] data, int limit) {
this.data = data; this.data = data;