Class BitArray


  • public final class BitArray
    extends Object
    This class was created to minimize usage of Java BitSet class, as this one is hard to use within specifics of network protocols. Uses network byte order.
    • Constructor Summary

      Constructors 
      Constructor Description
      BitArray​(int size)
      Creates a BitArray with fixed size of bits.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] array()
      Returns the backing byte array of this bitset.
      boolean get​(int index)
      Returns boolean value for a bit on specific position.
      void set​(int index, Boolean value)
      If the value given is TRUE, sets bit on given position.
      byte toByte()
      If possible, returns one byte as backing array.
      void toByteBuf​(io.netty.buffer.ByteBuf buffer)
      Writes backing array to given ByteBuf, even if the backing array is empty, to preserve the number of allocated bits.
      String toString()  
      static BitArray valueOf​(byte info)
      Returns new BitArray with given byte as backing.
      static BitArray valueOf​(byte[] bytes)
      Returns a new BitArray with given byte array as backing.
      static BitArray valueOf​(io.netty.buffer.ByteBuf buffer, int size)
      Returns a new BitArray created from bytes from given ByteBuf.
    • Constructor Detail

      • BitArray

        public BitArray​(int size)
        Creates a BitArray with fixed size of bits. For sizes smaller than 8 the whole byte is allocated.
        Parameters:
        size - Number of bits relevant for this BitArray. Needs to be greater than 0.
    • Method Detail

      • valueOf

        public static BitArray valueOf​(io.netty.buffer.ByteBuf buffer,
                                       int size)
        Returns a new BitArray created from bytes from given ByteBuf.
        Parameters:
        buffer - ByteBuf, whose readerIndex will be moved by minimum number of bytes required for the bit size.
        size - Number of bits to be allocated in BitArray
        Returns:
        new BitArray
      • valueOf

        public static BitArray valueOf​(byte[] bytes)
        Returns a new BitArray with given byte array as backing. array.
        Parameters:
        bytes - byte array
        Returns:
        new BitArray
      • valueOf

        public static BitArray valueOf​(byte info)
        Returns new BitArray with given byte as backing. array.
        Parameters:
        info - unsigned byte
        Returns:
        new BitArray
      • set

        public void set​(int index,
                        Boolean value)
        If the value given is TRUE, sets bit on given position. Checks for null value. Index is counted from the rightmost bit as 0 to size -1 being the leftmost bit.
        Parameters:
        index - position of bit that will be set
        value - Boolean
      • get

        public boolean get​(int index)
        Returns boolean value for a bit on specific position. Index is counted from the rightmost bit as 0 to size -1 being the leftmost bit.
        Parameters:
        index - position of bit
        Returns:
        boolean value
      • array

        public byte[] array()
        Returns the backing byte array of this bitset.
        Returns:
        byte[]
      • toByte

        public byte toByte()
        If possible, returns one byte as backing array.
        Returns:
        byte
      • toByteBuf

        public void toByteBuf​(io.netty.buffer.ByteBuf buffer)
        Writes backing array to given ByteBuf, even if the backing array is empty, to preserve the number of allocated bits.
        Parameters:
        buffer - ByteBuf