Class BitBufferHelper
java.lang.Object
org.opendaylight.openflowplugin.libraries.liblldp.BitBufferHelper
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 SummaryFields
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic voidcheckExceptions(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 bytegetByte(byte[] data) Returns the first byte from the byte array.static intgetInt(byte[] data) Returns the int value for the byte array passed.static intgetInt(byte[] data, int numBits) Returns the int value for the last numBits of the byte array passed.static longgetLong(byte[] data) Returns the long value for the byte array passed.static longgetLong(byte[] data, int numBits) Returns the long value for the last numBits of the byte array passed.static intgetLSBMask(int numBits) Returns numBits 1's in the LSB position.static intgetMSBMask(int numBits) Returns numBits 1's in the MSB position.static shortgetShort(byte[] data) Returns the short value for the byte array passed.static shortgetShort(byte[] data, int numBits) Returns the short value for the last numBits of the byte array passed.static voidsetBytes(byte[] data, byte[] input, int startOffset, int numBits) Deprecated.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[]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].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 longtoNumber(byte[] array) Returns the numerical value of the byte array passed.static longtoNumber(byte[] array, int numBits) Returns the numerical value of the last numBits (LSB bits) of the byte array passed.
- 
Field Details- 
BYTE_MASKpublic static final long BYTE_MASK- See Also:
 
 
- 
- 
Constructor Details- 
BitBufferHelperpublic BitBufferHelper()
 
- 
- 
Method Details- 
getBytepublic static byte getByte(byte[] data) Returns the first byte from the byte array.- Returns:
- byte value
 
- 
getShortpublic 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
 
- 
getShortpublic 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
 
- 
getIntpublic 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
 
- 
getIntpublic 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
 
- 
getLongpublic 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
 
- 
getLongpublic 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
 
- 
getBitspublic 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.UsecopyBitsFromLsb(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
 
- 
getMSBMaskpublic static int getMSBMask(int numBits) Returns numBits 1's in the MSB position.
- 
getLSBMaskpublic static int getLSBMask(int numBits) Returns numBits 1's in the LSB position.
- 
toNumberpublic static long toNumber(byte[] array) Returns the numerical value of the byte array passed.- Returns:
- long - numerical value of byte array passed
 
- 
toNumberpublic 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
 
- 
toByteArrayAccepts a number as input and returns its value in byte form in LSB aligned form example: input = 5000 [1001110001000] bytes = 19, -120 [00010011] [10001000].
- 
toByteArrayAccepts 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[]
 
- 
shiftBitsToMSBpublic 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[]
 
- 
shiftBitsToLSBpublic 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[]
 
- 
checkExceptionspublic 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
 
 
- 
copyBitsFromLsb(byte[], byte[], int, int)instead.