@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); }
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); }
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); } }
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(); }
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); }
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; }