### Question:
ByteUtils { public static String readString(ByteArrayInputStream inputStream) { if (inputStream == null) throw new NullPointerException("Input stream cannot be null."); StringBuilder sb = new StringBuilder(); byte readByte; while (((readByte = (byte)inputStream.read()) != 0x00) && readByte != -1) sb.append((char)readByte); return sb.toString(); }  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 testReadStringNullInputStream() { ByteArrayInputStream in = null; exception.expect(NullPointerException.class); exception.expectMessage(is(equalTo("Input stream cannot be null."))); ByteUtils.readString(in); }
@Test public final void testReadStringEmptyInputStream() { byte[] contents = new byte[0]; ByteArrayInputStream in = new ByteArrayInputStream(contents); String result = ByteUtils.readString(in); assertThat("Returned string must be empty", result.length(), is(equalTo(0))); }
@Test public final void testReadStringNotNullTerminated() { byte[] contents = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05}; ByteArrayInputStream in = new ByteArrayInputStream(contents); String result = ByteUtils.readString(in); assertThat("Returned string lenght must be " + contents.length, result.length(), is(equalTo(contents.length))); assertThat("Returned string must be equal to 'contents'", result, is(equalTo(new String(contents)))); }
@Test public final void testReadStringNullTerminated() { byte[] contents = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x00}; byte[] expectedResult = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05}; ByteArrayInputStream in = new ByteArrayInputStream(contents); String result = ByteUtils.readString(in); assertThat("Returned string lenght must be " + contents.length, result.length(), is(equalTo(expectedResult.length))); assertThat("Returned string must be equal to 'expectedResult'", result, is(equalTo(new String(expectedResult)))); }
@Test public final void testReadStringNullTerminatedAndMore() { byte[] contents = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08}; byte[] expectedResult = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05}; ByteArrayInputStream in = new ByteArrayInputStream(contents); String result = ByteUtils.readString(in); assertThat("Returned string lenght must be " + contents.length, result.length(), is(equalTo(expectedResult.length))); assertThat("Returned string must be equal to 'expectedResult'", result, is(equalTo(new String(expectedResult)))); }