ExtractorUtil { public static int peekToLength(ExtractorInput input, byte[] target, int offset, int length) throws IOException, InterruptedException { int totalBytesPeeked = 0; while (totalBytesPeeked < length) { int bytesPeeked = input.peek(target, offset + totalBytesPeeked, length - totalBytesPeeked); if (bytesPeeked == C.RESULT_END_OF_INPUT) { break; } totalBytesPeeked += bytesPeeked; } return totalBytesPeeked; } private  ExtractorUtil(); static int peekToLength(ExtractorInput input, byte[] target, int offset, int length);  }
@Test public void testPeekToLengthEndNotReached() throws Exception { FakeDataSource testDataSource = new FakeDataSource(); testDataSource .getDataSet() .newDefaultData() .appendReadData(Arrays.copyOfRange(TEST_DATA, 0, 3)) .appendReadData(Arrays.copyOfRange(TEST_DATA, 3, 6)) .appendReadData(Arrays.copyOfRange(TEST_DATA, 6, 9)); testDataSource.open(new DataSpec(Uri.parse(TEST_URI))); ExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET); byte[] target = new byte[TEST_DATA.length]; int offset = 2; int length = 4; int bytesPeeked = ExtractorUtil.peekToLength(input, target, offset, length); assertThat(bytesPeeked).isEqualTo(length); assertThat(input.getPeekPosition()).isEqualTo(length); assertThat(Arrays.copyOfRange(target, offset, offset + length)) .isEqualTo(Arrays.copyOf(TEST_DATA, length)); }
@Test public void testPeekToLengthEndReached() throws Exception { FakeDataSource testDataSource = new FakeDataSource(); testDataSource .getDataSet() .newDefaultData() .appendReadData(Arrays.copyOfRange(TEST_DATA, 0, 3)) .appendReadData(Arrays.copyOfRange(TEST_DATA, 3, 6)) .appendReadData(Arrays.copyOfRange(TEST_DATA, 6, 9)); testDataSource.open(new DataSpec(Uri.parse(TEST_URI))); ExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET); byte[] target = new byte[TEST_DATA.length]; int offset = 0; int length = TEST_DATA.length + 1; int bytesPeeked = ExtractorUtil.peekToLength(input, target, offset, length); assertThat(bytesPeeked).isEqualTo(TEST_DATA.length); assertThat(input.getPeekPosition()).isEqualTo(TEST_DATA.length); assertThat(target).isEqualTo(TEST_DATA); }