X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fcommons%2Fliblldp%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fliblldp%2FBitBufferHelper.java;fp=opendaylight%2Fcommons%2Fliblldp%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fliblldp%2FBitBufferHelper.java;h=a184d40d1ee96ed5d3986b3ffb3872fb76a269f0;hp=c2fa6c38d07eadc191bf640dbf72381d74e44ec3;hb=f6d4d8759fcce4d35f38f5a5a056acf1d3e0b4b2;hpb=3a25fec0b812950c61d2fb6018407a2a6ceb5090 diff --git a/opendaylight/commons/liblldp/src/main/java/org/opendaylight/controller/liblldp/BitBufferHelper.java b/opendaylight/commons/liblldp/src/main/java/org/opendaylight/controller/liblldp/BitBufferHelper.java index c2fa6c38d0..a184d40d1e 100644 --- a/opendaylight/commons/liblldp/src/main/java/org/opendaylight/controller/liblldp/BitBufferHelper.java +++ b/opendaylight/commons/liblldp/src/main/java/org/opendaylight/controller/liblldp/BitBufferHelper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -17,15 +17,13 @@ import org.slf4j.Logger; 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 + * 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. */ public abstract class BitBufferHelper { - protected static final Logger logger = LoggerFactory - .getLogger(BitBufferHelper.class); + protected static final Logger LOG = LoggerFactory.getLogger(BitBufferHelper.class); public static final long ByteMask = 0xFF; @@ -36,67 +34,67 @@ public abstract 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. + * * @return byte value */ public static byte getByte(final byte[] data) { - if ((data.length * NetUtils.NumBitsInAByte) > Byte.SIZE) { + if (data.length * NetUtils.NumBitsInAByte > Byte.SIZE) { try { - throw new BufferException( - "Container is too small for the number of requested bits"); + throw new BufferException("Container is too small for the number of requested bits"); } catch (final BufferException e) { - logger.error("", e); + LOG.error("", e); } } - return (data[0]); + return data[0]; } /** - * Returns the short value for the byte array passed. - * Size of byte array is restricted to Short.SIZE + * Returns the short value for the byte array passed. Size of byte array is + * restricted to Short.SIZE + * * @return short value */ 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"); + throw new BufferException("Container is too small for the number of requested bits"); } catch (final BufferException e) { - logger.error("", e); + LOG.error("", e); } } return (short) toNumber(data); } /** - * Returns the int value for the byte array passed. - * Size of byte array is restricted to Integer.SIZE + * 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"); + throw new BufferException("Container is too small for the number of requested bits"); } catch (final BufferException e) { - logger.error("", 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 + * 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"); + throw new BufferException("Container is too small for the number of requested bits"); } catch (final Exception e) { - logger.error("", e); + LOG.error("", e); } } return toNumber(data); @@ -105,15 +103,15 @@ public abstract class BitBufferHelper { /** * Returns the short value for the last numBits of the byte array passed. * Size of numBits is restricted to Short.SIZE + * * @return short - the short value of byte array */ 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"); + throw new BufferException("Container is too small for the number of requested bits"); } catch (final BufferException e) { - logger.error("", e); + LOG.error("", e); } } int startOffset = data.length * NetUtils.NumBitsInAByte - numBits; @@ -121,23 +119,23 @@ public abstract class BitBufferHelper { try { bits = BitBufferHelper.getBits(data, startOffset, numBits); } catch (final BufferException e) { - logger.error("", e); + LOG.error("", e); } return (short) toNumber(bits, numBits); } /** - * Returns the int value for the last numBits of the byte array passed. - * Size of numBits is restricted to Integer.SIZE + * Returns the int value for the last numBits of the byte array passed. Size + * of numBits is restricted to Integer.SIZE + * * @return int - the integer value of byte array */ 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"); + throw new BufferException("Container is too small for the number of requested bits"); } catch (final BufferException e) { - logger.error("", e); + LOG.error("", e); } } int startOffset = data.length * NetUtils.NumBitsInAByte - numBits; @@ -145,7 +143,7 @@ public abstract class BitBufferHelper { try { bits = BitBufferHelper.getBits(data, startOffset, numBits); } catch (final BufferException e) { - logger.error("", e); + LOG.error("", e); } return (int) toNumber(bits, numBits); } @@ -153,23 +151,22 @@ public abstract class BitBufferHelper { /** * Returns the long value for the last numBits of the byte array passed. * Size of numBits is restricted to Long.SIZE + * * @return long - the integer value of byte array */ 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"); + throw new BufferException("Container is too small for the number of requested bits"); } catch (final BufferException e) { - logger.error("", e); + LOG.error("", e); } } if (numBits > data.length * NetUtils.NumBitsInAByte) { try { - throw new BufferException( - "Trying to read more bits than contained in the data buffer"); + throw new BufferException("Trying to read more bits than contained in the data buffer"); } catch (final BufferException e) { - logger.error("", e); + LOG.error("", e); } } int startOffset = data.length * NetUtils.NumBitsInAByte - numBits; @@ -177,41 +174,37 @@ public abstract class BitBufferHelper { try { bits = BitBufferHelper.getBits(data, startOffset, numBits); } catch (final BufferException e) { - logger.error("", e); + LOG.error("", e); } return toNumber(bits, numBits); } /** - * Reads the specified number of bits from the passed byte array - * starting to read from the specified offset - * The bits read are stored in a byte array which size is dictated - * by the number of bits to be stored. - * The bits are stored in the byte array LSB aligned. + * Reads the specified number of bits from the passed byte array starting to + * read from the specified offset The bits read are stored in a byte array + * which size is dictated by the number of bits to be stored. The bits are + * stored in the byte array LSB aligned. * - * Ex. - * Read 7 bits at offset 10 - * 0 9 10 16 17 - * 0101000010 | 0000101 | 1111001010010101011 - * will be returned as {0,0,0,0,0,1,0,1} + * 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 startOffset - offset to start fetching bits from data from - * @param numBits - number of bits to be fetched from data + * @param startOffset + * - offset to start fetching bits from data from + * @param numBits + * - number of bits to be fetched from data * @return byte [] - LSB aligned bits * * @throws BufferException * when the startOffset and numBits parameters are not congruent * with the data buffer size */ - public static byte[] getBits(final byte[] data, final int startOffset, final int numBits) - throws BufferException { - + 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; + int numBytes = numBits % NetUtils.NumBitsInAByte != 0 ? 1 + numBits / NetUtils.NumBitsInAByte + : numBits / NetUtils.NumBitsInAByte; byte[] shiftedBytes = new byte[numBytes]; startByteOffset = startOffset / NetUtils.NumBitsInAByte; byte[] bytes = new byte[numBytes]; @@ -227,37 +220,31 @@ public abstract class BitBufferHelper { return bytes; } else { System.arraycopy(data, startByteOffset, bytes, 0, numBytes - 1); - bytes[numBytes - 1] = (byte) (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++) { // 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 + i] & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits); + valfromnext = data[startByteOffset + i + 1] & getMSBMask(extraOffsetBits); + bytes[i] = (byte) (valfromcurr << extraOffsetBits + | valfromnext >> NetUtils.NumBitsInAByte - 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)); + 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); + } else if (extranumBits == NetUtils.NumBitsInAByte - extraOffsetBits) { + valfromcurr = data[startByteOffset + i] & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits); bytes[i] = (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 + i] & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits); + valfromnext = data[startByteOffset + i + 1] + & getMSBMask(extranumBits - (NetUtils.NumBitsInAByte - extraOffsetBits)); + bytes[i] = (byte) (valfromcurr << extraOffsetBits + | valfromnext >> NetUtils.NumBitsInAByte - extraOffsetBits); } } @@ -275,17 +262,22 @@ public abstract class BitBufferHelper { /** * Bits are expected to be stored in the input byte array from LSB - * @param data to set the input byte - * @param input byte to be inserted - * @param startOffset offset of data[] to start inserting byte from - * @param numBits number of bits of input to be inserted into data[] + * + * @param data + * to set the input byte + * @param input + * byte to be inserted + * @param startOffset + * offset of data[] to start inserting byte from + * @param numBits + * number of bits of input to be inserted into data[] * * @throws BufferException * when the input, startOffset and numBits are not congruent * with the data buffer size */ - public static void setByte(final byte[] data, final byte input, final int startOffset, - final int numBits) throws BufferException { + public static void setByte(final byte[] data, final byte input, final int startOffset, final int numBits) + throws BufferException { byte[] inputByteArray = new byte[1]; Arrays.fill(inputByteArray, 0, 1, input); setBytes(data, inputByteArray, startOffset, numBits); @@ -293,16 +285,21 @@ public abstract class BitBufferHelper { /** * Bits are expected to be stored in the input byte array from LSB - * @param data to set the input byte - * @param input bytes to be inserted - * @param startOffset offset of data[] to start inserting byte from - * @param numBits number of bits of input to be inserted into data[] + * + * @param data + * to set the input byte + * @param input + * bytes to be inserted + * @param startOffset + * offset of data[] to start inserting byte from + * @param numBits + * number of bits of input to be inserted into data[] * @throws BufferException * when the startOffset and numBits parameters are not congruent * with data and input buffers' size */ - public static void setBytes(final byte[] data, final byte[] input, final int startOffset, - final int numBits) throws BufferException { + public static void setBytes(final byte[] data, final byte[] input, final int startOffset, final int numBits) + throws BufferException { checkExceptions(data, startOffset, numBits); insertBits(data, input, startOffset, numBits); } @@ -313,7 +310,7 @@ public abstract class BitBufferHelper { public static int getMSBMask(final int numBits) { int mask = 0; for (int i = 0; i < numBits; i++) { - mask = mask | (1 << (7 - i)); + mask = mask | 1 << 7 - i; } return mask; } @@ -324,7 +321,7 @@ public abstract class BitBufferHelper { public static int getLSBMask(final int numBits) { int mask = 0; for (int i = 0; i < numBits; i++) { - mask = mask | (1 << i); + mask = mask | 1 << i; } return mask; } @@ -343,8 +340,7 @@ public abstract class BitBufferHelper { if (value < 0) { value += 256; } - ret = ret - | (long) value << ((length - i - 1) * NetUtils.NumBitsInAByte); + ret = ret | (long) value << (length - i - 1) * NetUtils.NumBitsInAByte; } return ret; } @@ -363,18 +359,15 @@ public abstract class BitBufferHelper { 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.NumBitsInAByte; 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.NumBitsInAByte; } return ret; @@ -399,8 +392,7 @@ public abstract class BitBufferHelper { } else if (dataType == Long.class || dataType == long.class) { size = Long.SIZE; } else { - throw new IllegalArgumentException( - "Parameter must one of the following: Short/Int/Long\n"); + throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n"); } int length = size / NetUtils.NumBitsInAByte; @@ -408,8 +400,7 @@ public abstract class BitBufferHelper { // 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.NumBitsInAByte * (length - i - 1) & ByteMask); } return bytes; } @@ -419,7 +410,8 @@ public abstract class BitBufferHelper { * aligned form example: input = 5000 [1001110001000] bytes = -114, 64 * [10011100] [01000000] * - * @param numBits - the number of bits to be returned + * @param numBits + * - the number of bits to be returned * @return byte[] * */ @@ -435,8 +427,7 @@ public abstract class BitBufferHelper { } else if (dataType == Long.class) { size = Long.SIZE; } else { - throw new IllegalArgumentException( - "Parameter must one of the following: Short/Int/Long\n"); + throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n"); } int length = size / NetUtils.NumBitsInAByte; @@ -446,12 +437,10 @@ public abstract class BitBufferHelper { // 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.NumBitsInAByte * (length - i - 1) & ByteMask); } - 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) { @@ -480,7 +469,8 @@ public abstract class BitBufferHelper { * Example: For inputbytes = [00000111][01110001] and numBits = 12 it * returns: shiftedBytes = [01110111][00010000] * - * @param numBits - number of bits to be left aligned + * @param numBits + * - number of bits to be left aligned * @return byte[] */ public static byte[] shiftBitsToMSB(final byte[] inputBytes, final int numBits) { @@ -490,7 +480,7 @@ public abstract class BitBufferHelper { int i; for (i = 0; i < Byte.SIZE; i++) { - if (((byte) (inputBytes[0] & getMSBMask(i + 1))) != 0) { + if ((byte) (inputBytes[0] & getMSBMask(i + 1)) != 0) { leadZeroesMSB = i; break; } @@ -499,8 +489,8 @@ public abstract class BitBufferHelper { if (numBits % NetUtils.NumBitsInAByte == 0) { numBitstoShiftBy = 0; } else { - numBitstoShiftBy = ((NetUtils.NumBitsInAByte - (numBits % NetUtils.NumBitsInAByte)) < leadZeroesMSB) ? (NetUtils.NumBitsInAByte - (numBits % NetUtils.NumBitsInAByte)) - : leadZeroesMSB; + numBitstoShiftBy = NetUtils.NumBitsInAByte - numBits % NetUtils.NumBitsInAByte < leadZeroesMSB + ? NetUtils.NumBitsInAByte - numBits % NetUtils.NumBitsInAByte : leadZeroesMSB; } if (numBitstoShiftBy == 0) { return inputBytes; @@ -514,16 +504,23 @@ public abstract class BitBufferHelper { numEndRestBits = NetUtils.NumBitsInAByte - (inputBytes.length * NetUtils.NumBitsInAByte - numBits - numBitstoShiftBy); - for (i = 0; i < (size - 1); i++) { - if ((i + 1) == (size - 1)) { + for (i = 0; i < size - 1; i++) { + if (i + 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] & getLSBMask(numEndRestBits - - numBitstoShiftBy)) << numBitstoShiftBy); - } else - shiftedBytes[i] = (byte) ((inputBytes[i] << numBitstoShiftBy) | ((inputBytes[i + 1] & getMSBMask(numEndRestBits)) >> (NetUtils.NumBitsInAByte - numEndRestBits))); + shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy + | (inputBytes[i + 1] & getMSBMask(numBitstoShiftBy)) >> numEndRestBits + - numBitstoShiftBy); + shiftedBytes[i + 1] = (byte) ((inputBytes[i + 1] + & getLSBMask(numEndRestBits - numBitstoShiftBy)) << numBitstoShiftBy); + } else { + shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy + | (inputBytes[i + 1] & getMSBMask(numEndRestBits)) >> NetUtils.NumBitsInAByte + - numEndRestBits); + } } - shiftedBytes[i] = (byte) ((inputBytes[i] << numBitstoShiftBy) | (inputBytes[i + 1] & getMSBMask(numBitstoShiftBy)) >> (NetUtils.NumBitsInAByte - numBitstoShiftBy)); + shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy + | (inputBytes[i + 1] & getMSBMask(numBitstoShiftBy)) >> NetUtils.NumBitsInAByte + - numBitstoShiftBy); } } @@ -538,7 +535,8 @@ public abstract class BitBufferHelper { * returns: shiftedBytes = [00000111][01110001] * * @param inputBytes - * @param numBits - number of bits to be right aligned + * @param numBits + * - number of bits to be right aligned * @return byte[] */ public static byte[] shiftBitsToLSB(final byte[] inputBytes, final int numBits) { @@ -552,17 +550,16 @@ public abstract class BitBufferHelper { } 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.NumBitsInAByte - 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.NumBitsInAByte - 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.NumBitsInAByte - numBitstoShift); return shiftedBytes; } @@ -571,12 +568,13 @@ public abstract class BitBufferHelper { * of bits specified from the input data byte array. The input byte array * has the bits stored starting from the LSB */ - public static void insertBits(final byte[] data, final byte[] inputdataLSB, - final int startOffset, final int numBits) { + public static void insertBits(final byte[] data, final byte[] inputdataLSB, final int startOffset, + final int numBits) { byte[] inputdata = shiftBitsToMSB(inputdataLSB, numBits); // Align to - // MSB the - // passed byte - // array + // MSB the + // passed + // byte + // array int numBytes = numBits / NetUtils.NumBitsInAByte; int startByteOffset = startOffset / NetUtils.NumBitsInAByte; int extraOffsetBits = startOffset % NetUtils.NumBitsInAByte; @@ -595,88 +593,77 @@ public abstract class BitBufferHelper { 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))); + data[startByteOffset + numBytes] = (byte) (data[startByteOffset + numBytes] + | inputdata[numBytes] & getMSBMask(extranumBits)); } } else { for (i = 0; i < numBytes; i++) { if (i != 0) { - InputLSBbits = (inputdata[i - 1] & getLSBMask(extraOffsetBits)); + InputLSBbits = inputdata[i - 1] & getLSBMask(extraOffsetBits); } - InputMSBbits = (byte) (inputdata[i] & (getMSBMask(NetUtils.NumBitsInAByte - - extraOffsetBits))); - InputMSBbits = (InputMSBbits >= 0) ? InputMSBbits - : InputMSBbits + 256; + 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 << NetUtils.NumBitsInAByte - extraOffsetBits | InputMSBbits >> extraOffsetBits); InputMSBbits = InputLSBbits = 0; } - if (RestBits < (NetUtils.NumBitsInAByte - extraOffsetBits)) { + if (RestBits < NetUtils.NumBitsInAByte - extraOffsetBits) { if (numBytes != 0) { - InputLSBbits = (inputdata[i - 1] & getLSBMask(extraOffsetBits)); + 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)) { + 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)); + InputLSBbits = inputdata[i - 1] & getLSBMask(extraOffsetBits); } - InputMSBbits = (byte) (inputdata[i] & (getMSBMask(NetUtils.NumBitsInAByte - - extraOffsetBits))); - InputMSBbits = (InputMSBbits >= 0) ? InputMSBbits - : InputMSBbits + 256; + 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 << NetUtils.NumBitsInAByte - extraOffsetBits | InputMSBbits >> extraOffsetBits); } else { if (numBytes != 0) { - InputLSBbits = (inputdata[i - 1] & getLSBMask(extraOffsetBits)); + InputLSBbits = inputdata[i - 1] & getLSBMask(extraOffsetBits); } - InputMSBbits = (byte) (inputdata[i] & (getMSBMask(NetUtils.NumBitsInAByte - - extraOffsetBits))); - InputMSBbits = (InputMSBbits >= 0) ? InputMSBbits - : InputMSBbits + 256; + 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 << 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))); + InputLSBbits = inputdata[i] + & getLSBMask(RestBits - (NetUtils.NumBitsInAByte - extraOffsetBits)) << NetUtils.NumBitsInAByte + - RestBits; + data[startByteOffset + i + 1] = (byte) (data[startByteOffset + i + 1] + | InputLSBbits << NetUtils.NumBitsInAByte - extraOffsetBits); } } } /** * Checks for overflow and underflow exceptions - * @throws BufferException when the startOffset and numBits parameters - * are not congruent with the data buffer's size + * + * @throws BufferException + * when the startOffset and numBits parameters are not congruent + * with the data buffer's size */ public static void checkExceptions(final byte[] data, final int startOffset, final 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)); + endOffsetByte = startOffset / NetUtils.NumBitsInAByte + numBits / NetUtils.NumBitsInAByte + + (numBits % NetUtils.NumBitsInAByte != 0 ? 1 : startOffset % NetUtils.NumBitsInAByte != 0 ? 1 : 0); startByteOffset = startOffset / NetUtils.NumBitsInAByte; 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)) { - throw new BufferException( - "Illegal arguement/out of bound exception - data.length = " - + data.length + " startOffset = " + startOffset - + " numBits " + numBits); + if (startOffset < 0 || startByteOffset >= data.length || endOffsetByte > data.length || numBits < 0 + || numBits > NetUtils.NumBitsInAByte * data.length) { + throw new BufferException("Illegal arguement/out of bound exception - data.length = " + data.length + + " startOffset = " + startOffset + " numBits " + numBits); } } }