Class BitBufferHelper

java.lang.Object
org.opendaylight.openflowplugin.libraries.liblldp.BitBufferHelper

public abstract class BitBufferHelper extends Object
BitBufferHelper class that provides utility methods to - fetch specific bits from a serialized stream of bits - convert bits to primitive data type - like short, int, long - store bits in specified location in stream of bits - convert primitive data types to stream of bits.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    checkExceptions(byte[] data, int startOffset, int numBits)
    Checks for overflow and underflow exceptions.
    static byte @NonNull []
    getBits(byte[] data, int startOffset, int 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.
    static byte
    getByte(byte[] data)
    Returns the first byte from the byte array.
    static int
    getInt(byte[] data)
    Returns the int value for the byte array passed.
    static int
    getInt(byte[] data, int numBits)
    Returns the int value for the last numBits of the byte array passed.
    static long
    getLong(byte[] data)
    Returns the long value for the byte array passed.
    static long
    getLong(byte[] data, int numBits)
    Returns the long value for the last numBits of the byte array passed.
    static int
    getLSBMask(int numBits)
    Returns numBits 1's in the LSB position.
    static int
    getMSBMask(int numBits)
    Returns numBits 1's in the MSB position.
    static short
    getShort(byte[] data)
    Returns the short value for the byte array passed.
    static short
    getShort(byte[] data, int numBits)
    Returns the short value for the last numBits of the byte array passed.
    static void
    setBytes(byte[] data, byte[] input, int startOffset, int numBits)
    Deprecated.
    Use copyBitsFromLsb(byte[], byte[], int, int) instead.
    static byte[]
    shiftBitsToLSB(byte[] inputBytes, int numBits)
    It aligns the first numBits bits to the right end of the byte array preceding them with numBits % 8 zero bits.
    static byte[]
    shiftBitsToMSB(byte[] inputBytes, int numBits)
    Takes an LSB aligned byte array and returned the LSB numBits in a MSB aligned byte array.
    static byte[]
    Accepts a number as input and returns its value in byte form in LSB aligned form example: input = 5000 [1001110001000] bytes = 19, -120 [00010011] [10001000].
    static byte[]
    toByteArray(Number input, int numBits)
    Accepts a number as input and returns its value in byte form in MSB aligned form example: input = 5000 [1001110001000] bytes = -114, 64 [10011100] [01000000].
    static long
    toNumber(byte[] array)
    Returns the numerical value of the byte array passed.
    static long
    toNumber(byte[] array, int numBits)
    Returns the numerical value of the last numBits (LSB bits) of the byte array passed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • BitBufferHelper

      public BitBufferHelper()
  • Method Details

    • getByte

      public static byte getByte(byte[] data)
      Returns the first byte from the byte array.
      Returns:
      byte value
    • getShort

      public static short getShort(byte[] data)
      Returns the short value for the byte array passed. Size of byte array is restricted to Short.SIZE
      Returns:
      short value
    • getShort

      public static short getShort(byte[] data, int numBits)
      Returns the short value for the last numBits of the byte array passed. Size of numBits is restricted to Short.SIZE
      Returns:
      short - the short value of byte array
    • getInt

      public static int getInt(byte[] data)
      Returns the int value for the byte array passed. Size of byte array is restricted to Integer.SIZE
      Returns:
      int - the integer value of byte array
    • getInt

      public static int getInt(byte[] data, int numBits)
      Returns the int value for the last numBits of the byte array passed. Size of numBits is restricted to Integer.SIZE
      Returns:
      int - the integer value of byte array
    • getLong

      public static long getLong(byte[] data)
      Returns the long value for the byte array passed. Size of byte array is restricted to Long.SIZE
      Returns:
      long - the integer value of byte array
    • getLong

      public static long getLong(byte[] data, int numBits)
      Returns the long value for the last numBits of the byte array passed. Size of numBits is restricted to Long.SIZE
      Returns:
      long - the integer value of byte array
    • getBits

      public static byte @NonNull [] getBits(byte[] data, int startOffset, int numBits) throws BufferException
      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.

      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}

      Parameters:
      startOffset - - offset to start fetching bits from data from
      numBits - - number of bits to be fetched from data
      Returns:
      byte [] - LSB aligned bits
      Throws:
      BufferException - when the startOffset and numBits parameters are not congruent with the data buffer size
    • setBytes

      @Deprecated public static void setBytes(byte[] data, byte[] input, int startOffset, int numBits) throws BufferException
      Deprecated.
      Use copyBitsFromLsb(byte[], byte[], int, int) instead.
      Bits are expected to be stored in the input byte array from LSB.
      Parameters:
      data - to set the input byte
      input - bytes to be inserted
      startOffset - offset of data[] to start inserting byte from
      numBits - number of bits of input to be inserted into data[]
      Throws:
      BufferException - when the startOffset and numBits parameters are not congruent with data and input buffers' size
    • getMSBMask

      public static int getMSBMask(int numBits)
      Returns numBits 1's in the MSB position.
    • getLSBMask

      public static int getLSBMask(int numBits)
      Returns numBits 1's in the LSB position.
    • toNumber

      public static long toNumber(byte[] array)
      Returns the numerical value of the byte array passed.
      Returns:
      long - numerical value of byte array passed
    • toNumber

      public static long toNumber(byte[] array, int numBits)
      Returns the numerical value of the last numBits (LSB bits) of the byte array passed.
      Returns:
      long - numerical value of byte array passed
    • toByteArray

      public static byte[] toByteArray(Number input)
      Accepts a number as input and returns its value in byte form in LSB aligned form example: input = 5000 [1001110001000] bytes = 19, -120 [00010011] [10001000].
    • toByteArray

      public static byte[] toByteArray(Number input, int numBits)
      Accepts a number as input and returns its value in byte form in MSB aligned form example: input = 5000 [1001110001000] bytes = -114, 64 [10011100] [01000000].
      Parameters:
      numBits - - the number of bits to be returned
      Returns:
      byte[]
    • shiftBitsToMSB

      public static byte[] shiftBitsToMSB(byte[] inputBytes, int numBits)
      Takes an LSB aligned byte array and returned the LSB numBits in a MSB aligned byte array.

      It aligns the last numBits bits to the head of the byte array following them with numBits % 8 zero bits.

      Example: For inputbytes = [00000111][01110001] and numBits = 12 it returns: shiftedBytes = [01110111][00010000]

      Parameters:
      numBits - - number of bits to be left aligned
      Returns:
      byte[]
    • shiftBitsToLSB

      public static byte[] shiftBitsToLSB(byte[] inputBytes, int numBits)
      It aligns the first numBits bits to the right end of the byte array preceding them with numBits % 8 zero bits.

      Example: For inputbytes = [01110111][00010000] and numBits = 12 it returns: shiftedBytes = [00000111][01110001]

      Parameters:
      inputBytes - input bytes
      numBits - - number of bits to be right aligned
      Returns:
      byte[]
    • checkExceptions

      public static void checkExceptions(byte[] data, int startOffset, int numBits) throws BufferException
      Checks for overflow and underflow exceptions.
      Throws:
      BufferException - when the startOffset and numBits parameters are not congruent with the data buffer's size