/** * Creates a BitArray with fixed size of bits. For sizes smaller than * 8 the whole byte is allocated. * * @param size Number of bits relevant for this BitArray. Needs to be * greater than 0. */ public BitArray(final int size) { Preconditions.checkArgument(size >= 1, "Minimum size is 1 bit."); this.size = size; this.backingArray = new byte[calculateBytes(size)]; this.offset = (calculateBytes(this.size) * Byte.SIZE) - this.size; }
private BitArray(final byte[] backingArray, final int size) { Preconditions.checkNotNull(backingArray, "Byte Array cannot be null"); this.size = size; this.backingArray = (backingArray == null) ? null : backingArray.clone(); this.offset = (calculateBytes(this.size) * Byte.SIZE) - this.size; }
/** * Returns a new BitArray created from bytes from given ByteBuf. * * @param buffer ByteBuf, whose readerIndex will be moved by * minimum number of bytes required for the bit size. * @param size Number of bits to be allocated in BitArray * @return new BitArray */ public static BitArray valueOf(final ByteBuf buffer, final int size) { Preconditions.checkArgument(size >= 1, "Minimum size is 1 bit."); Preconditions.checkNotNull(buffer, "Byte Array cannot be null"); final byte[] b = new byte[calculateBytes(size)]; buffer.readBytes(b, 0, b.length); return new BitArray(b, size); }