Skip to content

Commit

Permalink
Hide ParsableByteArray#data behind a getter
Browse files Browse the repository at this point in the history
This allows us to enforce the limit because the array can only be
reassigned through reset(byte[]) or reset(byte[], int) (which update
the limit)

PiperOrigin-RevId: 323339960
  • Loading branch information
icbaker authored and ojw28 committed Jul 27, 2020
1 parent 478f59f commit ce2e6e2
Show file tree
Hide file tree
Showing 67 changed files with 320 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void decodeStreamMetadata(ExtractorInput input) throws IOException {
if (this.streamMetadata == null) {
this.streamMetadata = streamMetadata;
outputBuffer.reset(streamMetadata.getMaxDecodedFrameSize());
outputFrameHolder = new OutputFrameHolder(ByteBuffer.wrap(outputBuffer.data));
outputFrameHolder = new OutputFrameHolder(ByteBuffer.wrap(outputBuffer.getData()));
binarySearchSeeker =
outputSeekMap(
flacDecoderJni,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,14 @@ public static int parseAc4SyncframeAudioSampleCount(ByteBuffer buffer) {
public static void getAc4SampleHeader(int size, ParsableByteArray buffer) {
// See ETSI TS 103 190-1 V1.3.1, Annex G.
buffer.reset(SAMPLE_HEADER_SIZE);
buffer.data[0] = (byte) 0xAC;
buffer.data[1] = 0x40;
buffer.data[2] = (byte) 0xFF;
buffer.data[3] = (byte) 0xFF;
buffer.data[4] = (byte) ((size >> 16) & 0xFF);
buffer.data[5] = (byte) ((size >> 8) & 0xFF);
buffer.data[6] = (byte) (size & 0xFF);
byte[] data = buffer.getData();
data[0] = (byte) 0xAC;
data[1] = 0x40;
data[2] = (byte) 0xFF;
data[3] = (byte) 0xFF;
data[4] = (byte) ((size >> 16) & 0xFF);
data[5] = (byte) ((size >> 8) & 0xFF);
data[6] = (byte) (size & 0xFF);
}

private static int readVariableBits(ParsableBitArray data, int bitsPerRead) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public EventMessage decode(ParsableByteArray emsgData) {
long durationMs = emsgData.readUnsignedInt();
long id = emsgData.readUnsignedInt();
byte[] messageData =
Arrays.copyOfRange(emsgData.data, emsgData.getPosition(), emsgData.limit());
Arrays.copyOfRange(emsgData.getData(), emsgData.getPosition(), emsgData.limit());
return new EventMessage(schemeIdUri, value, durationMs, id, messageData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,10 @@ private static ChapterFrame decodeChapterFrame(
@Nullable FramePredicate framePredicate)
throws UnsupportedEncodingException {
int framePosition = id3Data.getPosition();
int chapterIdEndIndex = indexOfZeroByte(id3Data.data, framePosition);
String chapterId = new String(id3Data.data, framePosition, chapterIdEndIndex - framePosition,
"ISO-8859-1");
int chapterIdEndIndex = indexOfZeroByte(id3Data.getData(), framePosition);
String chapterId =
new String(
id3Data.getData(), framePosition, chapterIdEndIndex - framePosition, "ISO-8859-1");
id3Data.setPosition(chapterIdEndIndex + 1);

int startTime = id3Data.readInt();
Expand Down Expand Up @@ -640,9 +641,10 @@ private static ChapterTocFrame decodeChapterTOCFrame(
@Nullable FramePredicate framePredicate)
throws UnsupportedEncodingException {
int framePosition = id3Data.getPosition();
int elementIdEndIndex = indexOfZeroByte(id3Data.data, framePosition);
String elementId = new String(id3Data.data, framePosition, elementIdEndIndex - framePosition,
"ISO-8859-1");
int elementIdEndIndex = indexOfZeroByte(id3Data.getData(), framePosition);
String elementId =
new String(
id3Data.getData(), framePosition, elementIdEndIndex - framePosition, "ISO-8859-1");
id3Data.setPosition(elementIdEndIndex + 1);

int ctocFlags = id3Data.readUnsignedByte();
Expand All @@ -653,8 +655,8 @@ private static ChapterTocFrame decodeChapterTOCFrame(
String[] children = new String[childCount];
for (int i = 0; i < childCount; i++) {
int startIndex = id3Data.getPosition();
int endIndex = indexOfZeroByte(id3Data.data, startIndex);
children[i] = new String(id3Data.data, startIndex, endIndex - startIndex, "ISO-8859-1");
int endIndex = indexOfZeroByte(id3Data.getData(), startIndex);
children[i] = new String(id3Data.getData(), startIndex, endIndex - startIndex, "ISO-8859-1");
id3Data.setPosition(endIndex + 1);
}

Expand Down Expand Up @@ -721,7 +723,7 @@ private static BinaryFrame decodeBinaryFrame(ParsableByteArray id3Data, int fram
* @return The length of the data after processing.
*/
private static int removeUnsynchronization(ParsableByteArray data, int length) {
byte[] bytes = data.data;
byte[] bytes = data.getData();
int startPosition = data.getPosition();
for (int i = startPosition; i + 1 < startPosition + length; i++) {
if ((bytes[i] & 0xFF) == 0xFF && bytes[i + 1] == 0x00) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void reset(byte[] data) {
* @param parsableByteArray The {@link ParsableByteArray}.
*/
public void reset(ParsableByteArray parsableByteArray) {
reset(parsableByteArray.data, parsableByteArray.limit());
reset(parsableByteArray.getData(), parsableByteArray.limit());
setPosition(parsableByteArray.getPosition() * 8);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
*/
public final class ParsableByteArray {

public byte[] data;

private byte[] data;
private int position;

// TODO(internal b/147657250): Enforce this limit on all read methods.
private int limit;

Expand Down Expand Up @@ -138,13 +136,6 @@ public int getPosition() {
return position;
}

/**
* Returns the capacity of the array, which may be larger than the limit.
*/
public int capacity() {
return data.length;
}

/**
* Sets the reading offset in the array.
*
Expand All @@ -158,6 +149,23 @@ public void setPosition(int position) {
this.position = position;
}

/**
* Returns the underlying array.
*
* <p>Changes to this array are reflected in the results of the {@code read...()} methods.
*
* <p>This reference must be assumed to become invalid when {@link #reset} is called (because the
* array might get reallocated).
*/
public byte[] getData() {
return data;
}

/** Returns the capacity of the array, which may be larger than the limit. */
public int capacity() {
return data.length;
}

/**
* Moves the reading offset by {@code bytes}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2046,14 +2046,14 @@ public static boolean inflate(
if (input.bytesLeft() <= 0) {
return false;
}
byte[] outputData = output.data;
byte[] outputData = output.getData();
if (outputData.length < input.bytesLeft()) {
outputData = new byte[2 * input.bytesLeft()];
}
if (inflater == null) {
inflater = new Inflater();
}
inflater.setInput(input.data, input.getPosition(), input.bytesLeft());
inflater.setInput(input.getData(), input.getPosition(), input.bytesLeft());
try {
int outputSize = 0;
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static byte[] buildNalUnitForChild(ParsableByteArray data) {
int length = data.readUnsignedShort();
int offset = data.getPosition();
data.skipBytes(length);
return CodecSpecificDataUtil.buildNalUnit(data.data, offset, length);
return CodecSpecificDataUtil.buildNalUnit(data.getData(), offset, length);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public static HevcConfig parse(ParsableByteArray data) throws ParserException {
System.arraycopy(NalUnitUtil.NAL_START_CODE, 0, buffer, bufferPosition,
NalUnitUtil.NAL_START_CODE.length);
bufferPosition += NalUnitUtil.NAL_START_CODE.length;
System
.arraycopy(data.data, data.getPosition(), buffer, bufferPosition, nalUnitLength);
System.arraycopy(
data.getData(), data.getPosition(), buffer, bufferPosition, nalUnitLength);
bufferPosition += nalUnitLength;
data.skipBytes(nalUnitLength);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class ParsableByteArrayTest {

private static ParsableByteArray getTestDataArray() {
ParsableByteArray testArray = new ParsableByteArray(TEST_DATA.length);
System.arraycopy(TEST_DATA, 0, testArray.data, 0, TEST_DATA.length);
System.arraycopy(TEST_DATA, 0, testArray.getData(), 0, TEST_DATA.length);
return testArray;
}

Expand Down Expand Up @@ -246,7 +246,7 @@ public void modificationsAffectParsableArray() {
ParsableByteArray parsableByteArray = getTestDataArray();

// When modifying the wrapped byte array
byte[] data = parsableByteArray.data;
byte[] data = parsableByteArray.getData();
long readValue = parsableByteArray.readUnsignedInt();
data[0] = (byte) (TEST_DATA[0] + 1);
parsableByteArray.setPosition(0);
Expand All @@ -259,7 +259,7 @@ public void readingUnsignedLongWithMsbSetThrows() {
ParsableByteArray parsableByteArray = getTestDataArray();

// Given an array with the most-significant bit set on the top byte
byte[] data = parsableByteArray.data;
byte[] data = parsableByteArray.getData();
data[0] = (byte) 0x80;
// Then reading an unsigned long throws.
try {
Expand Down Expand Up @@ -291,7 +291,7 @@ public void readingBytesReturnsCopy() {
byte[] copy = new byte[length];
parsableByteArray.readBytes(copy, 0, length);
// Then the array elements are the same.
assertThat(copy).isEqualTo(parsableByteArray.data);
assertThat(copy).isEqualTo(parsableByteArray.getData());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public void inflate_withDeflatedData_success() {
ParsableByteArray output = new ParsableByteArray();
assertThat(Util.inflate(input, output, /* inflater= */ null)).isTrue();
assertThat(output.limit()).isEqualTo(testData.length);
assertThat(Arrays.copyOf(output.data, output.limit())).isEqualTo(testData);
assertThat(Arrays.copyOf(output.getData(), output.limit())).isEqualTo(testData);
}

// TODO: Revert to @Config(sdk = Config.ALL_SDKS) once b/143232359 is resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void readToBuffer(DecoderInputBuffer buffer, SampleExtrasHolder extrasHol
if (buffer.hasSupplementalData()) {
// If there is supplemental data, the sample data is prefixed by its size.
scratch.reset(4);
readData(extrasHolder.offset, scratch.data, 4);
readData(extrasHolder.offset, scratch.getData(), 4);
int sampleSize = scratch.readUnsignedIntToInt();
extrasHolder.offset += 4;
extrasHolder.size -= 4;
Expand Down Expand Up @@ -223,9 +223,9 @@ private void readEncryptionData(DecoderInputBuffer buffer, SampleExtrasHolder ex

// Read the signal byte.
scratch.reset(1);
readData(offset, scratch.data, 1);
readData(offset, scratch.getData(), 1);
offset++;
byte signalByte = scratch.data[0];
byte signalByte = scratch.getData()[0];
boolean subsampleEncryption = (signalByte & 0x80) != 0;
int ivSize = signalByte & 0x7F;

Expand All @@ -244,7 +244,7 @@ private void readEncryptionData(DecoderInputBuffer buffer, SampleExtrasHolder ex
int subsampleCount;
if (subsampleEncryption) {
scratch.reset(2);
readData(offset, scratch.data, 2);
readData(offset, scratch.getData(), 2);
offset += 2;
subsampleCount = scratch.readUnsignedShort();
} else {
Expand All @@ -263,7 +263,7 @@ private void readEncryptionData(DecoderInputBuffer buffer, SampleExtrasHolder ex
if (subsampleEncryption) {
int subsampleDataLength = 6 * subsampleCount;
scratch.reset(subsampleDataLength);
readData(offset, scratch.data, subsampleDataLength);
readData(offset, scratch.getData(), subsampleDataLength);
offset += subsampleDataLength;
scratch.setPosition(0);
for (int i = 0; i < subsampleCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void maybeInflateData(ParsableByteArray buffer) {
inflater = new Inflater();
}
if (Util.inflate(buffer, inflatedBuffer, inflater)) {
buffer.reset(inflatedBuffer.data, inflatedBuffer.limit());
buffer.reset(inflatedBuffer.getData(), inflatedBuffer.limit());
} // else assume data is not compressed.
}
}
Expand Down Expand Up @@ -183,7 +183,7 @@ private void parseBitmapSection(ParsableByteArray buffer, int sectionLength) {
int limit = bitmapData.limit();
if (position < limit && sectionLength > 0) {
int bytesToRead = Math.min(sectionLength, limit - position);
buffer.readBytes(bitmapData.data, position, bytesToRead);
buffer.readBytes(bitmapData.getData(), position, bytesToRead);
bitmapData.setPosition(position + bytesToRead);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public List<WebvttCssStyle> parseBlock(ParsableByteArray input) {
stringBuilder.setLength(0);
int initialInputPosition = input.getPosition();
skipStyleBlock(input);
styleInput.reset(input.data, input.getPosition());
styleInput.reset(input.getData(), input.getPosition());
styleInput.setPosition(initialInputPosition);

List<WebvttCssStyle> styles = new ArrayList<>();
Expand Down Expand Up @@ -154,7 +154,7 @@ private static String readCueTarget(ParsableByteArray input) {
int limit = input.limit();
boolean cueTargetEndFound = false;
while (position < limit && !cueTargetEndFound) {
char c = (char) input.data[position++];
char c = (char) input.getData()[position++];
cueTargetEndFound = c == ')';
}
return input.readString(--position - input.getPosition()).trim();
Expand Down Expand Up @@ -267,7 +267,7 @@ private static boolean maybeSkipWhitespace(ParsableByteArray input) {
}

private static char peekCharAtPosition(ParsableByteArray input, int position) {
return (char) input.data[position];
return (char) input.getData()[position];
}

@Nullable
Expand Down Expand Up @@ -297,7 +297,7 @@ private static String parsePropertyValue(ParsableByteArray input, StringBuilder
private static boolean maybeSkipComment(ParsableByteArray input) {
int position = input.getPosition();
int limit = input.limit();
byte[] data = input.data;
byte[] data = input.getData();
if (position + 2 <= limit && data[position++] == '/' && data[position++] == '*') {
while (position + 1 < limit) {
char skippedChar = (char) data[position++];
Expand All @@ -320,7 +320,7 @@ private static String parseIdentifier(ParsableByteArray input, StringBuilder str
int limit = input.limit();
boolean identifierEndFound = false;
while (position < limit && !identifierEndFound) {
char c = (char) input.data[position];
char c = (char) input.getData()[position];
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '#'
|| c == '-' || c == '.' || c == '_') {
position++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static Cue parseVttCueBox(ParsableByteArray sampleData, int remainingCue
remainingCueBoxBytes -= BOX_HEADER_SIZE;
int payloadLength = boxSize - BOX_HEADER_SIZE;
String boxPayload =
Util.fromUtf8Bytes(sampleData.data, sampleData.getPosition(), payloadLength);
Util.fromUtf8Bytes(sampleData.getData(), sampleData.getPosition(), payloadLength);
sampleData.skipBytes(payloadLength);
remainingCueBoxBytes -= payloadLength;
if (boxType == TYPE_sttg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private static boolean isProj(ParsableByteArray input) {
final double log2 = Math.log(2.0);
int coordinateCountSizeBits = (int) Math.ceil(Math.log(2.0 * coordinateCount) / log2);

ParsableBitArray bitInput = new ParsableBitArray(input.data);
ParsableBitArray bitInput = new ParsableBitArray(input.getData());
bitInput.setPosition(input.getPosition() * 8);
float[] vertices = new float[vertexCount * 5];
int[] coordinateIndices = new int[5];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ public static boolean checkFrameHeaderFromPeek(

ParsableByteArray scratch = new ParsableByteArray(FlacConstants.MAX_FRAME_HEADER_SIZE);
System.arraycopy(
frameStartBytes, /* srcPos= */ 0, scratch.data, /* destPos= */ 0, /* length= */ 2);
frameStartBytes, /* srcPos= */ 0, scratch.getData(), /* destPos= */ 0, /* length= */ 2);

int totalBytesPeeked =
ExtractorUtil.peekToLength(input, scratch.data, 2, FlacConstants.MAX_FRAME_HEADER_SIZE - 2);
ExtractorUtil.peekToLength(
input, scratch.getData(), 2, FlacConstants.MAX_FRAME_HEADER_SIZE - 2);
scratch.setLimit(totalBytesPeeked);

input.resetPeekPosition();
Expand Down Expand Up @@ -145,7 +146,7 @@ public static long getFirstSampleNumber(
int maxUtf8SampleNumberSize = isBlockSizeVariable ? 7 : 6;
ParsableByteArray scratch = new ParsableByteArray(maxUtf8SampleNumberSize);
int totalBytesPeeked =
ExtractorUtil.peekToLength(input, scratch.data, 0, maxUtf8SampleNumberSize);
ExtractorUtil.peekToLength(input, scratch.getData(), 0, maxUtf8SampleNumberSize);
scratch.setLimit(totalBytesPeeked);
input.resetPeekPosition();

Expand Down Expand Up @@ -325,7 +326,7 @@ private static boolean checkAndReadCrc(ParsableByteArray data, int frameStartPos
int crc = data.readUnsignedByte();
int frameEndPosition = data.getPosition();
int expectedCrc =
Util.crc8(data.data, frameStartPosition, frameEndPosition - 1, /* initialValue= */ 0);
Util.crc8(data.getData(), frameStartPosition, frameEndPosition - 1, /* initialValue= */ 0);
return crc == expectedCrc;
}

Expand Down
Loading

0 comments on commit ce2e6e2

Please sign in to comment.