VarintReader { public long readUnsignedVarint(ExtractorInput input, boolean allowEndOfInput, boolean removeLengthMask, int maximumAllowedLength) throws IOException, InterruptedException { if (state == STATE_BEGIN_READING) { if (!input.readFully(scratch, 0, 1, allowEndOfInput)) { return C.RESULT_END_OF_INPUT; } int firstByte = scratch[0] & 0xFF; length = parseUnsignedVarintLength(firstByte); if (length == C.LENGTH_UNSET) { throw new IllegalStateException("No valid varint length mask found"); } state = STATE_READ_CONTENTS; } if (length > maximumAllowedLength) { state = STATE_BEGIN_READING; return C.RESULT_MAX_LENGTH_EXCEEDED; } if (length != 1) { input.readFully(scratch, 1, length - 1); } state = STATE_BEGIN_READING; return assembleVarint(scratch, length, removeLengthMask); } VarintReader(); void reset(); long readUnsignedVarint(ExtractorInput input, boolean allowEndOfInput,
      boolean removeLengthMask, int maximumAllowedLength); int getLastLength(); static int parseUnsignedVarintLength(int firstByte); static long assembleVarint(byte[] varintBytes, int varintLength,
      boolean removeLengthMask);  }
@Test public void testReadVarintEndOfInputAtStart() throws IOException, InterruptedException { VarintReader reader = new VarintReader(); ExtractorInput input = new FakeExtractorInput.Builder() .setSimulateUnknownLength(true) .build(); long result = reader.readUnsignedVarint(input, true, false, 8); assertThat(result).isEqualTo(RESULT_END_OF_INPUT); try { reader.readUnsignedVarint(input, false, false, 8); fail(); } catch (EOFException e) { } }
@Test public void testReadVarintExceedsMaximumAllowedLength() throws IOException, InterruptedException { VarintReader reader = new VarintReader(); ExtractorInput input = new FakeExtractorInput.Builder() .setData(DATA_8_BYTE_0) .setSimulateUnknownLength(true) .build(); long result = reader.readUnsignedVarint(input, false, true, 4); assertThat(result).isEqualTo(RESULT_MAX_LENGTH_EXCEEDED); }