### Question:
ByteUtils { public static byte[] readUntilCR(ByteArrayInputStream inputStream) { if (inputStream == null) throw new NullPointerException("Input stream cannot be null."); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte readByte; while (((readByte = (byte)inputStream.read()) != 0x0D) && readByte != -1) outputStream.write(readByte); return outputStream.toByteArray(); }  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 testReadUntilCRNullInputStream() { exception.expect(NullPointerException.class); exception.expectMessage(is(equalTo("Input stream cannot be null."))); ByteUtils.readUntilCR(null); }
@Test public final void testReadUntilCREmptyInputStream() { ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]); byte[] result = ByteUtils.readUntilCR(in); assertThat("Read byte array must be empty", result.length, is(equalTo(0))); }
@Test public final void testReadUntilCRNoCR() { byte[] contents = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08}; ByteArrayInputStream in = new ByteArrayInputStream(contents); byte[] result = ByteUtils.readUntilCR(in); assertThat("Read byte array must be equal to 'contents'", result, is(equalTo(contents))); assertThat("Remaining bytes to read must be 0", in.available(), is(equalTo(0))); }
@Test public final void testReadUntilCR() { byte[] contents = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x0D, (byte)0x89, 0x25, 0x01}; byte[] expectedResult = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08}; ByteArrayInputStream in = new ByteArrayInputStream(contents); byte[] result = ByteUtils.readUntilCR(in); assertThat("Read byte array must be equal to 'expectedResult'", result, is(equalTo(expectedResult))); assertThat("Remaining bytes to read must be " + (contents.length - expectedResult.length - 1), in.available(), is(equalTo(contents.length - expectedResult.length - 1))); }