@@ -38,10 +38,12 @@ public static byte[] readBytes(int numBytes, ByteArrayInputStream inputStream) {
3838 if (inputStream == null )
3939 throw new NullPointerException ("Input stream cannot be null." );
4040 if (numBytes < 0 )
41- throw new IllegalArgumentException ("Number of bytes to read must be greater than 0." );
41+ throw new IllegalArgumentException ("Number of bytes to read must be equal or greater than 0." );
4242
4343 byte [] data = new byte [numBytes ];
4444 int len = inputStream .read (data , 0 , numBytes );
45+ if (len == - 1 )
46+ return new byte [0 ];
4547 if (len < numBytes ) {
4648 byte [] d = new byte [len ];
4749 System .arraycopy (data , 0 , d , 0 , len );
@@ -110,13 +112,16 @@ public static long byteArrayToLong(byte[] byteArray) {
110112 if (byteArray == null )
111113 throw new NullPointerException ("Byte array cannot be null." );
112114
115+ if (byteArray .length == 0 )
116+ return 0 ;
117+
113118 byte [] values = byteArray ;
114119 if (byteArray .length < 8 ) {
115120 values = new byte [8 ];
116- int diff = 8 - byteArray .length ;
121+ int diff = values . length - byteArray .length ;
117122 for (int i = 0 ; i < diff ; i ++)
118123 values [i ] = 0 ;
119- for (int i = diff ; i < 8 ; i ++)
124+ for (int i = diff ; i < values . length ; i ++)
120125 values [i ] = byteArray [i - diff ];
121126 }
122127 return ((long )values [0 ] << 56 )
@@ -162,13 +167,16 @@ public static int byteArrayToInt(byte[] byteArray) {
162167 if (byteArray == null )
163168 throw new NullPointerException ("Byte array cannot be null." );
164169
170+ if (byteArray .length == 0 )
171+ return 0 ;
172+
165173 byte [] values = byteArray ;
166174 if (byteArray .length < 4 ) {
167175 values = new byte [4 ];
168- int diff = 4 - byteArray .length ;
176+ int diff = values . length - byteArray .length ;
169177 for (int i = 0 ; i < diff ; i ++)
170178 values [i ] = 0 ;
171- for (int i = diff ; i < 4 ; i ++)
179+ for (int i = diff ; i < values . length ; i ++)
172180 values [i ] = byteArray [i - diff ];
173181 }
174182 return ((values [0 ] & 0xFF ) << 24 )
@@ -208,8 +216,18 @@ public static short byteArrayToShort(byte[] byteArray) {
208216 if (byteArray == null )
209217 throw new NullPointerException ("Byte array cannot be null." );
210218
211- return (short ) (((byteArray [0 ] << 8 ) & 0xFF00 )
212- | byteArray [1 ] & 0x00FF );
219+ if (byteArray .length == 0 )
220+ return 0 ;
221+
222+ byte [] values = byteArray ;
223+ if (byteArray .length < 2 ) {
224+ values = new byte [2 ];
225+ values [1 ] = byteArray [0 ];
226+ values [0 ] = 0 ;
227+ }
228+
229+ return (short ) (((values [0 ] << 8 ) & 0xFF00 )
230+ | values [1 ] & 0x00FF );
213231 }
214232
215233 /**
@@ -267,8 +285,14 @@ public static int byteToInt(byte b) {
267285 *
268286 * @return {@code true} if the given bit position is set to {@code 1}
269287 * in the {@code containerInteger}, {@code false} otherwise.
288+ *
289+ * @throws IllegalArgumentException if {@code bitPosition < 0} or
290+ * if {@code bitPosition > 31}.
270291 */
271292 public static boolean isBitEnabled (int containerInteger , int bitPosition ) {
293+ if (bitPosition < 0 || bitPosition > 31 )
294+ throw new IllegalArgumentException ("Bit position must be between 0 and 31." );
295+
272296 return (((containerInteger & 0xFFFFFFFF ) >> bitPosition ) & 0x01 ) == 0x01 ;
273297 }
274298
@@ -281,10 +305,22 @@ public static boolean isBitEnabled(int containerInteger, int bitPosition) {
281305 * @param bitLength Size in bits of the integer value to read.
282306 *
283307 * @return The integer read value.
308+ *
309+ * @throws IllegalArgumentException if {@code bitOffset < 0} or
310+ * if {@code bitOffset > 7} or
311+ * if {@code bitLength < 0} or
312+ * if {@code bitLength > 8}.
284313 */
285314 public static int readIntegerFromByte (byte containerByte , int bitOffset , int bitLength ) {
315+ if (bitOffset < 0 || bitOffset > 7 )
316+ throw new IllegalArgumentException ("Offset must be between 0 and 7." );
317+ if (bitLength < 0 || bitLength > 7 )
318+ throw new IllegalArgumentException ("Length must be between 0 and 8." );
319+
286320 int readInteger = 0 ;
287321 for (int i = 0 ; i < bitLength ; i ++) {
322+ if (bitOffset + i > 7 )
323+ break ;
288324 if (isBitEnabled (containerByte , bitOffset + i ))
289325 readInteger = readInteger | (int )Math .pow (2 , i );
290326 }
@@ -298,8 +334,14 @@ public static int readIntegerFromByte(byte containerByte, int bitOffset, int bit
298334 * @param bitOffset Offset inside the byte to read the boolean value.
299335 *
300336 * @return The read boolean value.
337+ *
338+ * @throws IllegalArgumentException if {@code bitOffset < 0} or
339+ * if {@code bitOffset > 31}.
301340 */
302341 public static boolean readBooleanFromByte (byte containerByte , int bitOffset ) {
342+ if (bitOffset < 0 || bitOffset > 31 )
343+ throw new IllegalArgumentException ("Bit offset must be between 0 and 7." );
344+
303345 return isBitEnabled (containerByte , bitOffset );
304346 }
305347
@@ -339,11 +381,17 @@ public static byte[] readUntilCR(ByteArrayInputStream inputStream) {
339381 * @return Final byte array of the given size containing the given data and
340382 * replacing with zeros the remaining space.
341383 *
384+ * @throws IllegalArgumentException if {@code finalSize < 0}.
342385 * @throws NullPointerException if {@code data == null}.
343386 */
344387 public static byte [] newByteArray (byte [] data , int finalSize ) {
345388 if (data == null )
346389 throw new NullPointerException ("Data cannot be null." );
390+ if (finalSize < 0 )
391+ throw new IllegalArgumentException ("Final size must be equal or greater than 0." );
392+
393+ if (finalSize == 0 )
394+ return new byte [0 ];
347395
348396 byte [] filledArray = new byte [finalSize ];
349397 int diff = finalSize - data .length ;
@@ -362,8 +410,16 @@ public static byte[] newByteArray(byte[] data, int finalSize) {
362410 * @param source Byte array to swap.
363411 *
364412 * @return The swapped byte array.
413+ *
414+ * @throws NullPointerException if {@code source == null}.
365415 */
366416 public static byte [] swapByteArray (byte [] source ) {
417+ if (source == null )
418+ throw new NullPointerException ("Source cannot be null." );
419+
420+ if (source .length == 0 )
421+ return new byte [0 ];
422+
367423 byte [] swapped = new byte [source .length ];
368424 for (int i = 0 ; i < source .length ; i ++)
369425 swapped [source .length - i - 1 ] = source [i ];
0 commit comments