BitBufferHelper { public static byte[] getBits(byte[] data, int startOffset, int numBits) throws BufferException { int startByteOffset = 0; int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE; final int extraOffsetBits = startOffset % NetUtils.NUM_BITS_IN_A_BYTE; int numBytes = numBits % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1 + numBits / NetUtils.NUM_BITS_IN_A_BYTE : numBits / NetUtils.NUM_BITS_IN_A_BYTE; startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE; byte[] bytes = new byte[numBytes]; if (numBits == 0) { return bytes; } checkExceptions(data, startOffset, numBits); if (extraOffsetBits == 0) { if (extranumBits == 0) { System.arraycopy(data, startByteOffset, bytes, 0, numBytes); return bytes; } else { System.arraycopy(data, startByteOffset, bytes, 0, numBytes - 1); bytes[numBytes - 1] = (byte) (data[startByteOffset + numBytes - 1] & getMSBMask(extranumBits)); } } else { int index; int valfromnext; for (index = 0; index < numBits / NetUtils.NUM_BITS_IN_A_BYTE; index++) { int valfromcurr = data[startByteOffset + index] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits); valfromnext = data[startByteOffset + index + 1] & getMSBMask(extraOffsetBits); bytes[index] = (byte) (valfromcurr << extraOffsetBits | valfromnext >> NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits); } if (extranumBits != 0) { if (extranumBits < NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) { valfromnext = (byte) (data[startByteOffset + index] & getMSBMask(extranumBits) >> extraOffsetBits); bytes[index] = (byte) (valfromnext << extraOffsetBits); } else if (extranumBits == NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) { int valfromcurr = data[startByteOffset + index] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits); bytes[index] = (byte) (valfromcurr << extraOffsetBits); } else { int valfromcurr = data[startByteOffset + index] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits); valfromnext = data[startByteOffset + index + 1] & getMSBMask(extranumBits - (NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits)); bytes[index] = (byte) (valfromcurr << extraOffsetBits | valfromnext >> NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits); } } } return shiftBitsToLSB(bytes, numBits); } private  BitBufferHelper(); static byte getByte(byte[] data); static short getShort(byte[] data, int numBits); static short getShort(byte[] data); static int getInt(byte[] data, int numBits); static int getInt(byte[] data); static long getLong(byte[] data, int numBits); static long getLong(byte[] data); static byte[] getBits(byte[] data, int startOffset, int numBits); static void setByte(byte[] data, byte input, int startOffset, int numBits); static void setBytes(byte[] data, byte[] input, int startOffset, int numBits); static int getMSBMask(int numBits); static int getLSBMask(int numBits); static long toNumber(byte[] array); static long toNumber(@Nonnull byte[] array, int numBits); static byte[] toByteArray(Number input); static byte[] toByteArray(Number input, int numBits); static byte[] shiftBitsToMSB(byte[] inputBytes, int numBits); static byte[] shiftBitsToLSB(byte[] inputBytes, int numBits); static void insertBits(byte[] data, byte[] inputdataLSB, int startOffset, int numBits); static void checkExceptions(byte[] data, int startOffset, int numBits); static final long BYTE_MASK; }
@Test public void testGetBits() throws Exception { byte[] data = { 10, 12, 14, 20, 55, 69, 82, 97, 109, 117, 127, -50 }; byte[] bits; bits = BitBufferHelper.getBits(data, 88, 8); Assert.assertTrue(bits[0] == -50); bits = BitBufferHelper.getBits(data, 8, 16); Assert.assertTrue(bits[0] == 12); Assert.assertTrue(bits[1] == 14); bits = BitBufferHelper.getBits(data, 32, 32); Assert.assertTrue(bits[0] == 55); Assert.assertTrue(bits[1] == 69); Assert.assertTrue(bits[2] == 82); Assert.assertTrue(bits[3] == 97); bits = BitBufferHelper.getBits(data, 16, 48); Assert.assertTrue(bits[0] == 14); Assert.assertTrue(bits[1] == 20); Assert.assertTrue(bits[2] == 55); Assert.assertTrue(bits[3] == 69); Assert.assertTrue(bits[4] == 82); Assert.assertTrue(bits[5] == 97); bits = BitBufferHelper.getBits(data, 40, 7); Assert.assertTrue(bits[0] == 34); bits = BitBufferHelper.getBits(data, 8, 13); Assert.assertTrue(bits[0] == 1); Assert.assertTrue(bits[1] == -127); bits = BitBufferHelper.getBits(data, 32, 28); Assert.assertTrue(bits[0] == 3); Assert.assertTrue(bits[1] == 116); Assert.assertTrue(bits[2] == 85); Assert.assertTrue(bits[3] == 38); bits = BitBufferHelper.getBits(data, 16, 41); Assert.assertTrue(bits[0] == 0); Assert.assertTrue(bits[1] == 28); Assert.assertTrue(bits[2] == 40); Assert.assertTrue(bits[3] == 110); Assert.assertTrue(bits[4] == -118); Assert.assertTrue(bits[5] == -92); bits = BitBufferHelper.getBits(data, 3, 7); Assert.assertTrue(bits[0] == 40); bits = BitBufferHelper.getBits(data, 13, 16); Assert.assertTrue(bits[0] == -127); Assert.assertTrue(bits[1] == -62); bits = BitBufferHelper.getBits(data, 5, 32); Assert.assertTrue(bits[0] == 65); Assert.assertTrue(bits[1] == -127); Assert.assertTrue(bits[2] == -62); Assert.assertTrue(bits[3] == -122); bits = BitBufferHelper.getBits(data, 23, 48); Assert.assertTrue(bits[0] == 10); Assert.assertTrue(bits[1] == 27); Assert.assertTrue(bits[2] == -94); Assert.assertTrue(bits[3] == -87); Assert.assertTrue(bits[4] == 48); Assert.assertTrue(bits[5] == -74); bits = BitBufferHelper.getBits(data, 66, 9); Assert.assertTrue(bits[0] == 1); Assert.assertTrue(bits[1] == 107); bits = BitBufferHelper.getBits(data, 13, 15); Assert.assertTrue(bits[0] == 64); Assert.assertTrue(bits[1] == -31); bits = BitBufferHelper.getBits(data, 5, 29); Assert.assertTrue(bits[0] == 8); Assert.assertTrue(bits[1] == 48); Assert.assertTrue(bits[2] == 56); Assert.assertTrue(bits[3] == 80); bits = BitBufferHelper.getBits(data, 31, 43); Assert.assertTrue(bits[0] == 0); Assert.assertTrue(bits[1] == -35); Assert.assertTrue(bits[2] == 21); Assert.assertTrue(bits[3] == 73); Assert.assertTrue(bits[4] == -123); Assert.assertTrue(bits[5] == -75); bits = BitBufferHelper.getBits(data, 4, 12); Assert.assertTrue(bits[0] == 10); Assert.assertTrue(bits[1] == 12); byte[] data1 = { 0, 8 }; bits = BitBufferHelper.getBits(data1, 7, 9); Assert.assertTrue(bits[0] == 0); Assert.assertTrue(bits[1] == 8); byte[] data2 = { 2, 8 }; bits = BitBufferHelper.getBits(data2, 0, 7); Assert.assertTrue(bits[0] == 1); bits = BitBufferHelper.getBits(data2, 7, 9); Assert.assertTrue(bits[0] == 0); Assert.assertTrue(bits[1] == 8); }
@Test public void testGetBytes() throws Exception { byte[] data = { 108, 96, 125, -112, 5, 6, 108, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 }; Assert.assertTrue(BitBufferHelper.getBits(data, 0, 8)[0] == 108); Assert.assertTrue(BitBufferHelper.getBits(data, 8, 8)[0] == 96); byte[] bits = BitBufferHelper.getBits(data, 0, 10); Assert.assertTrue(bits[0] == 1); Assert.assertTrue(bits[1] == -79); bits = BitBufferHelper.getBits(data, 3, 8); Assert.assertTrue(bits[0] == 99); }