BitReader { public Optional<Long> findStart() { if (!isByteBoundary()) { discardCurrentByte(); } int startPos = byteBuf.readerIndex(); startCode.init(); int position = byteBuf.forEachByte(startCode); if (position == BYTE_BUF_EOF) { return Optional.empty(); } skipBytes(position - startPos - START_CODE_PREFIX_SIZE); long startCodeSequence; try { startCodeSequence = readBits(START_CODE_BIT_SIZE); } catch (EOFException e) { LOGGER.debug( "read past end of file, but should never happen because the code already determined that the bytes exist", e); return Optional.empty(); } assert (startCodeSequence & 0xffffff00) == 0x00000100 : "start code does not follow the expected format"; return Optional.of(startCodeSequence & 0xFF); } BitReader(ByteBuf byteBuf); boolean testBit(); long readableBits(); void skipBits(int numberOfBits); byte readBit(); long readBits(int numberOfBits); boolean isByteBoundary(); Optional<Long> findStart();  }
@Test public void testFindStart1() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] {0, 0, 1, 1}); BitReader bitReader = new BitReader(byteBuf); Optional<Long> start = bitReader.findStart(); assertThat(start.isPresent(), is(true)); assertThat(start.get(), is(1L)); }