Fix checkstyle violations
[l2switch.git] / packethandler / implementation / src / main / java / org / opendaylight / l2switch / packethandler / decoders / utils / BitBufferHelper.java
index e74fd31a74569b762440637e0649546b668268af..c5cdbd2a2b195b79adaba9d4fe8901707598f9a4 100644 (file)
@@ -9,7 +9,6 @@
 package org.opendaylight.l2switch.packethandler.decoders.utils;
 
 import java.util.Arrays;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -17,12 +16,15 @@ import org.slf4j.LoggerFactory;
  * BitBufferHelper class that provides utility methods to - fetch specific bits
  * from a serialized stream of bits - convert bits to primitive data type - like
  * short, int, long - store bits in specified location in stream of bits -
- * convert primitive data types to stream of bits
+ * convert primitive data types to stream of bits.
  */
-public class BitBufferHelper {
+public final class BitBufferHelper {
+    private BitBufferHelper() {
+    }
+
     private 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
@@ -31,93 +33,31 @@ public class BitBufferHelper {
     // All this function return an exception if overflow or underflow
 
     /**
-     * Returns the first byte from the byte array
+     * Returns the first byte from the byte array.
      *
-     * @param data
+     * @param data the byte array
      * @return byte value
      */
     public static byte getByte(byte[] data) {
-        if ((data.length * NetUtils.NumBitsInAByte) > Byte.SIZE) {
-            try {
-                throw new BufferException("Container is too small for the number of requested bits");
-            } catch (BufferException e) {
-                LOG.error("", e);
-            }
-        }
-        return (data[0]);
-    }
-
-    /**
-     * Returns the short value for the byte array passed. Size of byte array is
-     * restricted to Short.SIZE
-     *
-     * @param data
-     * @return short value
-     */
-    public static short getShort(byte[] data) {
-        if (data.length > Short.SIZE) {
-            try {
-                throw new BufferException("Container is too small for the number of requested bits");
-            } catch (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 (short) toNumber(data);
-    }
-
-    /**
-     * Returns the int value for the byte array passed. Size of byte array is
-     * restricted to Integer.SIZE
-     *
-     * @param data
-     * @return int - the integer value of byte array
-     */
-    public static int getInt(byte[] data) {
-        if (data.length > Integer.SIZE) {
-            try {
-                throw new BufferException("Container is too small for the number of requested bits");
-            } catch (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
-     *
-     * @param data
-     * @return long - the integer value of byte array
-     */
-    public static long getLong(byte[] data) {
-        if (data.length > Long.SIZE) {
-            try {
-                throw new BufferException("Container is too small for the number of requested bits");
-            } catch (Exception e) {
-                LOG.error("", e);
-            }
-        }
-        return (long) toNumber(data);
+        return data[0];
     }
 
     /**
      * Returns the short value for the last numBits of the byte array passed.
-     * Size of numBits is restricted to Short.SIZE
+     * Size of numBits is restricted to Short.SIZE.
      *
-     * @param data
-     * @param numBits
+     * @param data the byte array
+     * @param numBits last number of bits
      * @return the short value of byte array
      */
     public static short getShort(byte[] data, int numBits) {
         if (numBits > Short.SIZE) {
-            try {
-                throw new BufferException("Container is too small for the number of requested bits");
-            } catch (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);
@@ -127,23 +67,33 @@ public class BitBufferHelper {
         return (short) toNumber(bits, numBits);
     }
 
+    /**
+     * Returns the short value for the byte array passed. Size of byte array is
+     * restricted to Short.SIZE.
+     *
+     * @param data the byte array
+     * @return short value
+     */
+    public static short getShort(byte[] data) {
+        if (data.length > Short.SIZE) {
+            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 last numBits of the byte array passed. Size
-     * of numBits is restricted to Integer.SIZE
+     * of numBits is restricted to Integer.SIZE.
      *
-     * @param data
-     * @param numBits
+     * @param data the byte array
+     * @param numBits the number of bits
      * @return the integer value of byte array
      */
     public static int getInt(byte[] data, int numBits) {
         if (numBits > Integer.SIZE) {
-            try {
-                throw new BufferException("Container is too small for the number of requested bits");
-            } catch (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);
@@ -153,37 +103,57 @@ public class BitBufferHelper {
         return (int) toNumber(bits, numBits);
     }
 
+    /**
+     * Returns the int value for the byte array passed. Size of byte array is
+     * restricted to Integer.SIZE.
+     *
+     * @param data the byte array
+     * @return int - the integer value of byte array
+     */
+    public static int getInt(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 long value for the last numBits of the byte array passed.
-     * Size of numBits is restricted to Long.SIZE
+     * Size of numBits is restricted to Long.SIZE.
      *
-     * @param data
-     * @param numBits
+     * @param data the byte array
+     * @param numBits the number of bits
      * @return the integer value of byte array
      */
     public static long getLong(byte[] data, int numBits) {
         if (numBits > Long.SIZE) {
-            try {
-                throw new BufferException("Container is too small for the number of requested bits");
-            } catch (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) {
-            try {
-                throw new BufferException("Trying to read more bits than contained in the data buffer");
-            } catch (BufferException e) {
-                LOG.error("", e);
-            }
+        if (numBits > data.length * NetUtils.NUM_BITS_IN_A_BYTE) {
+            LOG.error("getLong", new BufferException("Trying to read more bits than contained in the data buffer"));
         }
-        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);
         } catch (BufferException e) {
             LOG.error("", e);
         }
-        return (long) toNumber(bits, numBits);
+        return toNumber(bits, numBits);
+    }
+
+    /**
+     * Returns the long value for the byte array passed. Size of byte array is
+     * restricted to Long.SIZE.
+     *
+     * @param data the byte array
+     * @return long - the integer value of byte array
+     */
+    public static long getLong(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);
     }
 
     /**
@@ -191,11 +161,12 @@ public class BitBufferHelper {
      * read from the specified offset The bits read are stored in a byte array
      * which size is dictated by the number of bits to be stored. The bits are
      * stored in the byte array LSB aligned.
+     *
      * <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}
      *
-     * @param data
+     * @param data the byte array
      * @param startOffset
      *            offset to start fetching bits from data from
      * @param numBits
@@ -208,13 +179,11 @@ public class BitBufferHelper {
     public static byte[] getBits(byte[] data, int startOffset, 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;
@@ -228,38 +197,42 @@ public class BitBufferHelper {
                 return bytes;
             } else {
                 System.arraycopy(data, startByteOffset, bytes, 0, numBytes - 1);
-                bytes[numBytes - 1] = (byte) ((int) data[startByteOffset + numBytes - 1] & getMSBMask(extranumBits));
+                bytes[numBytes - 1] = (byte) (data[startByteOffset + numBytes - 1] & getMSBMask(extranumBits));
             }
         } else {
-            int i;
-            for (i = 0; i < numBits / NetUtils.NumBitsInAByte; i++) {
+            int index;
+            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)));
+                int 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) {
+                    int 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)));
+                    int 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
@@ -269,7 +242,7 @@ public class BitBufferHelper {
     // 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
      *            data to set the input byte
@@ -290,7 +263,7 @@ public class BitBufferHelper {
     }
 
     /**
-     * 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
      *            data to set the input byte
@@ -310,76 +283,69 @@ public class BitBufferHelper {
     }
 
     /**
-     * Returns numBits 1's in the MSB position
-     *
-     * @param numBits
-     * @return
+     * Returns numBits 1's in the MSB position.
      */
     public static int getMSBMask(int numBits) {
         int mask = 0;
         for (int i = 0; i < numBits; i++) {
-            mask = mask | (1 << (7 - i));
+            mask = mask | 1 << 7 - i;
         }
         return mask;
     }
 
     /**
-     * Returns numBits 1's in the LSB position
-     *
-     * @param numBits
-     * @return
+     * Returns numBits 1's in the LSB position.
      */
     public static int getLSBMask(int numBits) {
         int mask = 0;
         for (int i = 0; i < numBits; i++) {
-            mask = mask | (1 << i);
+            mask = mask | 1 << i;
         }
         return mask;
     }
 
     /**
-     * Returns the numerical value of the byte array passed
-     *
-     * @param array
-     * @return numerical value of byte array passed
+     * Returns the numerical value of the byte array passed.
      */
-    static public long toNumber(byte[] array) {
+    public static long toNumber(byte[] array) {
         long ret = 0;
         long length = array.length;
         int value = 0;
         for (int i = 0; i < length; i++) {
             value = array[i];
-            if (value < 0)
+            if (value < 0) {
                 value += 256;
-            ret = ret | (long) ((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
+     * array passed.
      *
-     * @param array
-     * @param numBits
+     * @param array the byte array
+     * @param numBits the number of bits
      * @return numerical value of byte array passed
      */
-    static public long toNumber(byte[] array, int numBits) {
-        int length = numBits / NetUtils.NumBitsInAByte;
-        int bitsRest = numBits % NetUtils.NumBitsInAByte;
+    public static long toNumber(byte[] array, 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));
+        value = array[startOffset - 1] < 0 ? array[startOffset - 1] + 256 : array[startOffset - 1];
+        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)
+            if (value < 0) {
                 value += 256;
-            ret = ret | (long) ((long) value << ((array.length - i - 1) * NetUtils.NumBitsInAByte));
+            }
+            ret = ret | (long) value << (array.length - i - 1) * NetUtils.NUM_BITS_IN_A_BYTE;
         }
 
         return ret;
@@ -388,10 +354,7 @@ public class BitBufferHelper {
     /**
      * 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]
-     *
-     * @param input
-     * @return byte[]
+     * [00010011] [10001000].
      */
 
     public static byte[] toByteArray(Number input) {
@@ -411,12 +374,12 @@ public class BitBufferHelper {
             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;
     }
@@ -424,12 +387,12 @@ public class BitBufferHelper {
     /**
      * 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 input
+     * @param input inout Number
      * @param numBits
      *            the number of bits to be returned
-     * @return byte[]
+     * @return byte[] a byte array
      */
     public static byte[] toByteArray(Number input, int numBits) {
         Class<? extends Number> dataType = input.getClass();
@@ -446,17 +409,17 @@ public class BitBufferHelper {
             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)) {
+        if (bytes[0] == 0 && dataType == Long.class || bytes[0] == 0 && dataType == Integer.class) {
             int index = 0;
             for (index = 0; index < length; ++index) {
                 if (bytes[index] != 0) {
@@ -475,70 +438,66 @@ public class BitBufferHelper {
         return shiftedBytes;
     }
 
-    /**
-     * Takes an LSB aligned byte array and returned the LSB numBits in a MSB
-     * aligned byte array
-     *
-     * @param inputbytes
-     * @param numBits
-     * @return
-     */
     /**
      * 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]
      *
-     * @param inputBytes
+     * @param inputBytes the byte array
      * @param numBits
      *            number of bits to be left aligned
-     * @return byte[]
+     * @return byte[] a byte array
      */
     public static byte[] shiftBitsToMSB(byte[] inputBytes, 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;
+        int index;
 
-        for (i = 0; i < Byte.SIZE; i++) {
-            if (((byte) (inputBytes[0] & getMSBMask(i + 1))) != 0) {
-                leadZeroesMSB = i;
+        for (index = 0; index < Byte.SIZE; index++) {
+            if ((byte) (inputBytes[0] & getMSBMask(index + 1)) != 0) {
+                leadZeroesMSB = index;
                 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++) {
-                if ((i + 1) == (size - 1)) {
+            for (index = 0; index < size - 1; index++) {
+                if (index + 1 == size - 1) {
                     if (numEndRestBits > numBitstoShiftBy) {
-                        shiftedBytes[i] = (byte) ((inputBytes[i] << numBitstoShiftBy) | ((inputBytes[i + 1]
-                                & getMSBMask(numBitstoShiftBy)) >> (numEndRestBits - numBitstoShiftBy)));
-                        shiftedBytes[i + 1] = (byte) ((inputBytes[i + 1]
+                        shiftedBytes[index] = (byte) (inputBytes[index] << numBitstoShiftBy | (inputBytes[index + 1]
+                                & getMSBMask(numBitstoShiftBy)) >> numEndRestBits - numBitstoShiftBy);
+                        shiftedBytes[index + 1] = (byte) ((inputBytes[index + 1]
                                 & getLSBMask(numEndRestBits - numBitstoShiftBy)) << numBitstoShiftBy);
-                    } else
-                        shiftedBytes[i] = (byte) ((inputBytes[i] << numBitstoShiftBy) | ((inputBytes[i + 1]
-                                & getMSBMask(numEndRestBits)) >> (NetUtils.NumBitsInAByte - numEndRestBits)));
+                    } else {
+                        shiftedBytes[index] = (byte) (inputBytes[index] << numBitstoShiftBy | (inputBytes[index + 1]
+                                & getMSBMask(numEndRestBits)) >> NetUtils.NUM_BITS_IN_A_BYTE - numEndRestBits);
+                    }
                 }
-                shiftedBytes[i] = (byte) ((inputBytes[i] << numBitstoShiftBy) | (inputBytes[i + 1]
-                        & getMSBMask(numBitstoShiftBy)) >> (NetUtils.NumBitsInAByte - numBitstoShiftBy));
+                shiftedBytes[index] = (byte) (inputBytes[index] << numBitstoShiftBy | (inputBytes[index + 1]
+                        & getMSBMask(numBitstoShiftBy)) >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShiftBy);
             }
 
         }
@@ -548,61 +507,63 @@ public class BitBufferHelper {
     /**
      * 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 the byte array
      * @param numBits
      *            number of bits to be right aligned
-     * @return byte[]
+     * @return byte[] a byte array
      */
     public static byte[] shiftBitsToLSB(byte[] inputBytes, 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 = (inputLsb < 0) ? (inputLsb + 256) : inputLsb;
+            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 = inputBytes[i] < 0 ? inputBytes[i] + 256 : inputBytes[i];
+            shiftedBytes[i] = (byte) (inputLsb << 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));
+        inputMsb = inputBytes[0] & getMSBMask(numBitstoShift);
+        inputMsb = inputMsb < 0 ? inputMsb + 256 : inputMsb;
+        shiftedBytes[0] = (byte) (inputMsb >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
         return shiftedBytes;
     }
 
     /**
      * Insert in the data buffer at position dictated by the offset the number
      * of bits specified from the input data byte array. The input byte array
-     * has the bits stored starting from the LSB
+     * has the bits stored starting from the LSB.
      *
-     * @param data
-     * @param inputdataLSB
-     * @param startOffset
-     * @param numBits
+     * @param data the byte array
+     * @param inputdataLSB LSB array
+     * @param startOffset start offset
+     * @param numBits the number of bits
      */
     public static void insertBits(byte[] data, byte[] inputdataLSB, int startOffset, int numBits) {
         byte[] inputdata = shiftBitsToMSB(inputdataLSB, numBits); // Align to
         // MSB the
         // 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;
@@ -610,63 +571,68 @@ public class BitBufferHelper {
 
         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);
                 data[startByteOffset + numBytes] = (byte) (data[startByteOffset + numBytes]
-                        | (inputdata[numBytes] & getMSBMask(extranumBits)));
+                        | inputdata[numBytes] & getMSBMask(extranumBits));
             }
         } else {
-            for (i = 0; i < numBytes; i++) {
-                if (i != 0)
-                    InputLSBbits = (inputdata[i - 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;
+            int index;
+            for (index = 0; index < numBytes; index++) {
+                if (index != 0) {
+                    inputLSBbits = inputdata[index - 1] & getLSBMask(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);
+                inputMSBbits = inputLSBbits = 0;
             }
-            if (RestBits < (NetUtils.NumBitsInAByte - extraOffsetBits)) {
-                if (numBytes != 0)
-                    InputLSBbits = (inputdata[i - 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)) {
-                if (numBytes != 0)
-                    InputLSBbits = (inputdata[i - 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));
+            if (restBits < NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
+                if (numBytes != 0) {
+                    inputLSBbits = inputdata[index - 1] & getLSBMask(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[index - 1] & getLSBMask(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));
-                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)));
+                if (numBytes != 0) {
+                    inputLSBbits = inputdata[index - 1] & getLSBMask(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.
      *
-     * @param data
-     * @param startOffset
-     * @param numBits
+     * @param data the byte array
+     * @param startOffset start offset
+     * @param numBits the number of bits
      * @throws BufferException
      *             when the startOffset and numBits parameters are not congruent
      *             with the data buffer's size
@@ -674,17 +640,17 @@ public class BitBufferHelper {
     public static void checkExceptions(byte[] data, int startOffset, int numBits) 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)) {
+        if (startOffset < 0 || startByteOffset >= data.length || endOffsetByte > data.length || numBits < 0
+                || 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);
         }