DatagramReader { public int read(final int numBits) { if (numBits < 0 || numBits > Integer.SIZE) { throw new IllegalArgumentException("bits must be in range 0 ... 32!"); } int bits = 0; if (currentBitIndex < 0 && (numBits & 0x7) == 0) { for (int i = 0; i < numBits; i += 8) { bits <<= 8; bits |= readByte(); } } else { for (int i = numBits - 1; i >= 0; i--) { if (currentBitIndex < 0) { readCurrentByte(); } boolean bit = (currentByte >> currentBitIndex & 1) != 0; if (bit) { bits |= (1 << i); } --currentBitIndex; } } return bits; } DatagramReader(final byte[] byteArray);  DatagramReader(final byte[] byteArray, boolean copy);  DatagramReader(final byte[] byteArray, int offset, int length);  DatagramReader(final ByteArrayInputStream byteStream); void mark(); void reset(); void close(); long readLong(final int numBits); int read(final int numBits); byte[] readBytes(final int count); byte readNextByte(); byte[] readBytesLeft(); boolean bytesAvailable(); boolean bytesAvailable(final int expectedBytes); int bitsLeft(); DatagramReader createRangeReader(int count); ByteArrayInputStream createRangeInputStream(int count);  }
@Test (expected = IllegalArgumentException.class) public void testReadExceedsAvailableBytes() { givenABuffer(new byte[] { 0x01, 0x02 }); reader.read(24); }
@Test public void testReadInt() { givenABuffer(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }); int value = reader.read(24); assertEquals(0x010203, value); value = reader.read(16); assertEquals(0x0405, value); }