Ethernet ethPkt = new Ethernet();
ethPkt.setSourceMACAddress(sourceMac).setEtherType(EtherTypes.LLDP.shortValue()).setPayload(discoveryPkt);
if (destinationAddress == null) {
- ethPkt.setDestinationMACAddress(LLDP.LLDPMulticastMac);
+ ethPkt.setDestinationMACAddress(LLDP.LLDP_MULTICAST_MAC);
} else {
ethPkt.setDestinationMACAddress(HexEncode.bytesFromHexString(destinationAddress.getValue()));
}
public static String macToString(byte[] mac) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
- b.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
+ b.append(String.format("%02X%s", mac[i], i < mac.length - 1 ? ":" : ""));
}
return b.toString();
if (isLLDP(payload)) {
Ethernet ethPkt = new Ethernet();
try {
- ethPkt.deserialize(payload, 0, payload.length * NetUtils.NumBitsInAByte);
+ ethPkt.deserialize(payload, 0, payload.length * NetUtils.NUM_BITS_IN_A_BYTE);
} catch (Exception e) {
LOG.warn("Failed to decode LLDP packet {}", e);
return nodeConnectorRef;
ethernetType = bb.getShort(ETHERNET_VLAN_OFFSET);
}
- return (ethernetType == ETHERNET_TYPE_LLDP);
+ return ethernetType == ETHERNET_TYPE_LLDP;
}
}
package org.opendaylight.openflowplugin.libraries.liblldp;
import java.util.Arrays;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class BitBufferHelper {
protected static final Logger LOG = LoggerFactory.getLogger(BitBufferHelper.class);
- public static final long ByteMask = 0xFF;
+ public static final long BYTE_MASK = 0xFF;
// Getters
// data: array where data are stored
* @return byte value
*/
public static byte getByte(final byte[] data) {
- if (data.length * NetUtils.NumBitsInAByte > Byte.SIZE) {
- try {
- throw new BufferException("Container is too small for the number of requested bits");
- } catch (final BufferException e) {
- LOG.error("", e);
- }
+ if (data.length * NetUtils.NUM_BITS_IN_A_BYTE > Byte.SIZE) {
+ LOG.error("getByte", new BufferException("Container is too small for the number of requested bits"));
}
return data[0];
}
*/
public static short getShort(final byte[] data) {
if (data.length > Short.SIZE) {
- try {
- throw new BufferException("Container is too small for the number of requested bits");
- } catch (final BufferException e) {
- LOG.error("", e);
- }
+ LOG.error("getShort", new BufferException("Container is too small for the number of requested bits"));
}
return (short) toNumber(data);
}
- /**
- * Returns the int value for the byte array passed. Size of byte array is
- * restricted to Integer.SIZE
- *
- * @return int - the integer value of byte array
- */
- public static int getInt(final byte[] data) {
- if (data.length > Integer.SIZE) {
- try {
- throw new BufferException("Container is too small for the number of requested bits");
- } catch (final BufferException e) {
- LOG.error("", e);
- }
- }
- return (int) toNumber(data);
- }
-
- /**
- * Returns the long value for the byte array passed. Size of byte array is
- * restricted to Long.SIZE
- *
- * @return long - the integer value of byte array
- */
- public static long getLong(final byte[] data) {
- if (data.length > Long.SIZE) {
- try {
- throw new BufferException("Container is too small for the number of requested bits");
- } catch (final Exception e) {
- LOG.error("", e);
- }
- }
- return toNumber(data);
- }
-
/**
* Returns the short value for the last numBits of the byte array passed.
* Size of numBits is restricted to Short.SIZE
*/
public static short getShort(final byte[] data, final int numBits) {
if (numBits > Short.SIZE) {
- try {
- throw new BufferException("Container is too small for the number of requested bits");
- } catch (final BufferException e) {
- LOG.error("", e);
- }
+ LOG.error("getShort", new BufferException("Container is too small for the number of requested bits"));
}
- int startOffset = data.length * NetUtils.NumBitsInAByte - numBits;
+ int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
byte[] bits = null;
try {
bits = BitBufferHelper.getBits(data, startOffset, numBits);
return (short) toNumber(bits, numBits);
}
+ /**
+ * Returns the int value for the byte array passed. Size of byte array is
+ * restricted to Integer.SIZE
+ *
+ * @return int - the integer value of byte array
+ */
+ public static int getInt(final byte[] data) {
+ if (data.length > Integer.SIZE) {
+ LOG.error("getInt", new BufferException("Container is too small for the number of requested bits"));
+ }
+ return (int) toNumber(data);
+ }
+
/**
* Returns the int value for the last numBits of the byte array passed. Size
* of numBits is restricted to Integer.SIZE
*/
public static int getInt(final byte[] data, final int numBits) {
if (numBits > Integer.SIZE) {
- try {
- throw new BufferException("Container is too small for the number of requested bits");
- } catch (final BufferException e) {
- LOG.error("", e);
- }
+ LOG.error("getInt", new BufferException("Container is too small for the number of requested bits"));
}
- int startOffset = data.length * NetUtils.NumBitsInAByte - numBits;
+ int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
byte[] bits = null;
try {
bits = BitBufferHelper.getBits(data, startOffset, numBits);
return (int) toNumber(bits, numBits);
}
+ /**
+ * Returns the long value for the byte array passed. Size of byte array is
+ * restricted to Long.SIZE
+ *
+ * @return long - the integer value of byte array
+ */
+ public static long getLong(final byte[] data) {
+ if (data.length > Long.SIZE) {
+ LOG.error("getLong", new BufferException("Container is too small for the number of requested bits"));
+ }
+ return toNumber(data);
+ }
+
/**
* Returns the long value for the last numBits of the byte array passed.
* Size of numBits is restricted to Long.SIZE
*/
public static long getLong(final byte[] data, final int numBits) {
if (numBits > Long.SIZE) {
- try {
- throw new BufferException("Container is too small for the number of requested bits");
- } catch (final BufferException e) {
- LOG.error("", e);
- }
+ LOG.error("getLong", new BufferException("Container is too small for the number of requested bits"));
}
- if (numBits > data.length * NetUtils.NumBitsInAByte) {
+ if (numBits > data.length * NetUtils.NUM_BITS_IN_A_BYTE) {
try {
throw new BufferException("Trying to read more bits than contained in the data buffer");
} catch (final BufferException e) {
LOG.error("", e);
}
}
- int startOffset = data.length * NetUtils.NumBitsInAByte - numBits;
+ int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
byte[] bits = null;
try {
bits = BitBufferHelper.getBits(data, startOffset, numBits);
* 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}
*
*/
public static byte[] getBits(final byte[] data, final int startOffset, final 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;
+ int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
+ final int extraOffsetBits = startOffset % NetUtils.NUM_BITS_IN_A_BYTE;
+ int numBytes = numBits % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1 + numBits / NetUtils.NUM_BITS_IN_A_BYTE
+ : numBits / NetUtils.NUM_BITS_IN_A_BYTE;
+ startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE;
byte[] bytes = new byte[numBytes];
if (numBits == 0) {
return bytes;
bytes[numBytes - 1] = (byte) (data[startByteOffset + numBytes - 1] & getMSBMask(extranumBits));
}
} else {
- int i;
- for (i = 0; i < numBits / NetUtils.NumBitsInAByte; i++) {
+ int index;
+ int valfromcurr;
+ int valfromnext;
+ for (index = 0; index < numBits / NetUtils.NUM_BITS_IN_A_BYTE; index++) {
// 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);
+ valfromcurr = data[startByteOffset + index] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
+ valfromnext = data[startByteOffset + index + 1] & getMSBMask(extraOffsetBits);
+ bytes[index] = (byte) (valfromcurr << extraOffsetBits
+ | valfromnext >> NetUtils.NUM_BITS_IN_A_BYTE - 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);
+ if (extranumBits < NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
+ valfromnext = (byte) (data[startByteOffset + index] & getMSBMask(extranumBits) >> extraOffsetBits);
+ bytes[index] = (byte) (valfromnext << extraOffsetBits);
+ } else if (extranumBits == NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
+ valfromcurr = data[startByteOffset + index]
+ & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
+ bytes[index] = (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);
+ valfromcurr = data[startByteOffset + index]
+ & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
+ valfromnext = data[startByteOffset + index + 1]
+ & getMSBMask(extranumBits - (NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
+ bytes[index] = (byte) (valfromcurr << extraOffsetBits
+ | valfromnext >> NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
}
}
}
// Aligns the bits to LSB
- shiftedBytes = shiftBitsToLSB(bytes, numBits);
- return shiftedBytes;
+ return shiftBitsToLSB(bytes, numBits);
}
// Setters
// numBits: number of bits to read
/**
- * Bits are expected to be stored in the input byte array from LSB
+ * Bits are expected to be stored in the input byte array from LSB.
*
* @param data
* to set the input byte
}
/**
- * Bits are expected to be stored in the input byte array from LSB
+ * Bits are expected to be stored in the input byte array from LSB.
*
* @param data
* to set the input byte
}
/**
- * Returns numBits 1's in the MSB position
+ * Returns numBits 1's in the MSB position.
*/
public static int getMSBMask(final int numBits) {
int mask = 0;
}
/**
- * Returns numBits 1's in the LSB position
+ * Returns numBits 1's in the LSB position.
*/
public static int getLSBMask(final int numBits) {
int mask = 0;
}
/**
- * Returns the numerical value of the byte array passed
+ * Returns the numerical value of the byte array passed.
*
* @return long - numerical value of byte array passed
*/
- static public long toNumber(final byte[] array) {
+ public static long toNumber(final byte[] array) {
long ret = 0;
long length = array.length;
int value = 0;
if (value < 0) {
value += 256;
}
- ret = ret | (long) value << (length - i - 1) * NetUtils.NumBitsInAByte;
+ ret = ret | (long) value << (length - i - 1) * NetUtils.NUM_BITS_IN_A_BYTE;
}
return ret;
}
/**
- * Returns the numerical value of the last numBits (LSB bits) of the byte
- * array passed
+ * Returns the numerical value of the last numBits (LSB bits) of the byte array passed.
*
* @return long - numerical value of byte array passed
*/
- static public long toNumber(final byte[] array, final int numBits) {
- int length = numBits / NetUtils.NumBitsInAByte;
- int bitsRest = numBits % NetUtils.NumBitsInAByte;
+ public static long toNumber(final byte[] array, final int numBits) {
+ int length = numBits / NetUtils.NUM_BITS_IN_A_BYTE;
+ int bitsRest = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
int startOffset = array.length - length;
long ret = 0;
int value = 0;
value = array[startOffset - 1] & getLSBMask(bitsRest);
value = array[startOffset - 1] < 0 ? array[startOffset - 1] + 256 : array[startOffset - 1];
- ret = ret | value << (array.length - startOffset) * NetUtils.NumBitsInAByte;
+ ret = ret | value << (array.length - startOffset) * NetUtils.NUM_BITS_IN_A_BYTE;
for (int i = startOffset; i < array.length; i++) {
value = array[i];
if (value < 0) {
value += 256;
}
- ret = ret | (long) value << (array.length - i - 1) * NetUtils.NumBitsInAByte;
+ ret = ret | (long) value << (array.length - i - 1) * NetUtils.NUM_BITS_IN_A_BYTE;
}
return ret;
/**
* 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]
+ * [00010011] [10001000].
*/
public static byte[] toByteArray(final Number input) {
Class<? extends Number> dataType = input.getClass();
throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n");
}
- int length = size / NetUtils.NumBitsInAByte;
- byte bytes[] = new byte[length];
+ int length = size / NetUtils.NUM_BITS_IN_A_BYTE;
+ byte[] bytes = new byte[length];
// Getting the bytes from input value
for (int i = 0; i < length; i++) {
- bytes[i] = (byte) (longValue >> NetUtils.NumBitsInAByte * (length - i - 1) & ByteMask);
+ bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK);
}
return bytes;
}
/**
* 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]
+ * [10011100] [01000000].
*
* @param numBits
* - the number of bits to be returned
throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n");
}
- int length = size / NetUtils.NumBitsInAByte;
- byte bytes[] = new byte[length];
+ int length = size / NetUtils.NUM_BITS_IN_A_BYTE;
+ byte[] bytes = new byte[length];
byte[] inputbytes = new byte[length];
- byte shiftedBytes[];
+ byte[] shiftedBytes;
// Getting the bytes from input value
for (int i = 0; i < length; i++) {
- bytes[i] = (byte) (longValue >> NetUtils.NumBitsInAByte * (length - i - 1) & ByteMask);
+ bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK);
}
if (bytes[0] == 0 && dataType == Long.class || bytes[0] == 0 && dataType == Integer.class) {
/**
* Takes an LSB aligned byte array and returned the LSB numBits in a MSB
* aligned byte array.
+ *
* <p>
* It aligns the last numBits bits to the head of the byte array following
* them with numBits % 8 zero bits.
*
+ * <p>
* Example: For inputbytes = [00000111][01110001] and numBits = 12 it
* returns: shiftedBytes = [01110111][00010000]
*
* @return byte[]
*/
public static byte[] shiftBitsToMSB(final byte[] inputBytes, final int numBits) {
- int numBitstoShiftBy = 0, leadZeroesMSB = 8, numEndRestBits = 0;
+ int numBitstoShiftBy = 0;
+ int leadZeroesMSB = 8;
+ int numEndRestBits = 0;
int size = inputBytes.length;
byte[] shiftedBytes = new byte[size];
- int i;
- for (i = 0; i < Byte.SIZE; i++) {
+ for (int i = 0; i < Byte.SIZE; i++) {
if ((byte) (inputBytes[0] & getMSBMask(i + 1)) != 0) {
leadZeroesMSB = i;
break;
}
}
- if (numBits % NetUtils.NumBitsInAByte == 0) {
+ if (numBits % NetUtils.NUM_BITS_IN_A_BYTE == 0) {
numBitstoShiftBy = 0;
} else {
- numBitstoShiftBy = NetUtils.NumBitsInAByte - numBits % NetUtils.NumBitsInAByte < leadZeroesMSB
- ? NetUtils.NumBitsInAByte - numBits % NetUtils.NumBitsInAByte : leadZeroesMSB;
+ numBitstoShiftBy = NetUtils.NUM_BITS_IN_A_BYTE - numBits % NetUtils.NUM_BITS_IN_A_BYTE < leadZeroesMSB
+ ? NetUtils.NUM_BITS_IN_A_BYTE - numBits % NetUtils.NUM_BITS_IN_A_BYTE : leadZeroesMSB;
}
if (numBitstoShiftBy == 0) {
return inputBytes;
}
- if (numBits < NetUtils.NumBitsInAByte) {
+ if (numBits < NetUtils.NUM_BITS_IN_A_BYTE) {
// inputbytes.length = 1 OR read less than a byte
shiftedBytes[0] = (byte) ((inputBytes[0] & getLSBMask(numBits)) << numBitstoShiftBy);
} else {
// # of bits to read from last byte
- numEndRestBits = NetUtils.NumBitsInAByte
- - (inputBytes.length * NetUtils.NumBitsInAByte - numBits - numBitstoShiftBy);
+ numEndRestBits = NetUtils.NUM_BITS_IN_A_BYTE
+ - (inputBytes.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits - numBitstoShiftBy);
- for (i = 0; i < size - 1; i++) {
+ for (int i = 0; i < size - 1; i++) {
if (i + 1 == size - 1) {
if (numEndRestBits > numBitstoShiftBy) {
shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy
& getLSBMask(numEndRestBits - numBitstoShiftBy)) << numBitstoShiftBy);
} else {
shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy
- | (inputBytes[i + 1] & getMSBMask(numEndRestBits)) >> NetUtils.NumBitsInAByte
+ | (inputBytes[i + 1] & getMSBMask(numEndRestBits)) >> NetUtils.NUM_BITS_IN_A_BYTE
- numEndRestBits);
}
}
shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy
- | (inputBytes[i + 1] & getMSBMask(numBitstoShiftBy)) >> NetUtils.NumBitsInAByte
+ | (inputBytes[i + 1] & getMSBMask(numBitstoShiftBy)) >> NetUtils.NUM_BITS_IN_A_BYTE
- numBitstoShiftBy);
}
* It aligns the first numBits bits to the right end of the byte array
* preceding them with numBits % 8 zero bits.
*
+ * <p>
* Example: For inputbytes = [01110111][00010000] and numBits = 12 it
* returns: shiftedBytes = [00000111][01110001]
*
- * @param inputBytes
+ * @param inputBytes input bytes
* @param numBits
* - number of bits to be right aligned
* @return byte[]
*/
public static byte[] shiftBitsToLSB(final byte[] inputBytes, final int numBits) {
int numBytes = inputBytes.length;
- int numBitstoShift = numBits % NetUtils.NumBitsInAByte;
+ int numBitstoShift = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
byte[] shiftedBytes = new byte[numBytes];
- int inputLsb = 0, inputMsb = 0;
+ int inputLsb = 0;
+ int inputMsb = 0;
if (numBitstoShift == 0) {
return inputBytes;
}
for (int i = 1; i < numBytes; i++) {
- inputLsb = inputBytes[i - 1] & getLSBMask(NetUtils.NumBitsInAByte - numBitstoShift);
+ inputLsb = inputBytes[i - 1] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
inputLsb = inputLsb < 0 ? inputLsb + 256 : inputLsb;
inputMsb = inputBytes[i] & getMSBMask(numBitstoShift);
inputMsb = inputBytes[i] < 0 ? inputBytes[i] + 256 : inputBytes[i];
shiftedBytes[i] = (byte) (inputLsb << numBitstoShift
- | inputMsb >> NetUtils.NumBitsInAByte - numBitstoShift);
+ | inputMsb >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
}
inputMsb = inputBytes[0] & getMSBMask(numBitstoShift);
inputMsb = inputMsb < 0 ? inputMsb + 256 : inputMsb;
- shiftedBytes[0] = (byte) (inputMsb >> NetUtils.NumBitsInAByte - numBitstoShift);
+ shiftedBytes[0] = (byte) (inputMsb >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
return shiftedBytes;
}
// passed
// byte
// array
- int numBytes = numBits / NetUtils.NumBitsInAByte;
- int startByteOffset = startOffset / NetUtils.NumBitsInAByte;
- int extraOffsetBits = startOffset % NetUtils.NumBitsInAByte;
- int extranumBits = numBits % NetUtils.NumBitsInAByte;
- int RestBits = numBits % NetUtils.NumBitsInAByte;
- int InputMSBbits = 0, InputLSBbits = 0;
- int i;
+ int numBytes = numBits / NetUtils.NUM_BITS_IN_A_BYTE;
+ int startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE;
+ int extraOffsetBits = startOffset % NetUtils.NUM_BITS_IN_A_BYTE;
+ int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
+ int restBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
+ int inputMSBbits = 0;
+ int inputLSBbits = 0;
if (numBits == 0) {
return;
if (extraOffsetBits == 0) {
if (extranumBits == 0) {
- numBytes = numBits / NetUtils.NumBitsInAByte;
+ numBytes = numBits / NetUtils.NUM_BITS_IN_A_BYTE;
System.arraycopy(inputdata, 0, data, startByteOffset, numBytes);
} else {
System.arraycopy(inputdata, 0, data, startByteOffset, numBytes);
| inputdata[numBytes] & getMSBMask(extranumBits));
}
} else {
- for (i = 0; i < numBytes; i++) {
- if (i != 0) {
- InputLSBbits = inputdata[i - 1] & getLSBMask(extraOffsetBits);
+ int index;
+ for (index = 0; index < numBytes; index++) {
+ if (index != 0) {
+ inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
}
- InputMSBbits = (byte) (inputdata[i] & getMSBMask(NetUtils.NumBitsInAByte - extraOffsetBits));
- InputMSBbits = InputMSBbits >= 0 ? InputMSBbits : InputMSBbits + 256;
- data[startByteOffset + i] = (byte) (data[startByteOffset + i]
- | InputLSBbits << NetUtils.NumBitsInAByte - extraOffsetBits | InputMSBbits >> extraOffsetBits);
- InputMSBbits = InputLSBbits = 0;
+ inputMSBbits = (byte) (inputdata[index] & getMSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
+ inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
+ data[startByteOffset + index] = (byte) (data[startByteOffset + index]
+ | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
+ | inputMSBbits >> extraOffsetBits);
+ inputMSBbits = inputLSBbits = 0;
}
- if (RestBits < NetUtils.NumBitsInAByte - extraOffsetBits) {
+ if (restBits < NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
if (numBytes != 0) {
- InputLSBbits = inputdata[i - 1] & getLSBMask(extraOffsetBits);
+ inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
}
- InputMSBbits = (byte) (inputdata[i] & getMSBMask(RestBits));
- InputMSBbits = InputMSBbits >= 0 ? InputMSBbits : InputMSBbits + 256;
- data[startByteOffset + i] = (byte) (data[startByteOffset + i]
- | InputLSBbits << NetUtils.NumBitsInAByte - extraOffsetBits | InputMSBbits >> extraOffsetBits);
- } else if (RestBits == NetUtils.NumBitsInAByte - extraOffsetBits) {
+ inputMSBbits = (byte) (inputdata[index] & getMSBMask(restBits));
+ inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
+ data[startByteOffset + index] = (byte) (data[startByteOffset + index]
+ | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
+ | inputMSBbits >> extraOffsetBits);
+ } else if (restBits == NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
if (numBytes != 0) {
- InputLSBbits = inputdata[i - 1] & getLSBMask(extraOffsetBits);
+ inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
}
- InputMSBbits = (byte) (inputdata[i] & getMSBMask(NetUtils.NumBitsInAByte - extraOffsetBits));
- InputMSBbits = InputMSBbits >= 0 ? InputMSBbits : InputMSBbits + 256;
- data[startByteOffset + i] = (byte) (data[startByteOffset + i]
- | InputLSBbits << NetUtils.NumBitsInAByte - extraOffsetBits | InputMSBbits >> extraOffsetBits);
+ inputMSBbits = (byte) (inputdata[index] & getMSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
+ inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
+ data[startByteOffset + index] = (byte) (data[startByteOffset + index]
+ | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
+ | inputMSBbits >> extraOffsetBits);
} else {
if (numBytes != 0) {
- InputLSBbits = inputdata[i - 1] & getLSBMask(extraOffsetBits);
+ inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
}
- InputMSBbits = (byte) (inputdata[i] & getMSBMask(NetUtils.NumBitsInAByte - extraOffsetBits));
- InputMSBbits = InputMSBbits >= 0 ? InputMSBbits : InputMSBbits + 256;
- data[startByteOffset + i] = (byte) (data[startByteOffset + i]
- | InputLSBbits << NetUtils.NumBitsInAByte - extraOffsetBits | InputMSBbits >> extraOffsetBits);
-
- InputLSBbits = inputdata[i]
- & getLSBMask(RestBits - (NetUtils.NumBitsInAByte - extraOffsetBits)) << NetUtils.NumBitsInAByte
- - RestBits;
- data[startByteOffset + i + 1] = (byte) (data[startByteOffset + i + 1]
- | InputLSBbits << NetUtils.NumBitsInAByte - extraOffsetBits);
+ inputMSBbits = (byte) (inputdata[index] & getMSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
+ inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
+ data[startByteOffset + index] = (byte) (data[startByteOffset + index]
+ | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
+ | inputMSBbits >> extraOffsetBits);
+
+ inputLSBbits = inputdata[index]
+ & getLSBMask(restBits - (NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits))
+ << NetUtils.NUM_BITS_IN_A_BYTE
+ - restBits;
+ data[startByteOffset + index + 1] = (byte) (data[startByteOffset + index + 1]
+ | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
}
}
}
/**
- * Checks for overflow and underflow exceptions
+ * Checks for overflow and underflow exceptions.
*
* @throws BufferException
* when the startOffset and numBits parameters are not congruent
throws BufferException {
int endOffsetByte;
int startByteOffset;
- endOffsetByte = startOffset / NetUtils.NumBitsInAByte + numBits / NetUtils.NumBitsInAByte
- + (numBits % NetUtils.NumBitsInAByte != 0 ? 1 : startOffset % NetUtils.NumBitsInAByte != 0 ? 1 : 0);
- startByteOffset = startOffset / NetUtils.NumBitsInAByte;
+ endOffsetByte = startOffset / NetUtils.NUM_BITS_IN_A_BYTE + numBits / NetUtils.NUM_BITS_IN_A_BYTE
+ + (numBits % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1
+ : startOffset % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1 : 0);
+ startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE;
if (data == null) {
throw new BufferException("data[] is null\n");
}
if (startOffset < 0 || startByteOffset >= data.length || endOffsetByte > data.length || numBits < 0
- || numBits > NetUtils.NumBitsInAByte * data.length) {
+ || numBits > NetUtils.NUM_BITS_IN_A_BYTE * data.length) {
throw new BufferException("Illegal arguement/out of bound exception - data.length = " + data.length
+ " startOffset = " + startOffset + " numBits " + numBits);
}
public class CustomTLVKey {
- private int oui;
- private byte subtype;
+ private final int oui;
+ private final byte subtype;
- /**
- * @param oui
- * @param subtype
- */
public CustomTLVKey(final int oui, final byte subtype) {
this.oui = oui;
this.subtype = subtype;
}
/**
- * @return the oui
+ * Returns the oui.
*/
public int getOui() {
return oui;
}
/**
- * @return the subtype
+ * Returns the subtype.
*/
public byte getSubtype() {
return subtype;
package org.opendaylight.openflowplugin.libraries.liblldp;
import java.io.Serializable;
-
import javax.xml.bind.annotation.XmlRootElement;
/**
- * @file DataLinkAddress.java
- *
- * @brief Abstract base class for a Datalink Address
- *
- */
-
-/**
- * Abstract base class for a Datalink Address
- *
+ * Abstract base class for a Datalink Address.
*/
@XmlRootElement
-abstract public class DataLinkAddress implements Serializable {
+public abstract class DataLinkAddress implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
}
/**
- * Constructor of super class
+ * Constructor of super class.
*
* @param name Create a new DataLink, not for general use but
- * available only for sub classes
+ * available only for sub classes
*/
protected DataLinkAddress(final String name) {
this.name = name;
}
/**
- * Used to copy the DataLinkAddress in a polymorphic way
- *
+ * Used to copy the DataLinkAddress in a polymorphic way.
*
* @return A clone of this DataLinkAddress
*/
@Override
- abstract public DataLinkAddress clone();
+ public abstract DataLinkAddress clone();
/**
- * Allow to distinguish among different data link addresses
- *
+ * Allow to distinguish among different data link addresses.
*
* @return Name of the DataLinkAdress we are working on
*/
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
+ result = prime * result + (name == null ? 0 : name.hashCode());
return result;
}
import java.util.List;
/**
- * The enum contains the most common 802.3 ethernet types and 802.2 + SNAP protocol ids
- *
- *
- *
+ * The enum contains the most common 802.3 ethernet types and 802.2 + SNAP protocol ids.
*/
public enum EtherTypes {
PVSTP("PVSTP", 0x010B), // 802.2 + SNAP (Spanning Tree)
LLDP("LLDP", 0x88CC), OLDQINQ("Old QINQ", 0x9100), // Old non-standard QinQ
CISCOQINQ("Cisco QINQ", 0x9200); // Cisco non-standard QinQ
- private static final String regexNumberString = "^[0-9]+$";
+ private static final String REGEX_NUMBER_STRING = "^[0-9]+$";
private String description;
private int number;
this.number = number;
}
+ @Override
public String toString() {
return description;
}
}
public static short getEtherTypeNumberShort(final String name) {
- if (name.matches(regexNumberString)) {
+ if (name.matches(REGEX_NUMBER_STRING)) {
return Short.valueOf(name);
}
for (EtherTypes type : EtherTypes.values()) {
}
public static int getEtherTypeNumberInt(final String name) {
- if (name.matches(regexNumberString)) {
+ if (name.matches(REGEX_NUMBER_STRING)) {
return Integer.valueOf(name);
}
for (EtherTypes type : EtherTypes.values()) {
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
-
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
/**
- * Class that represents the Ethernet frame objects
+ * Class that represents the Ethernet frame objects.
*/
public class Ethernet extends Packet {
private static final String DMAC = "DestinationMACAddress";
// TODO: This has to be outside and it should be possible for osgi
// to add new coming packet classes
- public static final Map<Short, Class<? extends Packet>> etherTypeClassMap;
+ public static final Map<Short, Class<? extends Packet>> ETHER_TYPE_CLASS_MAP = new HashMap<>();
+
static {
- etherTypeClassMap = new HashMap<>();
- etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
+ ETHER_TYPE_CLASS_MAP.put(EtherTypes.LLDP.shortValue(), LLDP.class);
}
- private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
- private static final long serialVersionUID = 1L;
- {
- put(DMAC, new ImmutablePair<>(0, 48));
- put(SMAC, new ImmutablePair<>(48, 48));
- put(ETHT, new ImmutablePair<>(96, 16));
- }
- };
+
+ private static final Map<String, Pair<Integer, Integer>> FIELD_COORDINATES = new LinkedHashMap<>();
+
+ static {
+ FIELD_COORDINATES.put(DMAC, new ImmutablePair<>(0, 48));
+ FIELD_COORDINATES.put(SMAC, new ImmutablePair<>(48, 48));
+ FIELD_COORDINATES.put(ETHT, new ImmutablePair<>(96, 16));
+ }
+
private final Map<String, byte[]> fieldValues;
/**
- * Default constructor that creates and sets the HashMap
+ * Default constructor that creates and sets the HashMap.
*/
public Ethernet() {
fieldValues = new HashMap<>();
- hdrFieldCoordMap = fieldCoordinates;
+ hdrFieldCoordMap = FIELD_COORDINATES;
hdrFieldsMap = fieldValues;
}
/**
- * Constructor that sets the access level for the packet and
- * creates and sets the HashMap
+ * Constructor that sets the access level for the packet and creates and sets the HashMap.
*/
public Ethernet(final boolean writeAccess) {
super(writeAccess);
fieldValues = new HashMap<>();
- hdrFieldCoordMap = fieldCoordinates;
+ hdrFieldCoordMap = FIELD_COORDINATES;
hdrFieldsMap = fieldValues;
}
@Override
public void setHeaderField(final String headerField, final byte[] readValue) {
if (headerField.equals(ETHT)) {
- payloadClass = etherTypeClassMap.get(BitBufferHelper
+ payloadClass = ETHER_TYPE_CLASS_MAP.get(BitBufferHelper
.getShort(readValue));
}
hdrFieldsMap.put(headerField, readValue);
}
/**
- * Gets the destination MAC address stored
+ * Gets the destination MAC address stored.
+ *
* @return byte[] - the destinationMACAddress
*/
public byte[] getDestinationMACAddress() {
}
/**
- * Gets the source MAC address stored
+ * Gets the source MAC address stored.
+ *
* @return byte[] - the sourceMACAddress
*/
public byte[] getSourceMACAddress() {
}
/**
- * Gets the etherType stored
+ * Gets the etherType stored.
+ *
* @return short - the etherType
*/
public short getEtherType() {
return BitBufferHelper.getShort(fieldValues.get(ETHT));
}
- public boolean isBroadcast(){
+ public boolean isBroadcast() {
return NetUtils.isBroadcastMACAddr(getDestinationMACAddress());
}
- public boolean isMulticast(){
+ public boolean isMulticast() {
return NetUtils.isMulticastMACAddr(getDestinationMACAddress());
}
/**
- * Sets the destination MAC address for the current Ethernet object instance
+ * Sets the destination MAC address for the current Ethernet object instance.
+ *
* @param destinationMACAddress the destinationMACAddress to set
*/
public Ethernet setDestinationMACAddress(final byte[] destinationMACAddress) {
}
/**
- * Sets the source MAC address for the current Ethernet object instance
+ * Sets the source MAC address for the current Ethernet object instance.
+ *
* @param sourceMACAddress the sourceMACAddress to set
*/
public Ethernet setSourceMACAddress(final byte[] sourceMACAddress) {
}
/**
- * Sets the etherType for the current Ethernet object instance
+ * Sets the etherType for the current Ethernet object instance.
+ *
* @param etherType the etherType to set
*/
public Ethernet setEtherType(final short etherType) {
fieldValues.put(ETHT, ethType);
return this;
}
-
}
package org.opendaylight.openflowplugin.libraries.liblldp;
import java.util.Arrays;
-
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
private byte[] macAddress;
public static final EthernetAddress BROADCASTMAC = createWellKnownAddress(new byte[] {
- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
- (byte) 0xff });
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff });
public static final EthernetAddress INVALIDHOST = BROADCASTMAC;
- public static final String addressName = "Ethernet MAC Address";
+ public static final String ADDRESS_NAME = "Ethernet MAC Address";
public static final int SIZE = 6;
- private static final EthernetAddress createWellKnownAddress(final byte[] mac) {
+ private static EthernetAddress createWellKnownAddress(final byte[] mac) {
try {
return new EthernetAddress(mac);
} catch (final ConstructionException ce) {
/**
* Public constructor for an Ethernet MAC address starting from
* the byte constituing the address, the constructor validate the
- * size of the arrive to make sure it met the expected size
+ * size of the arrive to make sure it met the expected size.
*
- * @param macAddress A byte array in big endian format
- * representing the Ethernet MAC Address
+ * @param macAddress A byte array in big endian format representing the Ethernet MAC Address
*/
public EthernetAddress(final byte[] macAddress) throws ConstructionException {
- super(addressName);
+ super(ADDRESS_NAME);
if (macAddress == null) {
throw new ConstructionException("Null input parameter passed");
System.arraycopy(macAddress, 0, this.macAddress, 0, SIZE);
}
+ @Override
public EthernetAddress clone() {
try {
return new EthernetAddress(this.macAddress.clone());
}
/**
- * Return the Ethernet Mac address in byte array format
+ * Return the Ethernet Mac address in byte array format.
*
* @return The Ethernet Mac address in byte array format
*/
import java.math.BigInteger;
/**
- * The class provides methods to convert hex encode strings
- *
- *
+ * The class provides methods to convert hex encode strings.
*/
-public class HexEncode {
+public final class HexEncode {
+ private HexEncode() {
+ }
+
/**
* This method converts byte array into String format without ":" inserted.
*
}
StringBuilder buf = new StringBuilder();
- for (int i = 0; i < bytes.length; i++) {
- short u8byte = (short) (bytes[i] & 0xff);
+ for (byte b : bytes) {
+ short u8byte = (short) (b & 0xff);
String tmp = Integer.toHexString(u8byte);
if (tmp.length() == 1) {
buf.append("0");
}
public static String longToHexString(final long val) {
- char arr[] = Long.toHexString(val).toCharArray();
+ char[] arr = Long.toHexString(val).toCharArray();
StringBuilder buf = new StringBuilder();
// prepend the right number of leading zeros
- int i = 0;
- for (; i < (16 - arr.length); i++) {
+ int index = 0;
+ for (; index < 16 - arr.length; index++) {
buf.append("0");
- if ((i & 0x01) == 1) {
+ if ((index & 0x01) == 1) {
buf.append(":");
}
}
for (int j = 0; j < arr.length; j++) {
buf.append(arr[j]);
- if ((((i + j) & 0x01) == 1) && (j < (arr.length - 1))) {
+ if ((index + j & 0x01) == 1 && j < arr.length - 1) {
buf.append(":");
}
}
import java.util.Map;
/**
- * Class that represents the LLDP frame objects
+ * Class that represents the LLDP frame objects.
*/
-
public class LLDP extends Packet {
private static final String CHASSISID = "ChassisId";
private static final String SYSTEMNAMEID = "SystemNameID";
private static final String PORTID = "PortId";
private static final String TTL = "TTL";
- private static final int LLDPDefaultTlvs = 3;
- private static final LLDPTLV emptyTLV = new LLDPTLV().setLength((short) 0).setType((byte) 0);
- public static final byte[] LLDPMulticastMac = { 1, (byte) 0x80, (byte) 0xc2, 0, 0, (byte) 0xe };
+ private static final int LLDP_DEFAULT_TLVS = 3;
+ private static final LLDPTLV EMPTY_TLV = new LLDPTLV().setLength((short) 0).setType((byte) 0);
+ public static final byte[] LLDP_MULTICAST_MAC = { 1, (byte) 0x80, (byte) 0xc2, 0, 0, (byte) 0xe };
private Map<Byte, LLDPTLV> mandatoryTLVs;
private Map<Byte, LLDPTLV> optionalTLVs;
private Map<CustomTLVKey, LLDPTLV> customTLVs;
/**
- * Default constructor that creates the tlvList LinkedHashMap
+ * Default constructor that creates the tlvList LinkedHashMap.
*/
public LLDP() {
init();
}
/**
- * Constructor that creates the tlvList LinkedHashMap and sets the write access for the same
+ * Constructor that creates the tlvList LinkedHashMap and sets the write access for the same.
*/
public LLDP(final boolean writeAccess) {
super(writeAccess);
}
private void init() {
- mandatoryTLVs = new LinkedHashMap<>(LLDPDefaultTlvs);
+ mandatoryTLVs = new LinkedHashMap<>(LLDP_DEFAULT_TLVS);
optionalTLVs = new LinkedHashMap<>();
customTLVs = new LinkedHashMap<>();
}
/**
- * @param String
- * - description of the type of TLV
- * @return byte - type of TLV
+ * Returns the TLV byte type.
+ *
+ * @param String description of the type of TLV
+ * @return byte type of TLV
*/
private byte getType(final String typeDesc) {
if (typeDesc.equals(CHASSISID)) {
}
/**
- * @param type
- * - description of the type of TLV
+ * Gets the full LLDPTLV.
+ *
+ * @param type description of the type of TLV
* @return LLDPTLV - full TLV
*/
public LLDPTLV getTLV(final String type) {
}
/**
- * @param type
- * - description of the type of TLV
- * @param tlv
- * - tlv to set
+ * Sets the LLDPTLV for a type.
+ *
+ * @param type description of the type of TLV
+ * @param tlv tlv to set
*/
public void setTLV(final String type, final LLDPTLV tlv) {
putToTLVs(getType(type), tlv);
}
/**
- * @return the chassisId TLV
+ * Returns the chassisId TLV.
*/
public LLDPTLV getChassisId() {
return getTLV(CHASSISID);
}
- /**
- * @param chassisId
- * - the chassisId to set
- */
public LLDP setChassisId(final LLDPTLV chassisId) {
setTLV(CHASSISID, chassisId);
return this;
}
/**
- * @return the SystemName TLV
+ * Returns the SystemName TLV.
*/
public LLDPTLV getSystemNameId() {
return getTLV(SYSTEMNAMEID);
}
- /**
- * @param systemNameId
- * - the systemNameId to set
- */
public LLDP setSystemNameId(final LLDPTLV systemNameId) {
setTLV(SYSTEMNAMEID, systemNameId);
return this;
}
/**
- * @return LLDPTLV - the portId TLV
+ * Returns the portId TLV.
*/
public LLDPTLV getPortId() {
return getTLV(PORTID);
}
- /**
- * @param portId
- * - the portId to set
- * @return LLDP
- */
public LLDP setPortId(final LLDPTLV portId) {
setTLV(PORTID, portId);
return this;
}
/**
- * @return LLDPTLV - the ttl TLV
+ * Return the ttl TLV.
*/
public LLDPTLV getTtl() {
return getTLV(TTL);
}
- /**
- * @param ttl
- * - the ttl to set
- * @return LLDP
- */
public LLDP setTtl(final LLDPTLV ttl) {
setTLV(TTL, ttl);
return this;
}
/**
- * @return the optionalTLVList
+ * Returns the optionalTLVList.
*/
public Iterable<LLDPTLV> getOptionalTLVList() {
return optionalTLVs.values();
}
/**
- * @return the customTlvList
+ * Returns the customTlvList.
*/
public Iterable<LLDPTLV> getCustomTlvList() {
return customTLVs.values();
}
- /**
- * @param optionalTLVList
- * the optionalTLVList to set
- * @return LLDP
- */
public LLDP setOptionalTLVList(final List<LLDPTLV> optionalTLVList) {
for (LLDPTLV tlv : optionalTLVList) {
optionalTLVs.put(tlv.getType(), tlv);
return this;
}
- /**
- * @param customTLV
- * the list of custom TLVs to set
- * @return this LLDP
- */
public LLDP addCustomTLV(final LLDPTLV customTLV) {
CustomTLVKey key = new CustomTLVKey(LLDPTLV.extractCustomOUI(customTLV),
LLDPTLV.extractCustomSubtype(customTLV));
if (LOG.isTraceEnabled()) {
LOG.trace("LLDP: {} (offset {} bitsize {})", new Object[] { HexEncode.bytesToHexString(data),
- lldpOffset, lldpSize });
+ lldpOffset, lldpSize });
}
/*
* Deserialize the TLVs until we reach the end of the packet
int startOffset = 0;
byte[] serializedBytes = new byte[getLLDPPacketLength()];
- final Iterable<LLDPTLV> allTlvs = Iterables.concat(mandatoryTLVs.values(), optionalTLVs.values(), customTLVs.values());
+ final Iterable<LLDPTLV> allTlvs = Iterables.concat(mandatoryTLVs.values(), optionalTLVs.values(),
+ customTLVs.values());
for (LLDPTLV tlv : allTlvs) {
int numBits = tlv.getTLVSize();
try {
BitBufferHelper.setBytes(serializedBytes, tlv.serialize(), startOffset, numBits);
} catch (final BufferException e) {
- throw new PacketException(e.getMessage());
+ throw new PacketException("Error from setBytes", e);
}
startOffset += numBits;
}
// Now add the empty LLDPTLV at the end
try {
- BitBufferHelper.setBytes(serializedBytes, LLDP.emptyTLV.serialize(), startOffset,
- LLDP.emptyTLV.getTLVSize());
+ BitBufferHelper.setBytes(serializedBytes, LLDP.EMPTY_TLV.serialize(), startOffset,
+ LLDP.EMPTY_TLV.getTLVSize());
} catch (final BufferException e) {
- throw new PacketException(e.getMessage());
+ throw new PacketException("Error from setBytes", e);
}
if (LOG.isTraceEnabled()) {
}
/**
- * Returns the size of LLDP packet in bytes
+ * Returns the size of LLDP packet in bytes.
*
* @return int - LLDP Packet size in bytes
*/
len += lldptlv.getTLVSize();
}
- len += LLDP.emptyTLV.getTLVSize();
+ len += LLDP.EMPTY_TLV.getTLVSize();
- return len / NetUtils.NumBitsInAByte;
+ return len / NetUtils.NUM_BITS_IN_A_BYTE;
}
}
package org.opendaylight.openflowplugin.libraries.liblldp;
-import org.apache.commons.lang3.ArrayUtils;
-import org.slf4j.LoggerFactory;
-
-import org.slf4j.Logger;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
+import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
/**
- * Class that represents the LLDPTLV objects
+ * Class that represents the LLDPTLV objects.
*/
-
+@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public class LLDPTLV extends Packet {
private static final String TYPE = "Type";
private static final String LENGTH = "Length";
private static final String VALUE = "Value";
- private static final int LLDPTLVFields = 3;
+ private static final int LLDPTLV_FIELDS = 3;
- /** OpenFlow OUI */
+ /** OpenFlow OUI. */
public static final byte[] OFOUI = new byte[] { (byte) 0x00, (byte) 0x26,
(byte) 0xe1 };
- /** Length of Organizationally defined subtype field of TLV in bytes */
- private static final byte customTlvSubTypeLength = (byte)1;
+ /** Length of Organizationally defined subtype field of TLV in bytes. */
+ private static final byte CUSTOM_TLV_SUB_TYPE_LENGTH = (byte)1;
- /** OpenFlow subtype: nodeConnectorId of source */
+ /** OpenFlow subtype: nodeConnectorId of source. */
public static final byte[] CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID = new byte[] { 0 };
- /** OpenFlow subtype: custom sec = hash code of verification of origin of LLDP */
+ /** OpenFlow subtype: custom sec = hash code of verification of origin of LLDP. */
public static final byte[] CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC = new byte[] { 1 };
- public static final int customTlvOffset = OFOUI.length + customTlvSubTypeLength;
- public static final byte chassisIDSubType[] = new byte[] { 4 }; // MAC address for the system
- public static final byte portIDSubType[] = new byte[] { 7 }; // locally assigned
-
- private static final Logger LOG = LoggerFactory.getLogger(LLDPTLV.class);
+ public static final int CUSTOM_TLV_OFFSET = OFOUI.length + CUSTOM_TLV_SUB_TYPE_LENGTH;
+ public static final byte[] CHASSISID_SUB_TYPE = new byte[] { 4 }; // MAC address for the system
+ public static final byte[] PORTID_SUB_TYPE = new byte[] { 7 }; // locally assigned
public enum TLVType {
Unknown((byte) 0), ChassisID((byte) 1), PortID((byte) 2), TTL((byte) 3), PortDesc(
}
}
- private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
- private static final long serialVersionUID = 1L;
+ private static final Map<String, Pair<Integer, Integer>> FIELD_COORDINATES = new LinkedHashMap<>();
- {
- put(TYPE, new MutablePair<>(0, 7));
- put(LENGTH, new MutablePair<>(7, 9));
- put(VALUE, new MutablePair<>(16, 0));
- }
- };
+ static {
+ FIELD_COORDINATES.put(TYPE, new MutablePair<>(0, 7));
+ FIELD_COORDINATES.put(LENGTH, new MutablePair<>(7, 9));
+ FIELD_COORDINATES.put(VALUE, new MutablePair<>(16, 0));
+ }
protected Map<String, byte[]> fieldValues;
/**
- * Default constructor that creates and sets the hash map values and sets
- * the payload to null
+ * Default constructor that creates and sets the hash map values and sets the payload to null.
*/
public LLDPTLV() {
payload = null;
- fieldValues = new HashMap<>(LLDPTLVFields);
- hdrFieldCoordMap = fieldCoordinates;
+ fieldValues = new HashMap<>(LLDPTLV_FIELDS);
+ hdrFieldCoordMap = FIELD_COORDINATES;
hdrFieldsMap = fieldValues;
}
/**
- * Constructor that writes the passed LLDPTLV values to the hdrFieldsMap
+ * Constructor that writes the passed LLDPTLV values to the hdrFieldsMap.
*/
public LLDPTLV(final LLDPTLV other) {
for (Map.Entry<String, byte[]> entry : other.hdrFieldsMap.entrySet()) {
}
/**
- * @return int - the length of TLV
+ * Returns the length of TLV.
*/
public int getLength() {
return (int) BitBufferHelper.toNumber(fieldValues.get(LENGTH),
- fieldCoordinates.get(LENGTH).getRight().intValue());
+ FIELD_COORDINATES.get(LENGTH).getRight().intValue());
}
/**
- * @return byte - the type of TLV
+ * Returns the type of TLV.
*/
public byte getType() {
return BitBufferHelper.getByte(fieldValues.get(TYPE));
}
/**
- * @return byte[] - the value field of TLV
+ * Returns the value field of TLV.
*/
public byte[] getValue() {
return fieldValues.get(VALUE);
}
/**
+ * Sets the type.
+ *
* @param type the type to set
* @return LLDPTLV
*/
}
/**
+ * Sets the length.
+ *
* @param length the length to set
* @return LLDPTLV
*/
}
/**
+ * Sets the value.
+ *
* @param value the value to set
* @return LLDPTLV
*/
final int prime = 31;
int result = super.hashCode();
result = prime * result
- + ((fieldValues == null) ? 0 : fieldValues.hashCode());
+ + (fieldValues == null ? 0 : fieldValues.hashCode());
return result;
}
@Override
public int getfieldnumBits(final String fieldName) {
if (fieldName.equals(VALUE)) {
- return (NetUtils.NumBitsInAByte * BitBufferHelper.getShort(
- fieldValues.get(LENGTH), fieldCoordinates.get(LENGTH)
- .getRight().intValue()));
+ return NetUtils.NUM_BITS_IN_A_BYTE * BitBufferHelper.getShort(
+ fieldValues.get(LENGTH), FIELD_COORDINATES.get(LENGTH)
+ .getRight().intValue());
}
- return fieldCoordinates.get(fieldName).getRight();
+ return FIELD_COORDINATES.get(fieldName).getRight();
}
/**
- * Returns the size in bits of the whole TLV
+ * Returns the size in bits of the whole TLV.
*
* @return int - size in bits of full TLV
*/
public int getTLVSize() {
- return (LLDPTLV.fieldCoordinates.get(TYPE).getRight() + // static
- LLDPTLV.fieldCoordinates.get(LENGTH).getRight() + // static
- getfieldnumBits(VALUE)); // variable
+ return LLDPTLV.FIELD_COORDINATES.get(TYPE).getRight() + // static
+ LLDPTLV.FIELD_COORDINATES.get(LENGTH).getRight() + // static
+ getfieldnumBits(VALUE); // variable
}
/**
- * Creates the SystemName TLV value
+ * Creates the SystemName TLV value.
*
* @param nodeId
* node identifier string
* @return the SystemName TLV value in byte array
*/
- static public byte[] createSystemNameTLVValue(final String nodeId) {
+ public static byte[] createSystemNameTLVValue(final String nodeId) {
byte[] nid = nodeId.getBytes();
return nid;
}
/**
- * Creates the ChassisID TLV value including the subtype and ChassisID
- * string
+ * Creates the ChassisID TLV value including the subtype and ChassisID string.
*
* @param nodeId
* node identifier string
* @return the ChassisID TLV value in byte array
*/
- static public byte[] createChassisIDTLVValue(final String nodeId) {
+ public static byte[] createChassisIDTLVValue(final String nodeId) {
byte[] nid = HexEncode.bytesFromHexString(nodeId);
byte[] cid = new byte[6];
- int srcPos = 0, dstPos = 0;
+ int srcPos = 0;
+ int dstPos = 0;
if (nid.length > cid.length) {
srcPos = nid.length - cid.length;
}
System.arraycopy(nid, srcPos, cid, dstPos, cid.length);
- byte[] cidValue = new byte[cid.length + chassisIDSubType.length];
+ byte[] cidValue = new byte[cid.length + CHASSISID_SUB_TYPE.length];
- System.arraycopy(chassisIDSubType, 0, cidValue, 0,
- chassisIDSubType.length);
- System.arraycopy(cid, 0, cidValue, chassisIDSubType.length, cid.length);
+ System.arraycopy(CHASSISID_SUB_TYPE, 0, cidValue, 0,
+ CHASSISID_SUB_TYPE.length);
+ System.arraycopy(cid, 0, cidValue, CHASSISID_SUB_TYPE.length, cid.length);
return cidValue;
}
/**
- * Creates the PortID TLV value including the subtype and PortID string
+ * Creates the PortID TLV value including the subtype and PortID string.
*
* @param portId
* port identifier string
* @return the PortID TLV value in byte array
*/
- static public byte[] createPortIDTLVValue(final String portId) {
+ public static byte[] createPortIDTLVValue(final String portId) {
byte[] pid = portId.getBytes(Charset.defaultCharset());
- byte[] pidValue = new byte[pid.length + portIDSubType.length];
+ byte[] pidValue = new byte[pid.length + PORTID_SUB_TYPE.length];
- System.arraycopy(portIDSubType, 0, pidValue, 0, portIDSubType.length);
- System.arraycopy(pid, 0, pidValue, portIDSubType.length, pid.length);
+ System.arraycopy(PORTID_SUB_TYPE, 0, pidValue, 0, PORTID_SUB_TYPE.length);
+ System.arraycopy(pid, 0, pidValue, PORTID_SUB_TYPE.length, pid.length);
return pidValue;
}
/**
- * Creates the custom TLV value including OUI, subtype and custom string
+ * Creates the custom TLV value including OUI, subtype and custom string.
*
* @param customString
* port identifier string
* @return the custom TLV value in byte array
* @see #createCustomTLVValue(byte[],byte[])
*/
- static public byte[] createCustomTLVValue(final String customString) {
+ public static byte[] createCustomTLVValue(final String customString) {
byte[] customByteArray = customString.getBytes(Charset.defaultCharset());
return createCustomTLVValue(CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID, customByteArray);
}
/**
- * Creates the custom TLV value including OUI, subtype and custom string
+ * Creates the custom TLV value including OUI, subtype and custom string.
+ *
* @param subtype openflow subtype
* @param customByteArray
* port identifier string
* @return the custom TLV value in byte array
*/
- static public byte[] createCustomTLVValue(final byte[] subtype, final byte[] customByteArray) {
- byte[] customValue = new byte[customTlvOffset + customByteArray.length];
+ public static byte[] createCustomTLVValue(final byte[] subtype, final byte[] customByteArray) {
+ byte[] customValue = new byte[CUSTOM_TLV_OFFSET + customByteArray.length];
System.arraycopy(OFOUI, 0, customValue, 0, OFOUI.length);
System.arraycopy(subtype, 0, customValue, OFOUI.length, 1);
- System.arraycopy(customByteArray, 0, customValue, customTlvOffset,
+ System.arraycopy(customByteArray, 0, customValue, CUSTOM_TLV_OFFSET,
customByteArray.length);
return customValue;
}
/**
- * Retrieves the string from TLV value and returns it in HexString format
+ * Retrieves the string from TLV value and returns it in HexString format.
*
* @param tlvValue
* the TLV value
* the TLV length
* @return the HexString
*/
- static public String getHexStringValue(final byte[] tlvValue, final int tlvLen) {
- byte[] cidBytes = new byte[tlvLen - chassisIDSubType.length];
- System.arraycopy(tlvValue, chassisIDSubType.length, cidBytes, 0,
+ public static String getHexStringValue(final byte[] tlvValue, final int tlvLen) {
+ byte[] cidBytes = new byte[tlvLen - CHASSISID_SUB_TYPE.length];
+ System.arraycopy(tlvValue, CHASSISID_SUB_TYPE.length, cidBytes, 0,
cidBytes.length);
return HexEncode.bytesToHexStringFormat(cidBytes);
}
/**
- * Retrieves the string from TLV value
+ * Retrieves the string from TLV value.
*
* @param tlvValue
* the TLV value
* the TLV length
* @return the string
*/
- static public String getStringValue(final byte[] tlvValue, final int tlvLen) {
- byte[] pidSubType = new byte[portIDSubType.length];
- byte[] pidBytes = new byte[tlvLen - portIDSubType.length];
+ public static String getStringValue(final byte[] tlvValue, final int tlvLen) {
+ byte[] pidSubType = new byte[PORTID_SUB_TYPE.length];
+ byte[] pidBytes = new byte[tlvLen - PORTID_SUB_TYPE.length];
System.arraycopy(tlvValue, 0, pidSubType, 0,
pidSubType.length);
- System.arraycopy(tlvValue, portIDSubType.length, pidBytes, 0,
+ System.arraycopy(tlvValue, PORTID_SUB_TYPE.length, pidBytes, 0,
pidBytes.length);
if (pidSubType[0] == (byte) 0x3) {
return HexEncode.bytesToHexStringFormat(pidBytes);
} else {
- return (new String(pidBytes, Charset.defaultCharset()));
+ return new String(pidBytes, Charset.defaultCharset());
}
}
/**
- * Retrieves the custom string from the Custom TLV value which includes OUI,
- * subtype and custom string
+ * Retrieves the custom string from the Custom TLV value which includes OUI, subtype and custom string.
*
* @param customTlvValue
* the custom TLV value
* the custom TLV length
* @return the custom string
*/
- static public String getCustomString(final byte[] customTlvValue, final int customTlvLen) {
+ public static String getCustomString(final byte[] customTlvValue, final int customTlvLen) {
String customString = "";
byte[] vendor = new byte[3];
System.arraycopy(customTlvValue, 0, vendor, 0, vendor.length);
if (Arrays.equals(vendor, LLDPTLV.OFOUI)) {
- int customArrayLength = customTlvLen - customTlvOffset;
+ int customArrayLength = customTlvLen - CUSTOM_TLV_OFFSET;
byte[] customArray = new byte[customArrayLength];
- System.arraycopy(customTlvValue, customTlvOffset, customArray, 0,
- customArrayLength);
+ System.arraycopy(customTlvValue, CUSTOM_TLV_OFFSET, customArray, 0, customArrayLength);
try {
customString = new String(customArray, "UTF-8");
} catch (final UnsupportedEncodingException e) {
+ LOG.warn("Error creating string from the custom TLV value: {}", Arrays.toString(customTlvValue), e);
}
}
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Utility class containing the common utility functions needed for operating on
- * networking data structures
+ * Utility class containing the common utility functions needed for operating on networking data structures.
*/
public abstract class NetUtils {
protected static final Logger LOG = LoggerFactory.getLogger(NetUtils.class);
+
/**
- * Constant holding the number of bits in a byte
+ * Constant holding the number of bits in a byte.
*/
- public static final int NumBitsInAByte = 8;
+ public static final int NUM_BITS_IN_A_BYTE = 8;
/**
- * Constant holding the number of bytes in MAC Address
+ * Constant holding the number of bytes in MAC Address.
*/
- public static final int MACAddrLengthInBytes = 6;
+ public static final int MAC_ADDR_LENGTH_IN_BYTES = 6;
/**
- * Constant holding the number of words in MAC Address
+ * Constant holding the number of words in MAC Address.
*/
- public static final int MACAddrLengthInWords = 3;
+ public static final int MAC_ADDR_LENGTH_IN_WORDS = 3;
/**
- * Constant holding the broadcast MAC address
+ * Constant holding the broadcast MAC address.
*/
- private static final byte[] BroadcastMACAddr = {-1, -1, -1, -1, -1, -1};
+ private static final byte[] BROADCAST_MAC_ADDR = {-1, -1, -1, -1, -1, -1};
/**
- * Converts a 4 bytes array into an integer number
+ * Converts a 4 bytes array into an integer number.
*
* @param ba
* the 4 bytes long byte array
* the length of it is not six.
*/
public static long byteArray6ToLong(final byte[] ba) {
- if (ba == null || ba.length != MACAddrLengthInBytes) {
+ if (ba == null || ba.length != MAC_ADDR_LENGTH_IN_BYTES) {
return 0L;
}
long num = 0L;
- int i = 0;
+ int index = 0;
do {
- num <<= NumBitsInAByte;
- num |= 0xff & ba[i];
- i++;
- } while (i < MACAddrLengthInBytes);
+ num <<= NUM_BITS_IN_A_BYTE;
+ num |= 0xff & ba[index];
+ index++;
+ } while (index < MAC_ADDR_LENGTH_IN_BYTES);
return num;
}
* The long number.
* @return The byte array.
*/
- public static byte[] longToByteArray6(long addr){
- byte[] mac = new byte[MACAddrLengthInBytes];
- int i = MACAddrLengthInBytes - 1;
+ public static byte[] longToByteArray6(long addr) {
+ byte[] mac = new byte[MAC_ADDR_LENGTH_IN_BYTES];
+ int index = MAC_ADDR_LENGTH_IN_BYTES - 1;
do {
- mac[i] = (byte) addr;
- addr >>>= NumBitsInAByte;
- i--;
- } while (i >= 0);
+ mac[index] = (byte) addr;
+ addr >>>= NUM_BITS_IN_A_BYTE;
+ index--;
+ } while (index >= 0);
return mac;
}
/**
- * Converts an integer number into a 4 bytes array
+ * Converts an integer number into a 4 bytes array.
*
- * @param i
+ * @param number
* the integer number
* @return the byte array
*/
- public static byte[] intToByteArray4(final int i) {
- return new byte[] { (byte) (i >> 24 & 0xff), (byte) (i >> 16 & 0xff), (byte) (i >> 8 & 0xff),
- (byte) (i & 0xff) };
+ public static byte[] intToByteArray4(final int number) {
+ return new byte[] { (byte) (number >> 24 & 0xff), (byte) (number >> 16 & 0xff), (byte) (number >> 8 & 0xff),
+ (byte) (number & 0xff) };
}
/**
- * Converts an IP address passed as integer value into the respective
- * InetAddress object
+ * Converts an IP address passed as integer value into the respective InetAddress object.
*
* @param address
* the IP address in integer form
* integer representing the length of the prefix network mask
* @param isV6
* boolean representing the IP version of the returned address
- * @return
*/
public static InetAddress getInetNetworkMask(final int prefixMaskLength, final boolean isV6) {
if (prefixMaskLength < 0 || !isV6 && prefixMaskLength > 32 || isV6 && prefixMaskLength > 128) {
return null;
}
- byte v4Address[] = { 0, 0, 0, 0 };
- byte v6Address[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- byte address[] = isV6 ? v6Address : v4Address;
+ byte[] v4Address = { 0, 0, 0, 0 };
+ byte[] v6Address = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+ byte[] address = isV6 ? v6Address : v4Address;
int numBytes = prefixMaskLength / 8;
int numBits = prefixMaskLength % 8;
- int i = 0;
- for (; i < numBytes; i++) {
- address[i] = (byte) 0xff;
+ int index = 0;
+ for (; index < numBytes; index++) {
+ address[index] = (byte) 0xff;
}
if (numBits > 0) {
int rem = 0;
for (int j = 0; j < numBits; j++) {
rem |= 1 << 7 - j;
}
- address[i] = (byte) rem;
+ address[index] = (byte) rem;
}
try {
if (subnetMask != null && (subnetMask.length == 4 || subnetMask.length == 16)) {
int index = 0;
while (index < subnetMask.length && subnetMask[index] == (byte) 0xFF) {
- maskLength += NetUtils.NumBitsInAByte;
+ maskLength += NetUtils.NUM_BITS_IN_A_BYTE;
index++;
}
if (index != subnetMask.length) {
- int bits = NetUtils.NumBitsInAByte - 1;
+ int bits = NetUtils.NUM_BITS_IN_A_BYTE - 1;
while (bits >= 0 && (subnetMask[index] & 1 << bits) != 0) {
bits--;
maskLength++;
/**
* Given an IP address and a prefix network mask length, it returns the
* equivalent subnet prefix IP address Example: for ip = "172.28.30.254" and
- * maskLen = 25 it will return "172.28.30.128"
+ * maskLen = 25 it will return "172.28.30.128".
*
* @param ip
* the IP address in InetAddress form
}
/**
- * Checks if the test address and mask conflicts with the filter address and
- * mask
+ * Checks if the test address and mask conflicts with the filter address and mask.
*
+ * <p>
* For example:
* testAddress: 172.28.2.23
* testMask: 255.255.255.0
* testMask: 255.255.255.0
* do conflict
*
+ * <p>
* testAddress: 172.28.2.23
* testMask: 255.255.255.0
* filterAddress: 172.28.1.10
* testMask: 255.255.0.0
* do not conflict
*
- * Null parameters are permitted
- *
- * @param testAddress
- * @param filterAddress
- * @param testMask
- * @param filterMask
- * @return
+ * <p>
+ * Null parameters are permitted.
*/
- public static boolean inetAddressConflict(final InetAddress testAddress, final InetAddress filterAddress, final InetAddress testMask,
- final InetAddress filterMask) {
+ public static boolean inetAddressConflict(final InetAddress testAddress, final InetAddress filterAddress,
+ final InetAddress testMask, final InetAddress filterMask) {
// Sanity check
if (testAddress == null || filterAddress == null) {
return false;
}
/**
- * Returns true if the passed MAC address is all zero
+ * Returns true if the passed MAC address is all zero.
*
* @param mac
* the byte array representing the MAC address
}
/**
- * Returns true if the MAC address is the broadcast MAC address and false
- * otherwise.
- *
- * @param MACAddress
- * @return
+ * Returns true if the MAC address is the broadcast MAC address and false otherwise.
*/
- public static boolean isBroadcastMACAddr(final byte[] MACAddress) {
- if (MACAddress.length == MACAddrLengthInBytes) {
+ public static boolean isBroadcastMACAddr(final byte[] macAddress) {
+ if (macAddress.length == MAC_ADDR_LENGTH_IN_BYTES) {
for (int i = 0; i < 6; i++) {
- if (MACAddress[i] != BroadcastMACAddr[i]) {
+ if (macAddress[i] != BROADCAST_MAC_ADDR[i]) {
return false;
}
}
return false;
}
+
/**
- * Returns true if the MAC address is a unicast MAC address and false
- * otherwise.
- *
- * @param MACAddress
- * @return
+ * Returns true if the MAC address is a unicast MAC address and false otherwise.
*/
- public static boolean isUnicastMACAddr(final byte[] MACAddress) {
- if (MACAddress.length == MACAddrLengthInBytes) {
- return (MACAddress[0] & 1) == 0;
+ public static boolean isUnicastMACAddr(final byte[] macAddress) {
+ if (macAddress.length == MAC_ADDR_LENGTH_IN_BYTES) {
+ return (macAddress[0] & 1) == 0;
}
return false;
}
* Returns true if the MAC address is a multicast MAC address and false
* otherwise. Note that this explicitly returns false for the broadcast MAC
* address.
- *
- * @param MACAddress
- * @return
*/
- public static boolean isMulticastMACAddr(final byte[] MACAddress) {
- if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) {
- return (MACAddress[0] & 1) != 0;
+ public static boolean isMulticastMACAddr(final byte[] macAddress) {
+ if (macAddress.length == MAC_ADDR_LENGTH_IN_BYTES && !isBroadcastMACAddr(macAddress)) {
+ return (macAddress[0] & 1) != 0;
}
return false;
}
/**
- * Returns true if the passed InetAddress contains all zero
+ * Returns true if the passed InetAddress contains all zero.
*
* @param ip
* the IP address to test
/**
* Checks if the passed IP v4 address in string form is valid The address
- * may specify a mask at the end as "/MM"
+ * may specify a mask at the end as "/MM".
*
* @param cidr
* the v4 address as A.B.C.D/MM
- * @return
*/
public static boolean isIPv4AddressValid(final String cidr) {
if (cidr == null) {
return false;
}
- String values[] = cidr.split("/");
+ String[] values = cidr.split("/");
Pattern ipv4Pattern = Pattern
.compile("(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])");
Matcher mm = ipv4Pattern.matcher(values[0]);
/**
* Checks if the passed IP v6 address in string form is valid The address
- * may specify a mask at the end as "/MMM"
+ * may specify a mask at the end as "/MMM".
*
* @param cidr
* the v6 address as A::1/MMM
- * @return
*/
public static boolean isIPv6AddressValid(final String cidr) {
if (cidr == null) {
return false;
}
- String values[] = cidr.split("/");
+ String[] values = cidr.split("/");
try {
// when given an IP address, InetAddress.getByName validates the ip
// address
*
* @param cidr
* the v4 or v6 address as IP/MMM
- * @return
*/
public static boolean isIPAddressValid(final String cidr) {
return NetUtils.isIPv4AddressValid(cidr) || NetUtils.isIPv6AddressValid(cidr);
* Following utilities are useful when you need to compare or bit shift java
* primitive type variable which are inherently signed
*/
+
/**
- * Returns the unsigned value of the passed byte variable
+ * Returns the unsigned value of the passed byte variable.
*
- * @param b
+ * @param value
* the byte value
* @return the int variable containing the unsigned byte value
*/
- public static int getUnsignedByte(final byte b) {
- return b & 0xFF;
+ public static int getUnsignedByte(final byte value) {
+ return value & 0xFF;
}
/**
- * Return the unsigned value of the passed short variable
+ * Return the unsigned value of the passed short variable.
*
- * @param s
+ * @param value
* the short value
* @return the int variable containing the unsigned short value
*/
- public static int getUnsignedShort(final short s) {
- return s & 0xFFFF;
+ public static int getUnsignedShort(final short value) {
+ return value & 0xFFFF;
}
/**
- * Returns the highest v4 or v6 InetAddress
+ * Returns the highest v4 or v6 InetAddress.
*
* @param v6
* true for IPv6, false for Ipv4
}
/**
- * Returns Broadcast MAC Address
+ * Returns Broadcast MAC Address.
*
* @return the byte array containing broadcast mac address
*/
public static byte[] getBroadcastMACAddr() {
- return Arrays.copyOf(BroadcastMACAddr, BroadcastMACAddr.length);
+ return Arrays.copyOf(BROADCAST_MAC_ADDR, BROADCAST_MAC_ADDR.length);
}
}
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
-
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract class which represents the generic network packet object It provides
* the basic methods which are common for all the packets, like serialize and
- * deserialize
+ * deserialize.
*/
-
public abstract class Packet {
protected static final Logger LOG = LoggerFactory.getLogger(Packet.class);
// Access level granted to this packet
/**
* This method deserializes the data bits obtained from the wire into the
- * respective header and payload which are of type Packet
+ * respective header and payload which are of type Packet.
*
* @param data - data from wire to deserialize
* @param bitOffset bit position where packet header starts in data
* array
* @param size size of packet in bits
* @return Packet
- * @throws PacketException
+ * @throws PacketException if deserialization fails
*/
public Packet deserialize(final byte[] data, final int bitOffset, final int size)
throws PacketException {
// Deserialize the header fields one by one
- int startOffset = 0, numBits = 0;
+ int startOffset = 0;
+ int numBits = 0;
for (Entry<String, Pair<Integer, Integer>> pairs : hdrFieldCoordMap
.entrySet()) {
String hdrField = pairs.getKey();
hdrFieldBytes = BitBufferHelper.getBits(data, startOffset,
numBits);
} catch (final BufferException e) {
- throw new PacketException(e.getMessage());
+ throw new PacketException("getBits failed", e);
}
/*
this.setHeaderField(hdrField, hdrFieldBytes);
if (LOG.isTraceEnabled()) {
- LOG.trace("{}: {}: {} (offset {} bitsize {})",
- new Object[] { this.getClass().getSimpleName(), hdrField,
- HexEncode.bytesToHexString(hdrFieldBytes),
- startOffset, numBits });
+ LOG.trace("{}: {}: {} (offset {} bitsize {})", this.getClass().getSimpleName(), hdrField,
+ HexEncode.bytesToHexString(hdrFieldBytes), startOffset, numBits);
}
}
// Deserialize the payload now
int payloadStart = startOffset + numBits;
- int payloadSize = data.length * NetUtils.NumBitsInAByte - payloadStart;
+ int payloadSize = data.length * NetUtils.NUM_BITS_IN_A_BYTE - payloadStart;
if (payloadClass != null) {
try {
payload = payloadClass.newInstance();
- } catch (final Exception e) {
- throw new RuntimeException(
- "Error parsing payload for Ethernet packet", e);
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new PacketException("Error parsing payload for Ethernet packet", e);
}
payload.deserialize(data, payloadStart, payloadSize);
payload.setParent(this);
* The payload class was not set, it means no class for parsing
* this payload is present. Let's store the raw payload if any.
*/
- int start = payloadStart / NetUtils.NumBitsInAByte;
- int stop = start + payloadSize / NetUtils.NumBitsInAByte;
+ int start = payloadStart / NetUtils.NUM_BITS_IN_A_BYTE;
+ int stop = start + payloadSize / NetUtils.NUM_BITS_IN_A_BYTE;
rawPayload = Arrays.copyOfRange(data, start, stop);
}
/**
* This method serializes the header and payload from the respective
- * packet class, into a single stream of bytes to be sent on the wire
+ * packet class, into a single stream of bytes to be sent on the wire.
*
* @return The byte array representing the serialized Packet
- * @throws PacketException
+ * @throws PacketException if serialization fails
*/
public byte[] serialize() throws PacketException {
int payloadSize = payloadBytes == null ? 0 : payloadBytes.length;
// Allocate the buffer to contain the full (header + payload) packet
- int headerSize = this.getHeaderSize() / NetUtils.NumBitsInAByte;
- byte packetBytes[] = new byte[headerSize + payloadSize];
+ int headerSize = this.getHeaderSize() / NetUtils.NUM_BITS_IN_A_BYTE;
+ byte[] packetBytes = new byte[headerSize + payloadSize];
if (payloadBytes != null) {
System.arraycopy(payloadBytes, 0, packetBytes, headerSize, payloadSize);
}
BitBufferHelper.setBytes(packetBytes, fieldBytes,
getfieldOffset(field), getfieldnumBits(field));
} catch (final BufferException e) {
- throw new PacketException(e.getMessage());
+ throw new PacketException("setBytes failed", e);
}
}
}
* for IPv4
*
* @param myBytes serialized bytes
- * @throws PacketException
+ * @throws PacketException on failure
*/
- protected void postSerializeCustomOperation(byte[] myBytes)
- throws PacketException {
+ protected void postSerializeCustomOperation(byte[] myBytes) throws PacketException {
// no op
}
*
* @param data The byte stream representing the Ethernet frame
* @param startBitOffset The bit offset from where the byte array corresponding to this Packet starts in the frame
- * @throws PacketException
+ * @throws PacketException on failure
*/
- protected void postDeserializeCustomOperation(byte[] data, int startBitOffset)
- throws PacketException {
+ protected void postDeserializeCustomOperation(byte[] data, int startBitOffset) throws PacketException {
// no op
}
/**
- * Gets the header length in bits
+ * Gets the header length in bits.
*
* @return int the header length in bits
*/
ret.append(HexEncode.bytesToHexString(value));
ret.append(", ");
}
- ret.replace(ret.length()-2, ret.length()-1, "]");
+ ret.replace(ret.length() - 2, ret.length() - 1, "]");
return ret.toString();
}
}
/**
- * Set a raw payload in the packet class
+ * Set a raw payload in the packet class.
*
- * @param payload The raw payload as byte array
+ * @param bytes The raw payload as byte array
*/
- public void setRawPayload(final byte[] payload) {
- this.rawPayload = Arrays.copyOf(payload, payload.length);
+ public void setRawPayload(final byte[] bytes) {
+ this.rawPayload = Arrays.copyOf(bytes, bytes.length);
}
/**
@Test
public void testBytesFromHexString() {
String byteStr1 = "00:11:22:33:44:55";
- byte byteArray1[] = HexEncode.bytesFromHexString(byteStr1);
+ byte[] byteArray1 = HexEncode.bytesFromHexString(byteStr1);
Assert.assertTrue(byteArray1[0] == (byte) 0x0);
Assert.assertTrue(byteArray1[1] == (byte) 0x11);
Assert.assertTrue(byteArray1[5] == (byte) 0x55);
String byteStr2 = "00:11:22:33:44:55";
- byte byteArray2[] = HexEncode.bytesFromHexString(byteStr2);
+ byte[] byteArray2 = HexEncode.bytesFromHexString(byteStr2);
Assert.assertFalse(byteArray2[0] == (byte) 0x55);
Assert.assertFalse(byteArray2[1] == (byte) 0x44);
*/
package org.opendaylight.openflowplugin.libraries.liblldp;
+import com.google.common.io.BaseEncoding;
+import com.google.common.primitives.Bytes;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.io.BaseEncoding;
-import com.google.common.primitives.Bytes;
-
-/**
- *
- */
+@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public class LLDPTLVTest {
- /** dummy custom tlv value */
+ /** dummy custom tlv value. */
private static final String CUSTOM_TLV_ULTIMATE = "What do you get when you multiply 6 by 9?";
- /** dummy custom tlv value in binary form */
+ /** dummy custom tlv value in binary form. */
private static final byte[] CUSTOM_TLV_ULTIMATE_BIN = new byte[] {
0x57, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x67, 0x65, 0x74,
0x20, 0x77, 0x68, 0x65, 0x6e, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69,
byte[] tlv = LLDPTLV.createCustomTLVValue(CUSTOM_TLV_ULTIMATE);
byte[] expectedCustomTlv = Bytes.concat(new byte[] {
- // custom type (7b) + length (9b) = 16b = 2B (skipped)
- // 0x7f, 24,
- // openflow OUI
- 0x00, 0x26, (byte) 0xe1,
- // subtype
- 0x00},
- // custom value
- CUSTOM_TLV_ULTIMATE_BIN);
+ // custom type (7b) + length (9b) = 16b = 2B (skipped)
+ // 0x7f, 24,
+ // openflow OUI
+ 0x00, 0x26, (byte) 0xe1,
+ // subtype
+ 0x00},
+ // custom value
+ CUSTOM_TLV_ULTIMATE_BIN);
BaseEncoding be = BaseEncoding.base16().withSeparator(" ", 2).lowerCase();
LOG.debug("expected: {}", be.encode(expectedCustomTlv));
/**
* Test method for
- * {@link org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV#getCustomString(byte[], int)}
- * .
- * @throws Exception
+ * {@link org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV#getCustomString(byte[], int)}.
*/
@Test
public void testGetCustomString() throws Exception {
byte[] inputCustomTlv = Bytes.concat(new byte[] {
- // custom type (7b) + length (9b) = 16b = 2B (skipped)
- // 0x7f, 24,
- // openflow OUI
- 0x00, 0x26, (byte) 0xe1,
- // subtype
- 0x00},
- // custom value
- CUSTOM_TLV_ULTIMATE_BIN);
+ // custom type (7b) + length (9b) = 16b = 2B (skipped)
+ // 0x7f, 24,
+ // openflow OUI
+ 0x00, 0x26, (byte) 0xe1,
+ // subtype
+ 0x00},
+ // custom value
+ CUSTOM_TLV_ULTIMATE_BIN);
String actual = LLDPTLV.getCustomString(inputCustomTlv, inputCustomTlv.length);
LOG.debug("actual custom TLV value as string: {}", actual);
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
-import org.junit.Before;
-import java.util.Iterator;
+
+import com.google.common.primitives.Bytes;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
-
import org.apache.commons.lang3.ArrayUtils;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
import org.junit.internal.ArrayComparisonFailure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.primitives.Bytes;
-
/**
- * Test of {@link LLDP} serialization feature (TODO: and deserialization)
+ * Test of {@link LLDP} serialization feature (TODO: and deserialization).
*/
public class LLDPTest {
private static final byte[] OUI_SUBTYPE_A = new byte[] { (byte) 0 };
private static final byte[] CUSTOM_SUBTYPE_A_VALUE = "first custom value A".getBytes();
- private static final short CUSTOM_SUBTYPE_A_LENGTH = (short) (OUI.length + OUI_SUBTYPE_A.length + CUSTOM_SUBTYPE_A_VALUE.length);
+ private static final short CUSTOM_SUBTYPE_A_LENGTH =
+ (short) (OUI.length + OUI_SUBTYPE_A.length + CUSTOM_SUBTYPE_A_VALUE.length);
private static final byte[] OUI_SUBTYPE_B = new byte[] { (byte) 1 };
private static final byte[] CUSTOM_SUBTYPE_B_VALUE = "second custom value B".getBytes();
- private static final short CUSTOM_SUBTYPE_B_LENGTH = (short) (OUI.length + OUI_SUBTYPE_B.length + CUSTOM_SUBTYPE_B_VALUE.length);
+ private static final short CUSTOM_SUBTYPE_B_LENGTH =
+ (short) (OUI.length + OUI_SUBTYPE_B.length + CUSTOM_SUBTYPE_B_VALUE.length);
private static final byte[] BYTES_BEFORE_CUSTOM_A = new byte[] { 0x00, 0x26, (byte) 0xe1, OUI_SUBTYPE_A[0] };
private static final byte[] BYTES_BEFORE_CUSTOM_B = new byte[] { 0x00, 0x26, (byte) 0xe1, OUI_SUBTYPE_B[0] };
}
/**
- * Tests whether serialization of LLDP packet is correct
+ * Tests whether serialization of LLDP packet is correct.
*
* @see LLDP#serialize()
- * @throws PacketException
*/
@Test
public void testSerialize() throws PacketException {
}
/**
- * Tests whether serialization of LLDP packet is correct
+ * Tests whether serialization of LLDP packet is correct.
*
* @see LLDP#deserialize(byte[], int, int)
- * @throws Exception
*/
@Test
public void testDeserialize() throws Exception {
awaitedBytes((byte) 0b11111110, CUSTOM_SUBTYPE_B_LENGTH, CUSTOM_SUBTYPE_B_VALUE,
BYTES_BEFORE_CUSTOM_B));
- lldpBuilder.deserialize(rawLldpTlv, 0, rawLldpTlv.length * NetUtils.NumBitsInAByte);
+ lldpBuilder.deserialize(rawLldpTlv, 0, rawLldpTlv.length * NetUtils.NUM_BITS_IN_A_BYTE);
Assert.assertEquals("chassis", new String(lldpBuilder.getChassisId().getValue()));
Assert.assertArrayEquals(TTL_VALUE, lldpBuilder.getTtl().getValue());
Assert.assertEquals("dummy port id", new String(lldpBuilder.getPortId().getValue()));
// custom items check
Iterable<LLDPTLV> customTlvs = lldpBuilder.getCustomTlvList();
- Iterator<LLDPTLV> iteratorLLDPTLV = customTlvs.iterator();
- assertEquals(true, iteratorLLDPTLV.hasNext());
- checkCustomTlv(iteratorLLDPTLV.next(), "first custom value A");
- assertEquals(true, iteratorLLDPTLV.hasNext());
- checkCustomTlv(iteratorLLDPTLV.next(), "second custom value B");
- assertEquals(false, iteratorLLDPTLV.hasNext());
+ Iterator<LLDPTLV> iterator = customTlvs.iterator();
+ assertEquals(true, iterator.hasNext());
+ checkCustomTlv(iterator.next(), "first custom value A");
+ assertEquals(true, iterator.hasNext());
+ checkCustomTlv(iterator.next(), "second custom value B");
+ assertEquals(false, iterator.hasNext());
}
/**
- * Test of {@link LLDP#addCustomTLV(LLDPTLV)}
- *
- * @throws PacketException
+ * Test of {@link LLDP#addCustomTLV(LLDPTLV)}.
*/
@Test
public void testAddCustomTLV() throws PacketException {
assertEquals(OUI_SUBTYPE_A[0], LLDPTLV.extractCustomSubtype(customTLV));
}
- /**
- * @param customItem
- * @param expectedValue
- */
private static void checkCustomTlv(final LLDPTLV customItem, final String expectedValue) {
Assert.assertEquals(127, customItem.getType());
LOG.debug("custom TLV1.length: {}", customItem.getLength());
new String(LLDPTLV.getCustomString(customItem.getValue(), customItem.getLength())));
}
- private static int checkTLV(final byte[] serializedData, final int offset, final byte typeTLVBits, final String typeTLVName,
- final short lengthTLV, final byte[] valueTLV, final byte... bytesBeforeValue) throws ArrayComparisonFailure {
+ private static int checkTLV(final byte[] serializedData, final int offset, final byte typeTLVBits,
+ final String typeTLVName, final short lengthTLV, final byte[] valueTLV, final byte... bytesBeforeValue)
+ throws ArrayComparisonFailure {
byte[] concreteTlvAwaited = awaitedBytes(typeTLVBits, lengthTLV, valueTLV, bytesBeforeValue);
int concreteTlvAwaitLength = concreteTlvAwaited.length;
assertArrayEquals("Serialization problem " + typeTLVName, concreteTlvAwaited,
return offset + concreteTlvAwaitLength;
}
- private static byte[] awaitedBytes(final byte typeTLV, final short length, final byte[] value, final byte[] bytesBeforeValue) {
+ private static byte[] awaitedBytes(final byte typeTLV, final short length, final byte[] value,
+ final byte[] bytesBeforeValue) {
byte[] awaited = ArrayUtils.EMPTY_BYTE_ARRAY;
// 0 - the less meaning byte (right), 1 most meaning byte (left)
return awaited;
}
- private static LLDPTLV dummyCustomTlv(final byte tlvType, final byte[] oui, final byte[] ouiSubtype, final short customLength,
- final byte[] subtypeValue) {
+ private static LLDPTLV dummyCustomTlv(final byte tlvType, final byte[] oui, final byte[] ouiSubtype,
+ final short customLength, final byte[] subtypeValue) {
byte[] fullCustomValue = new byte[0];
fullCustomValue = ArrayUtils.addAll(fullCustomValue, oui);
fullCustomValue = ArrayUtils.addAll(fullCustomValue, ouiSubtype);
// [01100011]
@Test
public void testGetBytes() throws Exception {
- byte data[] = { 108, 96, 125, -112, 5, 6, 108, 8, 9, 10, 11, 12, 13,
- 14, 15, 16, 17, 18, 19, 20, 21, 22 };
- byte[] x;
+ byte[] data = { 108, 96, 125, -112, 5, 6, 108, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 };
Assert.assertTrue(BitBufferHelper.getBits(data, 0, 8)[0] == 108);
Assert.assertTrue(BitBufferHelper.getBits(data, 8, 8)[0] == 96);
- x = BitBufferHelper.getBits(data, 0, 10);
- Assert.assertTrue(x[0] == 1);
- Assert.assertTrue(x[1] == -79);
+ byte[] bits = BitBufferHelper.getBits(data, 0, 10);
+ Assert.assertTrue(bits[0] == 1);
+ Assert.assertTrue(bits[1] == -79);
- x = BitBufferHelper.getBits(data, 3, 8);
- Assert.assertTrue(x[0] == 99);
+ bits = BitBufferHelper.getBits(data, 3, 8);
+ Assert.assertTrue(bits[0] == 99);
//Assert.assertTrue(x[1] == 97);
}
@Test
public void testToByteArray() {
short sh = Short.MAX_VALUE;
- byte[] data_sh = new byte[Byte.SIZE / 8];
- data_sh = BitBufferHelper.toByteArray(sh);
- Assert.assertTrue(data_sh[0] == 127);
- Assert.assertTrue(data_sh[1] == -1);
+ byte[] dataShort = new byte[Byte.SIZE / 8];
+ dataShort = BitBufferHelper.toByteArray(sh);
+ Assert.assertTrue(dataShort[0] == 127);
+ Assert.assertTrue(dataShort[1] == -1);
short sh2 = Short.MIN_VALUE;
- byte[] data_sh2 = new byte[Byte.SIZE / 8];
- data_sh2 = BitBufferHelper.toByteArray(sh2);
- Assert.assertTrue(data_sh2[0] == -128);
- Assert.assertTrue(data_sh2[1] == 0);
+ byte[] dataShort2 = new byte[Byte.SIZE / 8];
+ dataShort2 = BitBufferHelper.toByteArray(sh2);
+ Assert.assertTrue(dataShort2[0] == -128);
+ Assert.assertTrue(dataShort2[1] == 0);
short sh3 = 16384;
- byte[] data_sh3 = new byte[Byte.SIZE / 8];
- data_sh3 = BitBufferHelper.toByteArray(sh3);
- Assert.assertTrue(data_sh3[0] == 64);
- Assert.assertTrue(data_sh3[1] == 0);
+ byte[] dataShort3 = new byte[Byte.SIZE / 8];
+ dataShort3 = BitBufferHelper.toByteArray(sh3);
+ Assert.assertTrue(dataShort3[0] == 64);
+ Assert.assertTrue(dataShort3[1] == 0);
short sh4 = 146; //TCP headerlenflags - startoffset = 103
- byte[] data_sh4 = new byte[Byte.SIZE / 8];
- data_sh4 = BitBufferHelper.toByteArray(sh4);
- Assert.assertTrue(data_sh4[0] == 0);
- Assert.assertTrue(data_sh4[1] == -110);
-
- short sh4_2 = 5000; //IPv4 Offset - startOffset = 51 (to 63)
- byte[] data_sh4_2 = new byte[Byte.SIZE / 8];
- data_sh4_2 = BitBufferHelper.toByteArray(sh4_2);
- Assert.assertTrue(data_sh4_2[0] == 19);
- Assert.assertTrue(data_sh4_2[1] == -120);
-
- short sh4_3 = 5312; //numEndRestBits < numBitstoShiftBy
- byte[] data_sh4_3 = new byte[Byte.SIZE / 8];
- data_sh4_3 = BitBufferHelper.toByteArray(sh4_3);
- Assert.assertTrue(data_sh4_3[0] == 20);
- Assert.assertTrue(data_sh4_3[1] == -64);
-
- int Int = Integer.MAX_VALUE;
- byte[] data_Int = new byte[Integer.SIZE / 8];
- data_Int = BitBufferHelper.toByteArray(Int);
- Assert.assertTrue(data_Int[0] == 127);
- Assert.assertTrue(data_Int[1] == -1);
- Assert.assertTrue(data_Int[2] == -1);
- Assert.assertTrue(data_Int[3] == -1);
-
- int Int2 = Integer.MIN_VALUE;
- byte[] data_Int2 = new byte[Integer.SIZE / 8];
- data_Int2 = BitBufferHelper.toByteArray(Int2);
- Assert.assertTrue(data_Int2[0] == -128);
- Assert.assertTrue(data_Int2[1] == 0);
- Assert.assertTrue(data_Int2[2] == 0);
- Assert.assertTrue(data_Int2[3] == 0);
-
- int Int3 = 1077952576;
- byte[] data_Int3 = new byte[Integer.SIZE / 8];
- data_Int3 = BitBufferHelper.toByteArray(Int3);
- Assert.assertTrue(data_Int3[0] == 64);
- Assert.assertTrue(data_Int3[1] == 64);
- Assert.assertTrue(data_Int3[2] == 64);
- Assert.assertTrue(data_Int3[3] == 64);
-
- long Lng = Long.MAX_VALUE;
- byte[] data_lng = new byte[Long.SIZE / 8];
- data_lng = BitBufferHelper.toByteArray(Lng);
- Assert.assertTrue(data_lng[0] == 127);
- Assert.assertTrue(data_lng[1] == -1);
- Assert.assertTrue(data_lng[2] == -1);
- Assert.assertTrue(data_lng[3] == -1);
- Assert.assertTrue(data_lng[4] == -1);
- Assert.assertTrue(data_lng[5] == -1);
- Assert.assertTrue(data_lng[6] == -1);
- Assert.assertTrue(data_lng[7] == -1);
-
- long Lng2 = Long.MIN_VALUE;
- byte[] data_lng2 = new byte[Long.SIZE / 8];
- data_lng2 = BitBufferHelper.toByteArray(Lng2);
- Assert.assertTrue(data_lng2[0] == -128);
- Assert.assertTrue(data_lng2[1] == 0);
- Assert.assertTrue(data_lng2[2] == 0);
- Assert.assertTrue(data_lng2[3] == 0);
- Assert.assertTrue(data_lng2[4] == 0);
- Assert.assertTrue(data_lng2[5] == 0);
- Assert.assertTrue(data_lng2[6] == 0);
- Assert.assertTrue(data_lng2[7] == 0);
-
- byte B = Byte.MAX_VALUE;
- byte[] data_B = new byte[Byte.SIZE / 8];
- data_B = BitBufferHelper.toByteArray(B);
- Assert.assertTrue(data_B[0] == 127);
-
- byte B1 = Byte.MIN_VALUE;
- byte[] data_B1 = new byte[Byte.SIZE / 8];
- data_B1 = BitBufferHelper.toByteArray(B1);
- Assert.assertTrue(data_B1[0] == -128);
-
- byte B2 = 64;
- byte[] data_B2 = new byte[Byte.SIZE / 8];
- data_B2 = BitBufferHelper.toByteArray(B2);
- Assert.assertTrue(data_B2[0] == 64);
-
- byte B3 = 32;
- byte[] data_B3 = new byte[Byte.SIZE / 8];
- data_B3 = BitBufferHelper.toByteArray(B3);
- Assert.assertTrue(data_B3[0] == 32);
+ byte[] dataShort4 = new byte[Byte.SIZE / 8];
+ dataShort4 = BitBufferHelper.toByteArray(sh4);
+ Assert.assertTrue(dataShort4[0] == 0);
+ Assert.assertTrue(dataShort4[1] == -110);
+
+ short sh5 = 5000; //IPv4 Offset - startOffset = 51 (to 63)
+ byte[] dataShort5 = new byte[Byte.SIZE / 8];
+ dataShort5 = BitBufferHelper.toByteArray(sh5);
+ Assert.assertTrue(dataShort5[0] == 19);
+ Assert.assertTrue(dataShort5[1] == -120);
+
+ short sh6 = 5312; //numEndRestBits < numBitstoShiftBy
+ byte[] dataShort6 = new byte[Byte.SIZE / 8];
+ dataShort6 = BitBufferHelper.toByteArray(sh6);
+ Assert.assertTrue(dataShort6[0] == 20);
+ Assert.assertTrue(dataShort6[1] == -64);
+
+ int int1 = Integer.MAX_VALUE;
+ byte[] dataInt1 = new byte[Integer.SIZE / 8];
+ dataInt1 = BitBufferHelper.toByteArray(int1);
+ Assert.assertTrue(dataInt1[0] == 127);
+ Assert.assertTrue(dataInt1[1] == -1);
+ Assert.assertTrue(dataInt1[2] == -1);
+ Assert.assertTrue(dataInt1[3] == -1);
+
+ int int2 = Integer.MIN_VALUE;
+ byte[] dataInt2 = new byte[Integer.SIZE / 8];
+ dataInt2 = BitBufferHelper.toByteArray(int2);
+ Assert.assertTrue(dataInt2[0] == -128);
+ Assert.assertTrue(dataInt2[1] == 0);
+ Assert.assertTrue(dataInt2[2] == 0);
+ Assert.assertTrue(dataInt2[3] == 0);
+
+ int int3 = 1077952576;
+ byte[] dataInt3 = new byte[Integer.SIZE / 8];
+ dataInt3 = BitBufferHelper.toByteArray(int3);
+ Assert.assertTrue(dataInt3[0] == 64);
+ Assert.assertTrue(dataInt3[1] == 64);
+ Assert.assertTrue(dataInt3[2] == 64);
+ Assert.assertTrue(dataInt3[3] == 64);
+
+ long long1 = Long.MAX_VALUE;
+ byte[] dataLong1 = new byte[Long.SIZE / 8];
+ dataLong1 = BitBufferHelper.toByteArray(long1);
+ Assert.assertTrue(dataLong1[0] == 127);
+ Assert.assertTrue(dataLong1[1] == -1);
+ Assert.assertTrue(dataLong1[2] == -1);
+ Assert.assertTrue(dataLong1[3] == -1);
+ Assert.assertTrue(dataLong1[4] == -1);
+ Assert.assertTrue(dataLong1[5] == -1);
+ Assert.assertTrue(dataLong1[6] == -1);
+ Assert.assertTrue(dataLong1[7] == -1);
+
+ long long2 = Long.MIN_VALUE;
+ byte[] dataLong2 = new byte[Long.SIZE / 8];
+ dataLong2 = BitBufferHelper.toByteArray(long2);
+ Assert.assertTrue(dataLong2[0] == -128);
+ Assert.assertTrue(dataLong2[1] == 0);
+ Assert.assertTrue(dataLong2[2] == 0);
+ Assert.assertTrue(dataLong2[3] == 0);
+ Assert.assertTrue(dataLong2[4] == 0);
+ Assert.assertTrue(dataLong2[5] == 0);
+ Assert.assertTrue(dataLong2[6] == 0);
+ Assert.assertTrue(dataLong2[7] == 0);
+
+ byte byte1 = Byte.MAX_VALUE;
+ byte[] dataByte1 = new byte[Byte.SIZE / 8];
+ dataByte1 = BitBufferHelper.toByteArray(byte1);
+ Assert.assertTrue(dataByte1[0] == 127);
+
+ byte byte2 = Byte.MIN_VALUE;
+ byte[] dataByte2 = new byte[Byte.SIZE / 8];
+ dataByte2 = BitBufferHelper.toByteArray(byte2);
+ Assert.assertTrue(dataByte2[0] == -128);
+
+ byte byte3 = 64;
+ byte[] dataByte3 = new byte[Byte.SIZE / 8];
+ dataByte3 = BitBufferHelper.toByteArray(byte3);
+ Assert.assertTrue(dataByte3[0] == 64);
+
+ byte byte4 = 32;
+ byte[] dataByte4 = new byte[Byte.SIZE / 8];
+ dataByte4 = BitBufferHelper.toByteArray(byte4);
+ Assert.assertTrue(dataByte4[0] == 32);
}
@Test
public void testToByteArrayVariable() {
int len = 9;
- byte[] data_sh;
- data_sh = BitBufferHelper.toByteArray(511, len);
- Assert.assertTrue(data_sh[0] == (byte) 255);
- Assert.assertTrue(data_sh[1] == (byte) 128);
-
- data_sh = BitBufferHelper.toByteArray(511, len);
- Assert.assertTrue(data_sh[0] == (byte) 255);
- Assert.assertTrue(data_sh[1] == (byte) 128);
-
- data_sh = BitBufferHelper.toByteArray((long) 511, len);
- Assert.assertTrue(data_sh[0] == (byte) 255);
- Assert.assertTrue(data_sh[1] == (byte) 128);
+ byte[] dataShort;
+ dataShort = BitBufferHelper.toByteArray(511, len);
+ Assert.assertTrue(dataShort[0] == (byte) 255);
+ Assert.assertTrue(dataShort[1] == (byte) 128);
+
+ dataShort = BitBufferHelper.toByteArray(511, len);
+ Assert.assertTrue(dataShort[0] == (byte) 255);
+ Assert.assertTrue(dataShort[1] == (byte) 128);
+
+ dataShort = BitBufferHelper.toByteArray((long) 511, len);
+ Assert.assertTrue(dataShort[0] == (byte) 255);
+ Assert.assertTrue(dataShort[1] == (byte) 128);
}
@Test
public void testToInt() {
- byte data[] = { 1 };
+ byte[] data = { 1 };
Assert.assertTrue(BitBufferHelper.toNumber(data) == 1);
- byte data2[] = { 1, 1 };
+ byte[] data2 = { 1, 1 };
Assert.assertTrue(BitBufferHelper.toNumber(data2) == 257);
- byte data3[] = { 1, 1, 1 };
+ byte[] data3 = { 1, 1, 1 };
Assert.assertTrue(BitBufferHelper.toNumber(data3) == 65793);
}
@Test
public void testToLongGetter() {
- byte data[] = { 1, 1 };
+ byte[] data = { 1, 1 };
Assert.assertTrue(BitBufferHelper.getLong(data) == 257L);
}
// [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]*/
public void testInsertBits() throws Exception {
//CASE 1: startOffset%8 == 0 && numBits%8 == 0
- byte inputdata[] = { 75, 110, 107, 80, 10, 12, 35, 100, 125, 65 };
+ byte[] inputdata = { 75, 110, 107, 80, 10, 12, 35, 100, 125, 65 };
int startOffset = 0;
int numBits = 8;
- byte data1[] = new byte[2];
+ byte[] data1 = new byte[2];
startOffset = 0;
numBits = 16;
BitBufferHelper.insertBits(data1, inputdata, startOffset, numBits);
Assert.assertTrue(data1[0] == 75);
Assert.assertTrue(data1[1] == 110);
- byte data2[] = new byte[4];
+ byte[] data2 = new byte[4];
startOffset = 0;
numBits = 32;
BitBufferHelper.insertBits(data2, inputdata, startOffset, numBits);
Assert.assertTrue(data2[3] == 80);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] // OUTPUT: [01001011] [01101000] = {75, 104}
- byte data10[] = new byte[2];
+ // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [01001011] [01101000] = {75, 104}
+ byte[] data10 = new byte[2];
startOffset = 0;
numBits = 13;
BitBufferHelper.insertBits(data10, inputdata, startOffset, numBits);
Assert.assertTrue(data10[1] == 104);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] // OUTPUT: [01001000] = {72}
- byte data11[] = new byte[4];
+ // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [01001000] = {72}
+ byte[] data11 = new byte[4];
startOffset = 8;
numBits = 6;
BitBufferHelper.insertBits(data11, inputdata, startOffset, numBits);
Assert.assertTrue(data11[1] == 72);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [01001011] [01101110] [01101000] = {75, 110, 105}
- byte data12[] = new byte[4];
+ // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [01001011] [01101110] [01101000] = {75, 110, 105}
+ byte[] data12 = new byte[4];
startOffset = 0;
numBits = 23;
BitBufferHelper.insertBits(data12, inputdata, startOffset, numBits);
Assert.assertTrue(data12[2] == 106);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [01001011] [01101110] [01100000] = {75, 110, 96}
- byte data13[] = new byte[4];
+ // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [01001011] [01101110] [01100000] = {75, 110, 96}
+ byte[] data13 = new byte[4];
startOffset = 8;
numBits = 20;
BitBufferHelper.insertBits(data13, inputdata, startOffset, numBits);
Assert.assertTrue(data13[3] == 96);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [01001011] [01101110] [01101011] [10100000]= {75, 110, 107, 80}
- byte data14[] = new byte[4];
+ // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [01001011] [01101110] [01101011] [10100000]= {75, 110, 107, 80}
+ byte[] data14 = new byte[4];
startOffset = 0;
numBits = 30;
BitBufferHelper.insertBits(data14, inputdata, startOffset, numBits);
//CASE 3: startOffset%8 != 0, numBits%8 = 0
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00001001] [11000000] = {72, 96}
- byte data16[] = new byte[5];
+ // [01001011] [01101110] [01101011] [10100000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00001001] [11000000] = {72, 96}
+ byte[] data16 = new byte[5];
startOffset = 3;
numBits = 8;
BitBufferHelper.insertBits(data16, inputdata, startOffset, numBits);
Assert.assertTrue(data16[2] == 0);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: [00000100] [1011 0110] [1110 0000] = {4, -54, -96}
startOffset = 3;
numBits = 16;
- byte data17[] = new byte[5];
+ byte[] data17 = new byte[5];
BitBufferHelper.insertBits(data17, inputdata, startOffset, numBits);
Assert.assertTrue(data17[0] == 9);
Assert.assertTrue(data17[1] == 109);
// INPUT: {79, 110, 111}
// = [01001111] [01101110] [01101111]
//OUTPUT: [0000 1001] [1110 1101] [110 00000] = {9, -19, -64}
- byte data18[] = new byte[5];
- byte inputdata3[] = { 79, 110, 111 };
+ byte[] data18 = new byte[5];
+ byte[] inputdata3 = { 79, 110, 111 };
startOffset = 3;
numBits = 16;
BitBufferHelper.insertBits(data18, inputdata3, startOffset, numBits);
Assert.assertTrue(data18[2] == -64);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: [0000 1001] [0110 1101] [1100 1101] [0110 1010] [0000 0001] = {9, 109, -51, 106, 0}
startOffset = 3;
numBits = 32;
- byte data19[] = new byte[5];
+ byte[] data19 = new byte[5];
BitBufferHelper.insertBits(data19, inputdata, startOffset, numBits);
Assert.assertTrue(data19[0] == 9);
Assert.assertTrue(data19[1] == 109);
Assert.assertTrue(data19[4] == 0);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: data[4, 5, 6] = [0 010 0101] [1 011 0111] [0 000 0000] = {37, -73, 0}
startOffset = 33;
numBits = 16;
- byte data20[] = new byte[7];
+ byte[] data20 = new byte[7];
BitBufferHelper.insertBits(data20, inputdata, startOffset, numBits);
Assert.assertTrue(data20[4] == 37);
Assert.assertTrue(data20[5] == -73);
//CASE 4: extranumBits != 0 AND extraOffsetBits != 0
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: [0000 1001] [0100 0000] = {9, 96}
startOffset = 3;
numBits = 7;
- byte data21[] = new byte[7];
+ byte[] data21 = new byte[7];
BitBufferHelper.insertBits(data21, inputdata, startOffset, numBits);
Assert.assertTrue(data21[0] == 9);
Assert.assertTrue(data21[1] == 64);
Assert.assertTrue(data21[2] == 0);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: data = [00000 010] [01011 011] [01110 000] = {37, -73, 0}
startOffset = 5;
numBits = 17;
- byte data22[] = new byte[7];
+ byte[] data22 = new byte[7];
BitBufferHelper.insertBits(data22, inputdata, startOffset, numBits);
Assert.assertTrue(data22[0] == 2);
Assert.assertTrue(data22[1] == 91);
Assert.assertTrue(data22[2] == 112);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: [0000 1001] [0110 1101] [110 01101] [01 00000] = {9, 109, -51, 64}
startOffset = 3;
numBits = 23;
- byte data23[] = new byte[7];
+ byte[] data23 = new byte[7];
BitBufferHelper.insertBits(data23, inputdata, startOffset, numBits);
Assert.assertTrue(data23[0] == 9);
Assert.assertTrue(data23[1] == 109);
Assert.assertTrue(data23[3] == 64);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: [0000 1001] [0110 1101] = {9, 109}
startOffset = 3;
numBits = 13;
- byte data24[] = new byte[7];
+ byte[] data24 = new byte[7];
BitBufferHelper.insertBits(data24, inputdata, startOffset, numBits);
Assert.assertTrue(data24[0] == 9);
Assert.assertTrue(data24[1] == 109);
Assert.assertTrue(data24[2] == 0);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: [0000 0100] [1011 0110] [1110 0110] = {4, -74, -26}
startOffset = 4;
numBits = 20;
- byte data25[] = new byte[7];
+ byte[] data25 = new byte[7];
BitBufferHelper.insertBits(data25, inputdata, startOffset, numBits);
Assert.assertTrue(data25[0] == 4);
Assert.assertTrue(data25[1] == -74);
Assert.assertTrue(data25[3] == -0);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: [0000 0010] [0101 1011] = {0, 2, 91, 0}
startOffset = 13;
numBits = 11;
- byte data26[] = new byte[7];
+ byte[] data26 = new byte[7];
BitBufferHelper.insertBits(data26, inputdata, startOffset, numBits);
Assert.assertTrue(data26[0] == 0);
Assert.assertTrue(data26[1] == 2);
Assert.assertTrue(data26[3] == 0);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
// OUTPUT: [000 01001] [011 01101] [110 0 0000] = {9, 109, -64, 0}
startOffset = 3;
numBits = 17;
- byte data27[] = new byte[7];
+ byte[] data27 = new byte[7];
BitBufferHelper.insertBits(data27, inputdata, startOffset, numBits);
Assert.assertTrue(data27[0] == 9);
Assert.assertTrue(data27[1] == 109);
Assert.assertTrue(data27[3] == 0);
// INPUT: {75, 110, 107, 80, 10, 12, 35, 100, 125, 65} =
- // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001] //OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
- // OUTPUT: [00 000000] [00 000000] [00 010010] [11 011011] [10 011010] [11 010100] [0000 0000] = {0, 0, 18, -37,-102,-44,0}
+ // [01001011] [01101110] [01101011] [01010000] [00001010] [00001100] [00100011] [01100100] [11111101] [01000001]
+ // OUTPUT: [00000000] [00000100] [10110110] [11100000]= {0, 4, -54, -96}
+ // OUTPUT: [00 000000] [00 000000] [00 010010] [11 011011] [10 011010] [11 010100] [0000 0000] =
+ // {0, 0, 18, -37,-102,-44,0}
startOffset = 18;
numBits = 34;
- byte data28[] = new byte[7];
+ byte[] data28 = new byte[7];
BitBufferHelper.insertBits(data28, inputdata, startOffset, numBits);
Assert.assertTrue(data28[0] == 0);
Assert.assertTrue(data28[1] == 0);
@Test
public void testGetShort() throws Exception {
- byte data[] = new byte[2];
+ byte[] data = new byte[2];
data[0] = 7;
data[1] = 8;
int length = 9; // num bits
@Test
public void testToIntVarLength() throws Exception {
- byte data[] = { (byte) 255, (byte) 128 };
+ byte[] data = { (byte) 255, (byte) 128 };
int length = 9; // num bits
Assert.assertTrue(BitBufferHelper.getInt(data, length) == 384);
- byte data2[] = { 0, 8 };
+ byte[] data2 = { 0, 8 };
Assert.assertTrue(BitBufferHelper.getInt(data2, 9) == 8);
- byte data3[] = { 1, 1, 1 };
+ byte[] data3 = { 1, 1, 1 };
Assert.assertTrue(BitBufferHelper.getInt(data3) == 65793);
- byte data4[] = { 1, 1, 1 };
+ byte[] data4 = { 1, 1, 1 };
Assert.assertTrue(BitBufferHelper.getInt(data4) == 65793);
- byte data5[] = { 1, 1 };
+ byte[] data5 = { 1, 1 };
Assert.assertTrue(BitBufferHelper.getInt(data5) == 257);
}
}
@Test
- public void testShiftBitstoLSBMSB() {
+ public void testShiftBitsToLSBAndMSB() {
byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
byte[] clone = BitBufferHelper.shiftBitsToMSB(BitBufferHelper
// Exception is expected if NOT raised test will fail
Assert.assertTrue(false);
} catch (final ConstructionException e) {
+ // expected
}
// Array too short
// Exception is expected if NOT raised test will fail
Assert.assertTrue(false);
} catch (final ConstructionException e) {
+ // expected
}
// Array too long
try {
- ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
- (byte) 0x0 });
+ ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
+ (byte) 0x0, (byte) 0x0, (byte) 0x0 });
// Exception is expected if NOT raised test will fail
Assert.assertTrue(false);
} catch (final ConstructionException e) {
+ // expected
}
}
EthernetAddress ea2;
try {
ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
Assert.assertTrue(ea1.equals(ea2));
} catch (final ConstructionException e) {
// Exception is NOT expected if raised test will fail
try {
ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
ea2 = ea1.clone();
Assert.assertTrue(ea1.equals(ea2));
try {
ea1 = EthernetAddress.BROADCASTMAC;
ea2 = new EthernetAddress(new byte[] { (byte) 0xff, (byte) 0xff,
- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff });
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff });
Assert.assertTrue(ea1.equals(ea2));
} catch (final ConstructionException e) {
// Exception is NOT expected if raised test will fail
EthernetAddress ea2;
try {
ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x2 });
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x2 });
ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
Assert.assertTrue(!ea1.equals(ea2));
} catch (final ConstructionException e) {
// Exception is NOT expected if raised test will fail