FlacFrameReader { public static long getFirstSampleNumber( ExtractorInput input, FlacStreamMetadata flacStreamMetadata) throws IOException, InterruptedException { input.resetPeekPosition(); input.advancePeekPosition(1); byte[] blockingStrategyByte = new byte[1]; input.peekFully(blockingStrategyByte, 0, 1); boolean isBlockSizeVariable = (blockingStrategyByte[0] & 1) == 1; input.advancePeekPosition(2); int maxUtf8SampleNumberSize = isBlockSizeVariable ? 7 : 6; ParsableByteArray scratch = new ParsableByteArray(maxUtf8SampleNumberSize); int totalBytesPeeked = ExtractorUtil.peekToLength(input, scratch.data, 0, maxUtf8SampleNumberSize); scratch.setLimit(totalBytesPeeked); input.resetPeekPosition(); SampleNumberHolder sampleNumberHolder = new SampleNumberHolder(); if (!checkAndReadFirstSampleNumber( scratch, flacStreamMetadata, isBlockSizeVariable, sampleNumberHolder)) { throw new ParserException(); } return sampleNumberHolder.sampleNumber; } 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 getFirstSampleNumber_doesNotUpdateReadPositionAndAlignsPeekPosition() throws Exception { FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame( "flac/bear_one_metadata_block.flac", streamMetadataHolder); long initialReadPosition = input.getPosition(); input.advancePeekPosition(FlacConstants.MAX_FRAME_HEADER_SIZE); FlacFrameReader.getFirstSampleNumber(input, streamMetadataHolder.flacStreamMetadata); assertThat(input.getPosition()).isEqualTo(initialReadPosition); assertThat(input.getPeekPosition()).isEqualTo(input.getPosition()); }
@Test public void getFirstSampleNumber_returnsSampleNumber() throws Exception { FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame( "flac/bear_one_metadata_block.flac", streamMetadataHolder); input.skip(5030); long result = FlacFrameReader.getFirstSampleNumber(input, streamMetadataHolder.flacStreamMetadata); assertThat(result).isEqualTo(4096); }