### Question:
BitReaderBuffer { public int readBits(int i) { byte b = buffer.get(initialPos + position / 8); int v = b < 0 ? b + 256 : b; int left = 8 - position % 8; int rc; if (i <= left) { rc = (v << (position % 8) & 0xFF) >> ((position % 8) + (left - i)); position += i; } else { int now = left; int then = i - left; rc = readBits(now); rc = rc << then; rc += readBits(then); } ((Buffer)buffer).position(initialPos + (int) Math.ceil((double) position / 8)); return rc; } BitReaderBuffer(ByteBuffer buffer); boolean readBool(); int readBits(int i); int getPosition(); int byteSync(); int remainingBits();  }### Answer:
@Test public void readFromTheMiddle() { ByteBuffer b = ByteBuffer.wrap(new byte[]{0, -1}); b.get(); BitReaderBuffer brb = new BitReaderBuffer(b); Assert.assertEquals(15, brb.readBits(4)); Assert.assertEquals(15, brb.readBits(4)); }
@Test public void testRead_8() { BitReaderBuffer bitReaderBuffer = new BitReaderBuffer(testSequence); Assert.assertEquals(15, bitReaderBuffer.readBits(4)); Assert.assertEquals(15, bitReaderBuffer.readBits(4)); Assert.assertEquals(0, bitReaderBuffer.readBits(4)); Assert.assertEquals(0, bitReaderBuffer.readBits(4)); }
@Test public void testReadCrossByte() { BitReaderBuffer bitReaderBuffer = new BitReaderBuffer(testSequence); Assert.assertEquals(31, bitReaderBuffer.readBits(5)); Assert.assertEquals(14, bitReaderBuffer.readBits(4)); Assert.assertEquals(0, bitReaderBuffer.readBits(3)); Assert.assertEquals(0, bitReaderBuffer.readBits(4)); }
@Test public void testReadMultiByte() { BitReaderBuffer bitReaderBuffer = new BitReaderBuffer(testSequence); Assert.assertEquals(510, bitReaderBuffer.readBits(9)); }
@Test public void testReadMultiByte2() { BitReaderBuffer bitReaderBuffer = new BitReaderBuffer(testSequence); Assert.assertEquals(0x1fe01, bitReaderBuffer.readBits(17)); }