Merge "Fix BitBufferHelper::toNumber()"
[openflowplugin.git] / libraries / liblldp / src / main / java / org / opendaylight / openflowplugin / libraries / liblldp / BitBufferHelper.java
1 /*
2  * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.libraries.liblldp;
9
10 import java.util.Arrays;
11 import javax.annotation.Nonnull;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 /**
16  * BitBufferHelper class that provides utility methods to - fetch specific bits
17  * from a serialized stream of bits - convert bits to primitive data type - like
18  * short, int, long - store bits in specified location in stream of bits -
19  * convert primitive data types to stream of bits.
20  */
21 public abstract class BitBufferHelper {
22     private static final Logger LOG = LoggerFactory.getLogger(BitBufferHelper.class);
23
24     public static final long BYTE_MASK = 0xFF;
25
26     // Getters
27     // data: array where data are stored
28     // startOffset: bit from where to start reading
29     // numBits: number of bits to read
30     // All this function return an exception if overflow or underflow
31
32     /**
33      * Returns the first byte from the byte array.
34      *
35      * @return byte value
36      */
37     public static byte getByte(final byte[] data) {
38         if (data.length * NetUtils.NUM_BITS_IN_A_BYTE > Byte.SIZE) {
39             LOG.error("getByte", new BufferException("Container is too small for the number of requested bits"));
40         }
41         return data[0];
42     }
43
44     /**
45      * Returns the short value for the byte array passed. Size of byte array is
46      * restricted to Short.SIZE
47      *
48      * @return short value
49      */
50     public static short getShort(final byte[] data) {
51         if (data.length > Short.SIZE) {
52             LOG.error("getShort", new BufferException("Container is too small for the number of requested bits"));
53         }
54         return (short) toNumber(data);
55     }
56
57     /**
58      * Returns the short value for the last numBits of the byte array passed.
59      * Size of numBits is restricted to Short.SIZE
60      *
61      * @return short - the short value of byte array
62      */
63     public static short getShort(final byte[] data, final int numBits) {
64         if (numBits > Short.SIZE) {
65             LOG.error("getShort", new BufferException("Container is too small for the number of requested bits"));
66         }
67         int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
68         try {
69             byte[] bits = BitBufferHelper.getBits(data, startOffset, numBits);
70             return (short) toNumber(bits, numBits);
71         } catch (final BufferException e) {
72             LOG.error("getBits failed", e);
73         }
74         return 0;
75     }
76
77     /**
78      * Returns the int value for the byte array passed. Size of byte array is
79      * restricted to Integer.SIZE
80      *
81      * @return int - the integer value of byte array
82      */
83     public static int getInt(final byte[] data) {
84         if (data.length > Integer.SIZE) {
85             LOG.error("getInt", new BufferException("Container is too small for the number of requested bits"));
86         }
87         return (int) toNumber(data);
88     }
89
90     /**
91      * Returns the int value for the last numBits of the byte array passed. Size
92      * of numBits is restricted to Integer.SIZE
93      *
94      * @return int - the integer value of byte array
95      */
96     public static int getInt(final byte[] data, final int numBits) {
97         if (numBits > Integer.SIZE) {
98             LOG.error("getInt", new BufferException("Container is too small for the number of requested bits"));
99         }
100         int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
101         try {
102             byte[] bits = BitBufferHelper.getBits(data, startOffset, numBits);
103             return (int) toNumber(bits, numBits);
104         } catch (final BufferException e) {
105             LOG.error("getBits failed", e);
106         }
107         return 0;
108     }
109
110     /**
111      * Returns the long value for the byte array passed. Size of byte array is
112      * restricted to Long.SIZE
113      *
114      * @return long - the integer value of byte array
115      */
116     public static long getLong(final byte[] data) {
117         if (data.length > Long.SIZE) {
118             LOG.error("getLong", new BufferException("Container is too small for the number of requested bits"));
119         }
120         return toNumber(data);
121     }
122
123     /**
124      * Returns the long value for the last numBits of the byte array passed.
125      * Size of numBits is restricted to Long.SIZE
126      *
127      * @return long - the integer value of byte array
128      */
129     public static long getLong(final byte[] data, final int numBits) {
130         if (numBits > Long.SIZE) {
131             LOG.error("getLong", new BufferException("Container is too small for the number of requested bits"));
132         }
133         if (numBits > data.length * NetUtils.NUM_BITS_IN_A_BYTE) {
134             try {
135                 throw new BufferException("Trying to read more bits than contained in the data buffer");
136             } catch (final BufferException e) {
137                 LOG.error("", e);
138             }
139         }
140         int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
141         try {
142             byte[] bits = BitBufferHelper.getBits(data, startOffset, numBits);
143             return toNumber(bits, numBits);
144         } catch (final BufferException e) {
145             LOG.error("getBits failed", e);
146         }
147         return 0;
148     }
149
150     /**
151      * Reads the specified number of bits from the passed byte array starting to
152      * read from the specified offset The bits read are stored in a byte array
153      * which size is dictated by the number of bits to be stored. The bits are
154      * stored in the byte array LSB aligned.
155      *
156      * <p>
157      * Ex. Read 7 bits at offset 10 0 9 10 16 17 0101000010 | 0000101 |
158      * 1111001010010101011 will be returned as {0,0,0,0,0,1,0,1}
159      *
160      * @param startOffset
161      *            - offset to start fetching bits from data from
162      * @param numBits
163      *            - number of bits to be fetched from data
164      * @return byte [] - LSB aligned bits
165      *
166      * @throws BufferException
167      *             when the startOffset and numBits parameters are not congruent
168      *             with the data buffer size
169      */
170     @Nonnull
171     public static byte[] getBits(final byte[] data, final int startOffset, final int numBits) throws BufferException {
172         int startByteOffset;
173         int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
174         final int extraOffsetBits = startOffset % NetUtils.NUM_BITS_IN_A_BYTE;
175         int numBytes = numBits % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1 + numBits / NetUtils.NUM_BITS_IN_A_BYTE
176                 : numBits / NetUtils.NUM_BITS_IN_A_BYTE;
177         startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE;
178         byte[] bytes = new byte[numBytes];
179         if (numBits == 0) {
180             return bytes;
181         }
182
183         checkExceptions(data, startOffset, numBits);
184
185         if (extraOffsetBits == 0) {
186             if (extranumBits == 0) {
187                 System.arraycopy(data, startByteOffset, bytes, 0, numBytes);
188                 return bytes;
189             } else {
190                 System.arraycopy(data, startByteOffset, bytes, 0, numBytes - 1);
191                 bytes[numBytes - 1] = (byte) (data[startByteOffset + numBytes - 1] & getMSBMask(extranumBits));
192             }
193         } else {
194             int index;
195             int valfromcurr;
196             int valfromnext;
197             for (index = 0; index < numBits / NetUtils.NUM_BITS_IN_A_BYTE; index++) {
198                 // Reading numBytes starting from offset
199                 valfromcurr = data[startByteOffset + index] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
200                 valfromnext = data[startByteOffset + index + 1] & getMSBMask(extraOffsetBits);
201                 bytes[index] = (byte) (valfromcurr << extraOffsetBits
202                         | valfromnext >> NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
203             }
204             // Now adding the rest of the bits if any
205             if (extranumBits != 0) {
206                 if (extranumBits < NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
207                     valfromnext = (byte) (data[startByteOffset + index] & getMSBMask(extranumBits) >> extraOffsetBits);
208                     bytes[index] = (byte) (valfromnext << extraOffsetBits);
209                 } else if (extranumBits == NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
210                     valfromcurr = data[startByteOffset + index]
211                             & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
212                     bytes[index] = (byte) (valfromcurr << extraOffsetBits);
213                 } else {
214                     valfromcurr = data[startByteOffset + index]
215                             & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
216                     valfromnext = data[startByteOffset + index + 1]
217                             & getMSBMask(extranumBits - (NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
218                     bytes[index] = (byte) (valfromcurr << extraOffsetBits
219                             | valfromnext >> NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
220                 }
221
222             }
223         }
224         // Aligns the bits to LSB
225         return shiftBitsToLSB(bytes, numBits);
226     }
227
228     // Setters
229     // data: array where data will be stored
230     // input: the data that need to be stored in the data array
231     // startOffset: bit from where to start writing
232     // numBits: number of bits to read
233
234     /**
235      * Bits are expected to be stored in the input byte array from LSB.
236      *
237      * @param data
238      *            to set the input byte
239      * @param input
240      *            byte to be inserted
241      * @param startOffset
242      *            offset of data[] to start inserting byte from
243      * @param numBits
244      *            number of bits of input to be inserted into data[]
245      *
246      * @throws BufferException
247      *             when the input, startOffset and numBits are not congruent
248      *             with the data buffer size
249      */
250     public static void setByte(final byte[] data, final byte input, final int startOffset, final int numBits)
251             throws BufferException {
252         byte[] inputByteArray = new byte[1];
253         Arrays.fill(inputByteArray, 0, 1, input);
254         setBytes(data, inputByteArray, startOffset, numBits);
255     }
256
257     /**
258      * Bits are expected to be stored in the input byte array from LSB.
259      *
260      * @param data
261      *            to set the input byte
262      * @param input
263      *            bytes to be inserted
264      * @param startOffset
265      *            offset of data[] to start inserting byte from
266      * @param numBits
267      *            number of bits of input to be inserted into data[]
268      * @throws BufferException
269      *             when the startOffset and numBits parameters are not congruent
270      *             with data and input buffers' size
271      */
272     public static void setBytes(final byte[] data, final byte[] input, final int startOffset, final int numBits)
273             throws BufferException {
274         checkExceptions(data, startOffset, numBits);
275         insertBits(data, input, startOffset, numBits);
276     }
277
278     /**
279      * Returns numBits 1's in the MSB position.
280      */
281     public static int getMSBMask(final int numBits) {
282         int mask = 0;
283         for (int i = 0; i < numBits; i++) {
284             mask = mask | 1 << 7 - i;
285         }
286         return mask;
287     }
288
289     /**
290      * Returns numBits 1's in the LSB position.
291      */
292     public static int getLSBMask(final int numBits) {
293         int mask = 0;
294         for (int i = 0; i < numBits; i++) {
295             mask = mask | 1 << i;
296         }
297         return mask;
298     }
299
300     /**
301      * Returns the numerical value of the byte array passed.
302      *
303      * @return long - numerical value of byte array passed
304      */
305     public static long toNumber(final byte[] array) {
306         long ret = 0;
307         for (byte anArray : array) {
308             int value = anArray;
309             if (value < 0) {
310                 value += 256;
311             }
312             ret = ret << Byte.SIZE | value;
313         }
314         return ret;
315     }
316
317     /**
318      * Returns the numerical value of the last numBits (LSB bits) of the byte array passed.
319      *
320      * @return long - numerical value of byte array passed
321      */
322     public static long toNumber(final byte[] array, final int numBits) {
323         int length = numBits / Byte.SIZE;
324         int bitsRest = numBits % Byte.SIZE;
325         int startOffset = array.length - length;
326         long ret = 0;
327
328         for (int i = Math.max(0, startOffset - 1); i < array.length; i++) {
329             int value = array[i];
330             if (i == startOffset - 1) {
331                 value &= getLSBMask(bitsRest);
332             }
333             if (value < 0) {
334                 value += 256;
335             }
336             ret = ret << Byte.SIZE | value;
337         }
338
339         return ret;
340     }
341
342     /**
343      * Accepts a number as input and returns its value in byte form in LSB
344      * aligned form example: input = 5000 [1001110001000] bytes = 19, -120
345      * [00010011] [10001000].
346      */
347     public static byte[] toByteArray(final Number input) {
348         Class<? extends Number> dataType = input.getClass();
349         short size;
350         long longValue = input.longValue();
351
352         if (dataType == Byte.class || dataType == byte.class) {
353             size = Byte.SIZE;
354         } else if (dataType == Short.class || dataType == short.class) {
355             size = Short.SIZE;
356         } else if (dataType == Integer.class || dataType == int.class) {
357             size = Integer.SIZE;
358         } else if (dataType == Long.class || dataType == long.class) {
359             size = Long.SIZE;
360         } else {
361             throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n");
362         }
363
364         int length = size / NetUtils.NUM_BITS_IN_A_BYTE;
365         byte[] bytes = new byte[length];
366
367         // Getting the bytes from input value
368         for (int i = 0; i < length; i++) {
369             bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK);
370         }
371         return bytes;
372     }
373
374     /**
375      * Accepts a number as input and returns its value in byte form in MSB
376      * aligned form example: input = 5000 [1001110001000] bytes = -114, 64
377      * [10011100] [01000000].
378      *
379      * @param numBits
380      *            - the number of bits to be returned
381      * @return byte[]
382      *
383      */
384     public static byte[] toByteArray(final Number input, final int numBits) {
385         Class<? extends Number> dataType = input.getClass();
386         short size;
387         long longValue = input.longValue();
388
389         if (dataType == Short.class) {
390             size = Short.SIZE;
391         } else if (dataType == Integer.class) {
392             size = Integer.SIZE;
393         } else if (dataType == Long.class) {
394             size = Long.SIZE;
395         } else {
396             throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n");
397         }
398
399         int length = size / NetUtils.NUM_BITS_IN_A_BYTE;
400         byte[] bytes = new byte[length];
401         byte[] inputbytes = new byte[length];
402         byte[] shiftedBytes;
403
404         // Getting the bytes from input value
405         for (int i = 0; i < length; i++) {
406             bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK);
407         }
408
409         if (bytes[0] == 0 && dataType == Long.class || bytes[0] == 0 && dataType == Integer.class) {
410             int index;
411             for (index = 0; index < length; ++index) {
412                 if (bytes[index] != 0) {
413                     bytes[0] = bytes[index];
414                     break;
415                 }
416             }
417             System.arraycopy(bytes, index, inputbytes, 0, length - index);
418             Arrays.fill(bytes, length - index + 1, length - 1, (byte) 0);
419         } else {
420             System.arraycopy(bytes, 0, inputbytes, 0, length);
421         }
422
423         shiftedBytes = shiftBitsToMSB(inputbytes, numBits);
424
425         return shiftedBytes;
426     }
427
428     /**
429      * Takes an LSB aligned byte array and returned the LSB numBits in a MSB
430      * aligned byte array.
431      *
432      * <p>
433      * It aligns the last numBits bits to the head of the byte array following
434      * them with numBits % 8 zero bits.
435      *
436      * <p>
437      * Example: For inputbytes = [00000111][01110001] and numBits = 12 it
438      * returns: shiftedBytes = [01110111][00010000]
439      *
440      * @param numBits
441      *            - number of bits to be left aligned
442      * @return byte[]
443      */
444     public static byte[] shiftBitsToMSB(final byte[] inputBytes, final int numBits) {
445         int numBitstoShiftBy;
446         int leadZeroesMSB = 8;
447         int numEndRestBits;
448         int size = inputBytes.length;
449         byte[] shiftedBytes = new byte[size];
450
451         for (int i = 0; i < Byte.SIZE; i++) {
452             if ((byte) (inputBytes[0] & getMSBMask(i + 1)) != 0) {
453                 leadZeroesMSB = i;
454                 break;
455             }
456         }
457
458         if (numBits % NetUtils.NUM_BITS_IN_A_BYTE == 0) {
459             numBitstoShiftBy = 0;
460         } else {
461             numBitstoShiftBy = NetUtils.NUM_BITS_IN_A_BYTE - numBits % NetUtils.NUM_BITS_IN_A_BYTE < leadZeroesMSB
462                     ? NetUtils.NUM_BITS_IN_A_BYTE - numBits % NetUtils.NUM_BITS_IN_A_BYTE : leadZeroesMSB;
463         }
464         if (numBitstoShiftBy == 0) {
465             return inputBytes;
466         }
467
468         if (numBits < NetUtils.NUM_BITS_IN_A_BYTE) {
469             // inputbytes.length = 1 OR read less than a byte
470             shiftedBytes[0] = (byte) ((inputBytes[0] & getLSBMask(numBits)) << numBitstoShiftBy);
471         } else {
472             // # of bits to read from last byte
473             numEndRestBits = NetUtils.NUM_BITS_IN_A_BYTE
474                     - (inputBytes.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits - numBitstoShiftBy);
475
476             for (int i = 0; i < size - 1; i++) {
477                 if (i + 1 == size - 1) {
478                     if (numEndRestBits > numBitstoShiftBy) {
479                         shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy
480                                 | (inputBytes[i + 1] & getMSBMask(numBitstoShiftBy)) >> numEndRestBits
481                                         - numBitstoShiftBy);
482                         shiftedBytes[i + 1] = (byte) ((inputBytes[i + 1]
483                                 & getLSBMask(numEndRestBits - numBitstoShiftBy)) << numBitstoShiftBy);
484                     } else {
485                         shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy
486                                 | (inputBytes[i + 1] & getMSBMask(numEndRestBits)) >> NetUtils.NUM_BITS_IN_A_BYTE
487                                         - numEndRestBits);
488                     }
489                 }
490                 shiftedBytes[i] = (byte) (inputBytes[i] << numBitstoShiftBy
491                         | (inputBytes[i + 1] & getMSBMask(numBitstoShiftBy)) >> NetUtils.NUM_BITS_IN_A_BYTE
492                                 - numBitstoShiftBy);
493             }
494
495         }
496         return shiftedBytes;
497     }
498
499     /**
500      * It aligns the first numBits bits to the right end of the byte array
501      * preceding them with numBits % 8 zero bits.
502      *
503      * <p>
504      * Example: For inputbytes = [01110111][00010000] and numBits = 12 it
505      * returns: shiftedBytes = [00000111][01110001]
506      *
507      * @param inputBytes input bytes
508      * @param numBits
509      *            - number of bits to be right aligned
510      * @return byte[]
511      */
512     public static byte[] shiftBitsToLSB(final byte[] inputBytes, final int numBits) {
513         int numBytes = inputBytes.length;
514         int numBitstoShift = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
515         byte[] shiftedBytes = new byte[numBytes];
516         int inputLsb;
517         int inputMsb;
518
519         if (numBitstoShift == 0) {
520             return inputBytes;
521         }
522
523         for (int i = 1; i < numBytes; i++) {
524             inputLsb = inputBytes[i - 1] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
525             inputLsb = inputLsb < 0 ? inputLsb + 256 : inputLsb;
526             inputMsb = inputBytes[i] & getMSBMask(numBitstoShift);
527             inputMsb = inputBytes[i] < 0 ? inputBytes[i] + 256 : inputBytes[i];
528             shiftedBytes[i] = (byte) (inputLsb << numBitstoShift
529                     | inputMsb >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
530         }
531         inputMsb = inputBytes[0] & getMSBMask(numBitstoShift);
532         inputMsb = inputMsb < 0 ? inputMsb + 256 : inputMsb;
533         shiftedBytes[0] = (byte) (inputMsb >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift);
534         return shiftedBytes;
535     }
536
537     /**
538      * Insert in the data buffer at position dictated by the offset the number
539      * of bits specified from the input data byte array. The input byte array
540      * has the bits stored starting from the LSB
541      */
542     public static void insertBits(final byte[] data, final byte[] inputdataLSB, final int startOffset,
543             final int numBits) {
544         byte[] inputdata = shiftBitsToMSB(inputdataLSB, numBits); // Align to
545                                                                     // MSB the
546                                                                     // passed
547                                                                     // byte
548                                                                     // array
549         int numBytes = numBits / NetUtils.NUM_BITS_IN_A_BYTE;
550         int startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE;
551         int extraOffsetBits = startOffset % NetUtils.NUM_BITS_IN_A_BYTE;
552         int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
553         int restBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
554         int inputMSBbits;
555         int inputLSBbits = 0;
556
557         if (numBits == 0) {
558             return;
559         }
560
561         if (extraOffsetBits == 0) {
562             if (extranumBits == 0) {
563                 numBytes = numBits / NetUtils.NUM_BITS_IN_A_BYTE;
564                 System.arraycopy(inputdata, 0, data, startByteOffset, numBytes);
565             } else {
566                 System.arraycopy(inputdata, 0, data, startByteOffset, numBytes);
567                 data[startByteOffset + numBytes] = (byte) (data[startByteOffset + numBytes]
568                         | inputdata[numBytes] & getMSBMask(extranumBits));
569             }
570         } else {
571             int index;
572             for (index = 0; index < numBytes; index++) {
573                 if (index != 0) {
574                     inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
575                 }
576                 inputMSBbits = (byte) (inputdata[index] & getMSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
577                 inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
578                 data[startByteOffset + index] = (byte) (data[startByteOffset + index]
579                         | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
580                         | inputMSBbits >> extraOffsetBits);
581                 inputMSBbits = inputLSBbits = 0;
582             }
583             if (restBits < NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
584                 if (numBytes != 0) {
585                     inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
586                 }
587                 inputMSBbits = (byte) (inputdata[index] & getMSBMask(restBits));
588                 inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
589                 data[startByteOffset + index] = (byte) (data[startByteOffset + index]
590                         | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
591                         | inputMSBbits >> extraOffsetBits);
592             } else if (restBits == NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits) {
593                 if (numBytes != 0) {
594                     inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
595                 }
596                 inputMSBbits = (byte) (inputdata[index] & getMSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
597                 inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
598                 data[startByteOffset + index] = (byte) (data[startByteOffset + index]
599                         | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
600                         | inputMSBbits >> extraOffsetBits);
601             } else {
602                 if (numBytes != 0) {
603                     inputLSBbits = inputdata[index - 1] & getLSBMask(extraOffsetBits);
604                 }
605                 inputMSBbits = (byte) (inputdata[index] & getMSBMask(NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits));
606                 inputMSBbits = inputMSBbits >= 0 ? inputMSBbits : inputMSBbits + 256;
607                 data[startByteOffset + index] = (byte) (data[startByteOffset + index]
608                         | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits
609                         | inputMSBbits >> extraOffsetBits);
610
611                 inputLSBbits = inputdata[index]
612                         & getLSBMask(restBits - (NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits))
613                         << NetUtils.NUM_BITS_IN_A_BYTE
614                                 - restBits;
615                 data[startByteOffset + index + 1] = (byte) (data[startByteOffset + index + 1]
616                         | inputLSBbits << NetUtils.NUM_BITS_IN_A_BYTE - extraOffsetBits);
617             }
618         }
619     }
620
621     /**
622      * Checks for overflow and underflow exceptions.
623      *
624      * @throws BufferException
625      *             when the startOffset and numBits parameters are not congruent
626      *             with the data buffer's size
627      */
628     public static void checkExceptions(final byte[] data, final int startOffset, final int numBits)
629             throws BufferException {
630         int endOffsetByte;
631         int startByteOffset;
632         endOffsetByte = startOffset / NetUtils.NUM_BITS_IN_A_BYTE + numBits / NetUtils.NUM_BITS_IN_A_BYTE
633                 + (numBits % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1
634                         : startOffset % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1 : 0);
635         startByteOffset = startOffset / NetUtils.NUM_BITS_IN_A_BYTE;
636
637         if (data == null) {
638             throw new BufferException("data[] is null\n");
639         }
640
641         if (startOffset < 0 || startByteOffset >= data.length || endOffsetByte > data.length || numBits < 0
642                 || numBits > NetUtils.NUM_BITS_IN_A_BYTE * data.length) {
643             throw new BufferException("Illegal arguement/out of bound exception - data.length = " + data.length
644                     + " startOffset = " + startOffset + " numBits " + numBits);
645         }
646     }
647 }