FlacFrameReader { public static boolean checkFrameHeaderFromPeek( ExtractorInput input, FlacStreamMetadata flacStreamMetadata, int frameStartMarker, SampleNumberHolder sampleNumberHolder) throws IOException, InterruptedException { long originalPeekPosition = input.getPeekPosition(); byte[] frameStartBytes = new byte[2]; input.peekFully(frameStartBytes, 0, 2); int frameStart = (frameStartBytes[0] & 0xFF) << 8 | (frameStartBytes[1] & 0xFF); if (frameStart != frameStartMarker) { input.resetPeekPosition(); input.advancePeekPosition((int) (originalPeekPosition - input.getPosition())); return false; } ParsableByteArray scratch = new ParsableByteArray(FlacConstants.MAX_FRAME_HEADER_SIZE); System.arraycopy( frameStartBytes, 0, scratch.data, 0, 2); int totalBytesPeeked = ExtractorUtil.peekToLength(input, scratch.data, 2, FlacConstants.MAX_FRAME_HEADER_SIZE - 2); scratch.setLimit(totalBytesPeeked); input.resetPeekPosition(); input.advancePeekPosition((int) (originalPeekPosition - input.getPosition())); return checkAndReadFrameHeader( scratch, flacStreamMetadata, frameStartMarker, sampleNumberHolder); } private  FlacFrameReader(); static boolean checkAndReadFrameHeader(
      ParsableByteArray data,
      FlacStreamMetadata flacStreamMetadata,
      int frameStartMarker,
      SampleNumberHolder sampleNumberHolder); static boolean checkFrameHeaderFromPeek(
      ExtractorInput input,
      FlacStreamMetadata flacStreamMetadata,
      int frameStartMarker,
      SampleNumberHolder sampleNumberHolder); static long getFirstSampleNumber(
      ExtractorInput input, FlacStreamMetadata flacStreamMetadata); static int readFrameBlockSizeSamplesFromKey(ParsableByteArray data, int blockSizeKey);  }
@Test public void checkFrameHeaderFromPeek_validData_doesNotUpdatePositions() throws Exception { String file = "flac/bear_one_metadata_block.flac"; FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame(file, streamMetadataHolder); int frameStartMarker = FlacMetadataReader.getFrameStartMarker(input); long peekPosition = input.getPosition(); input = buildExtractorInput(file); input.advancePeekPosition((int) peekPosition); FlacFrameReader.checkFrameHeaderFromPeek( input, streamMetadataHolder.flacStreamMetadata, frameStartMarker, new SampleNumberHolder()); assertThat(input.getPosition()).isEqualTo(0); assertThat(input.getPeekPosition()).isEqualTo(peekPosition); }
@Test public void checkFrameHeaderFromPeek_validData_isTrue() throws Exception { FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame( "flac/bear_one_metadata_block.flac", streamMetadataHolder); int frameStartMarker = FlacMetadataReader.getFrameStartMarker(input); boolean result = FlacFrameReader.checkFrameHeaderFromPeek( input, streamMetadataHolder.flacStreamMetadata, frameStartMarker, new SampleNumberHolder()); assertThat(result).isTrue(); }
@Test public void checkFrameHeaderFromPeek_validData_writesSampleNumber() throws Exception { FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame( "flac/bear_one_metadata_block.flac", streamMetadataHolder); int frameStartMarker = FlacMetadataReader.getFrameStartMarker(input); input.skip(5030); SampleNumberHolder sampleNumberHolder = new SampleNumberHolder(); FlacFrameReader.checkFrameHeaderFromPeek( input, streamMetadataHolder.flacStreamMetadata, frameStartMarker, sampleNumberHolder); assertThat(sampleNumberHolder.sampleNumber).isEqualTo(4096); }
@Test public void checkFrameHeaderFromPeek_invalidData_isFalse() throws Exception { FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame( "flac/bear_one_metadata_block.flac", streamMetadataHolder); boolean result = FlacFrameReader.checkFrameHeaderFromPeek( input, streamMetadataHolder.flacStreamMetadata, -1, new SampleNumberHolder()); assertThat(result).isFalse(); }
@Test public void checkFrameHeaderFromPeek_invalidData_doesNotUpdatePositions() throws Exception { String file = "flac/bear_one_metadata_block.flac"; FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame(file, streamMetadataHolder); long peekPosition = input.getPosition(); input = buildExtractorInput(file); input.advancePeekPosition((int) peekPosition); FlacFrameReader.checkFrameHeaderFromPeek( input, streamMetadataHolder.flacStreamMetadata, -1, new SampleNumberHolder()); assertThat(input.getPosition()).isEqualTo(0); assertThat(input.getPeekPosition()).isEqualTo(peekPosition); }