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;
// 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.
/**

View File

@ -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

View File

@ -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++) {

View File

@ -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());
}

View File

@ -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;