/** * Bits are expected to be stored in the input byte array from LSB * * @param byte[] - data to set the input byte * @param byte[] - input bytes to be inserted * @param startOffset - offset of data[] to start inserting byte from * @param numBits - number of bits of input to be inserted into data[] * @return void * @throws BufferException when the startOffset and numBits parameters are not congruent with data * and input buffers' size */ public static void setBytes(byte[] data, byte[] input, int startOffset, int numBits) throws BufferException { checkExceptions(data, startOffset, numBits); insertBits(data, input, startOffset, numBits); }
/** * Reads the specified number of bits from the passed byte array starting to read from the * specified offset The bits read are stored in a byte array which size is dictated by the number * of bits to be stored. The bits are stored in the byte array LSB aligned. * * <p>Ex. Read 7 bits at offset 10 0 9 10 16 17 0101000010 | 0000101 | 1111001010010101011 will be * returned as {0,0,0,0,0,1,0,1} * * @param byte[] data * @param int startOffset - offset to start fetching bits from data from * @param int numBits - number of bits to be fetched from data * @return byte [] - LSB aligned bits * @throws BufferException when the startOffset and numBits parameters are not congruent with the * data buffer size */ public static byte[] getBits(byte[] data, int startOffset, int numBits) throws BufferException { int startByteOffset = 0; int valfromcurr, valfromnext; int extranumBits = numBits % NetUtils.NumBitsInAByte; int extraOffsetBits = startOffset % NetUtils.NumBitsInAByte; int numBytes = (numBits % NetUtils.NumBitsInAByte != 0) ? 1 + numBits / NetUtils.NumBitsInAByte : numBits / NetUtils.NumBitsInAByte; byte[] shiftedBytes = new byte[numBytes]; startByteOffset = startOffset / NetUtils.NumBitsInAByte; 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) ((int) data[startByteOffset + numBytes - 1] & getMSBMask(extranumBits)); } } else { int i; for (i = 0; i < numBits / NetUtils.NumBitsInAByte; i++) { // Reading numBytes starting from offset valfromcurr = (data[startByteOffset + i]) & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits); valfromnext = (data[startByteOffset + i + 1]) & getMSBMask(extraOffsetBits); bytes[i] = (byte) (valfromcurr << (extraOffsetBits) | (valfromnext >> (NetUtils.NumBitsInAByte - extraOffsetBits))); } // Now adding the rest of the bits if any if (extranumBits != 0) { if (extranumBits < (NetUtils.NumBitsInAByte - extraOffsetBits)) { valfromnext = (byte) (data[startByteOffset + i] & ((getMSBMask(extranumBits)) >> extraOffsetBits)); bytes[i] = (byte) (valfromnext << extraOffsetBits); } else if (extranumBits == (NetUtils.NumBitsInAByte - extraOffsetBits)) { valfromcurr = (data[startByteOffset + i]) & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits); bytes[i] = (byte) (valfromcurr << extraOffsetBits); } else { valfromcurr = (data[startByteOffset + i]) & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits); valfromnext = (data[startByteOffset + i + 1]) & (getMSBMask(extranumBits - (NetUtils.NumBitsInAByte - extraOffsetBits))); bytes[i] = (byte) (valfromcurr << (extraOffsetBits) | (valfromnext >> (NetUtils.NumBitsInAByte - extraOffsetBits))); } } } // Aligns the bits to LSB shiftedBytes = shiftBitsToLSB(bytes, numBits); return shiftedBytes; }