BitReader { public long readBits(int numberOfBits) throws EOFException { Validate.inclusiveBetween(1, 32, numberOfBits, "numberOfBits must be [1,32]"); if (numberOfBits == 1) { return readBit(); } long result = 0; for (int i = 0; i < numberOfBits; i++) { result |= readBit() << (numberOfBits - i - 1); } return result; } 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 testReadBits1() throws EOFException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] {(byte) 0b10111010}); BitReader bitReader = new BitReader(byteBuf); assertThat(bitReader.readBits(1), is((long) 1)); assertThat(bitReader.readBits(1), is((long) 0)); assertThat(bitReader.readBits(1), is((long) 1)); assertThat(bitReader.readBits(1), is((long) 1)); assertThat(bitReader.readBits(1), is((long) 1)); assertThat(bitReader.readBits(1), is((long) 0)); assertThat(bitReader.readBits(1), is((long) 1)); assertThat(bitReader.readBits(1), is((long) 0)); }
@Test public void testReadBits2() throws EOFException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] {(byte) 0b10111010}); BitReader bitReader = new BitReader(byteBuf); assertThat(bitReader.readBits(2), is((long) 0b10)); assertThat(bitReader.readBits(2), is((long) 0b11)); assertThat(bitReader.readBits(2), is((long) 0b10)); assertThat(bitReader.readBits(2), is((long) 0b10)); }
@Test public void testReadBits3() throws EOFException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] {(byte) 0b10111010}); BitReader bitReader = new BitReader(byteBuf); assertThat(bitReader.readBits(3), is((long) 0b101)); assertThat(bitReader.readBits(3), is((long) 0b110)); assertThat(bitReader.readBits(2), is((long) 0b10)); }
@Test public void testReadBits4() throws EOFException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] {(byte) 0b10111010}); BitReader bitReader = new BitReader(byteBuf); assertThat(bitReader.readBits(4), is((long) 0b1011)); assertThat(bitReader.readBits(4), is((long) 0b1010)); }
@Test public void testReadBits5() throws EOFException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] {(byte) 0b10111010}); BitReader bitReader = new BitReader(byteBuf); assertThat(bitReader.readBits(5), is((long) 0b10111)); assertThat(bitReader.readBits(3), is((long) 0b010)); }
@Test public void testReadBits6() throws EOFException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] {(byte) 0b10111010}); BitReader bitReader = new BitReader(byteBuf); assertThat(bitReader.readBits(6), is((long) 0b101110)); assertThat(bitReader.readBits(2), is((long) 0b10)); }
@Test public void testReadBits7() throws EOFException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] {(byte) 0b10111010}); BitReader bitReader = new BitReader(byteBuf); assertThat(bitReader.readBits(7), is((long) 0b1011101)); assertThat(bitReader.readBits(1), is((long) 0b0)); }
@Test public void testReadBits8() throws EOFException { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] {(byte) 0b10111010}); BitReader bitReader = new BitReader(byteBuf); assertThat(bitReader.readBits(8), is((long) 0b10111010)); }