/*
- * 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,
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;
// 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);
/**
* 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;
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;
try {
bits = BitBufferHelper.getBits(data, startOffset, numBits);
} catch (final BufferException e) {
- logger.error("", e);
+ LOG.error("", e);
}
return (int) toNumber(bits, numBits);
}
/**
* 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;
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];
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);
}
}
/**
* 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);
/**
* 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);
}
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;
}
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;
}
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;
}
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;
} 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;
// 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;
}
* 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[]
*
*/
} 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;
// 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) {
* 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) {
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;
}
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;
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);
}
}
* 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) {
}
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;
}
* 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;
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);
}
}
}