### Question:
ByteUtils { public static byte[] readBytes(int numBytes, ByteArrayInputStream inputStream) { if (inputStream == null) throw new NullPointerException("Input stream cannot be null."); if (numBytes < 0) throw new IllegalArgumentException("Number of bytes to read must be equal or greater than 0."); byte[] data = new byte[numBytes]; int len = inputStream.read(data, 0, numBytes); if (len == - 1) return new byte[0]; if (len < numBytes) { byte[] d = new byte[len]; System.arraycopy(data, 0, d, 0, len); return d; } return data; }  static byte[] readBytes(int numBytes, ByteArrayInputStream inputStream); static String readString(ByteArrayInputStream inputStream); static byte[] longToByteArray(long value); static long byteArrayToLong(byte[] byteArray); static byte[] intToByteArray(int value); static int byteArrayToInt(byte[] byteArray); static byte[] shortToByteArray(short value); static short byteArrayToShort(byte[] byteArray); static byte[] stringToByteArray(String value); static String byteArrayToString(byte[] value); static int byteToInt(byte b); static boolean isBitEnabled(int containerInteger, int bitPosition); static int readIntegerFromByte(byte containerByte, int bitOffset, int bitLength); static boolean readBooleanFromByte(byte containerByte, int bitOffset); static byte[] readUntilCR(ByteArrayInputStream inputStream); static byte[] newByteArray(byte[] data, int finalSize); static byte[] swapByteArray(byte[] source);  }### Answer:
@Test public final void testReadBytesNullInputStream() { ByteArrayInputStream in = null; exception.expect(NullPointerException.class); exception.expectMessage(is(equalTo("Input stream cannot be null."))); ByteUtils.readBytes(2, in); }
@Test public final void testReadBytesNegativeNumberOfBytes() { ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]); exception.expect(IllegalArgumentException.class); exception.expectMessage(is(equalTo("Number of bytes to read must be equal or greater than 0."))); ByteUtils.readBytes(-5, in); }
@Test public final void testReadBytesEmptyInputStream() { byte[] contents = new byte[0]; ByteArrayInputStream in = new ByteArrayInputStream(contents); byte[] result = ByteUtils.readBytes(2, in); assertThat("Returned byte array must be empty", result.length, is(equalTo(0))); }
@Test public final void testReadBytesMoreBytesThanAvailable() { byte[] contents = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04}; ByteArrayInputStream in = new ByteArrayInputStream(contents); byte[] result = ByteUtils.readBytes(contents.length + 1, in); assertThat("Returned byte array lenght must be " + contents.length, result.length, is(equalTo(contents.length))); assertThat("Returned byte array must be equal to 'contents'", result, is(equalTo(contents))); }
@Test public final void testReadBytesExactTheAvailable() { byte[] contents = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04}; ByteArrayInputStream in = new ByteArrayInputStream(contents); byte[] result = ByteUtils.readBytes(contents.length, in); assertThat("Returned byte array lenght must be " + contents.length, result.length, is(equalTo(contents.length))); assertThat("Returned byte array must be equal to 'contents'", result, is(equalTo(contents))); }
@Test public final void testReadBytesLessThanAvailable() { byte[] contents = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04}; byte[] expectedResult = new byte[]{0x00, 0x01, 0x02, 0x03}; ByteArrayInputStream in = new ByteArrayInputStream(contents); byte[] result = ByteUtils.readBytes(contents.length - 1, in); assertThat("Returned byte array lenght must be " + (contents.length - 1), result.length, is(equalTo(contents.length - 1))); assertThat("Returned byte array must be equal to 'expectedResult'", result, is(equalTo(expectedResult))); }