FlacFrameReader { public static boolean checkAndReadFrameHeader( ParsableByteArray data, FlacStreamMetadata flacStreamMetadata, int frameStartMarker, SampleNumberHolder sampleNumberHolder) { int frameStartPosition = data.getPosition(); long frameHeaderBytes = data.readUnsignedInt(); if (frameHeaderBytes >>> 16 != frameStartMarker) { return false; } boolean isBlockSizeVariable = (frameHeaderBytes >>> 16 & 1) == 1; int blockSizeKey = (int) (frameHeaderBytes >> 12 & 0xF); int sampleRateKey = (int) (frameHeaderBytes >> 8 & 0xF); int channelAssignmentKey = (int) (frameHeaderBytes >> 4 & 0xF); int bitsPerSampleKey = (int) (frameHeaderBytes >> 1 & 0x7); boolean reservedBit = (frameHeaderBytes & 1) == 1; return checkChannelAssignment(channelAssignmentKey, flacStreamMetadata) && checkBitsPerSample(bitsPerSampleKey, flacStreamMetadata) && !reservedBit && checkAndReadFirstSampleNumber( data, flacStreamMetadata, isBlockSizeVariable, sampleNumberHolder) && checkAndReadBlockSizeSamples(data, flacStreamMetadata, blockSizeKey) && checkAndReadSampleRate(data, flacStreamMetadata, sampleRateKey) && checkAndReadCrc(data, frameStartPosition); } 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 checkAndReadFrameHeader_validData_updatesPosition() throws Exception { FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame( "flac/bear_one_metadata_block.flac", streamMetadataHolder); int frameStartMarker = FlacMetadataReader.getFrameStartMarker(input); ParsableByteArray scratch = new ParsableByteArray(FlacConstants.MAX_FRAME_HEADER_SIZE); input.read(scratch.data, 0, FlacConstants.MAX_FRAME_HEADER_SIZE); FlacFrameReader.checkAndReadFrameHeader( scratch, streamMetadataHolder.flacStreamMetadata, frameStartMarker, new SampleNumberHolder()); assertThat(scratch.getPosition()).isEqualTo(FlacConstants.MIN_FRAME_HEADER_SIZE); }
@Test public void checkAndReadFrameHeader_validData_isTrue() throws Exception { FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame( "flac/bear_one_metadata_block.flac", streamMetadataHolder); int frameStartMarker = FlacMetadataReader.getFrameStartMarker(input); ParsableByteArray scratch = new ParsableByteArray(FlacConstants.MAX_FRAME_HEADER_SIZE); input.read(scratch.data, 0, FlacConstants.MAX_FRAME_HEADER_SIZE); boolean result = FlacFrameReader.checkAndReadFrameHeader( scratch, streamMetadataHolder.flacStreamMetadata, frameStartMarker, new SampleNumberHolder()); assertThat(result).isTrue(); }
@Test public void checkAndReadFrameHeader_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); ParsableByteArray scratch = new ParsableByteArray(FlacConstants.MAX_FRAME_HEADER_SIZE); input.read(scratch.data, 0, FlacConstants.MAX_FRAME_HEADER_SIZE); SampleNumberHolder sampleNumberHolder = new SampleNumberHolder(); FlacFrameReader.checkAndReadFrameHeader( scratch, streamMetadataHolder.flacStreamMetadata, frameStartMarker, sampleNumberHolder); assertThat(sampleNumberHolder.sampleNumber).isEqualTo(4096); }
@Test public void checkAndReadFrameHeader_invalidData_isFalse() throws Exception { FlacStreamMetadataHolder streamMetadataHolder = new FlacStreamMetadataHolder( null); ExtractorInput input = buildExtractorInputReadingFromFirstFrame( "flac/bear_one_metadata_block.flac", streamMetadataHolder); ParsableByteArray scratch = new ParsableByteArray(FlacConstants.MAX_FRAME_HEADER_SIZE); input.read(scratch.data, 0, FlacConstants.MAX_FRAME_HEADER_SIZE); boolean result = FlacFrameReader.checkAndReadFrameHeader( scratch, streamMetadataHolder.flacStreamMetadata, -1, new SampleNumberHolder()); assertThat(result).isFalse(); }