@Test(expected = EOFException.class) public void readInvalidBytes() throws IOException { byte[] given = new byte[]{0x01, 0x00}; InputStream givenInputStream = new ByteArrayInputStream(given); STR.read(givenInputStream); }
public static String read(InputStream is) throws IOException { Validators.notNull("is", is); int numChars = is.read(); if (numChars == -1) { throw new EOFException("length of InputStream is 0."); } if (numChars == 0) { return ""; } int numBytes = numChars * UINT16.SIZE_IN_BYTES; byte[] bytes = new byte[numBytes]; int numReadBytes = is.read(bytes); if (numReadBytes != numBytes) { String message = String.format(Locale.US, "NumChars is %d (= %d bytes), but actual data is %d bytes.", numChars, numBytes, numReadBytes); throw new EOFException(message); } return new String(bytes, 0, numBytes - NULL_TERMINATOR.length, CHARSET); }
STR { public static String read(InputStream is) throws IOException { Validators.notNull("is", is); int numChars = is.read(); if (numChars == -1) { throw new EOFException("length of InputStream is 0."); } if (numChars == 0) { return ""; } int numBytes = numChars * UINT16.SIZE_IN_BYTES; byte[] bytes = new byte[numBytes]; int numReadBytes = is.read(bytes); if (numReadBytes != numBytes) { String message = String.format(Locale.US, "NumChars is %d (= %d bytes), but actual data is %d bytes.", numChars, numBytes, numReadBytes); throw new EOFException(message); } return new String(bytes, 0, numBytes - NULL_TERMINATOR.length, CHARSET); } }
STR { public static String read(InputStream is) throws IOException { Validators.notNull("is", is); int numChars = is.read(); if (numChars == -1) { throw new EOFException("length of InputStream is 0."); } if (numChars == 0) { return ""; } int numBytes = numChars * UINT16.SIZE_IN_BYTES; byte[] bytes = new byte[numBytes]; int numReadBytes = is.read(bytes); if (numReadBytes != numBytes) { String message = String.format(Locale.US, "NumChars is %d (= %d bytes), but actual data is %d bytes.", numChars, numBytes, numReadBytes); throw new EOFException(message); } return new String(bytes, 0, numBytes - NULL_TERMINATOR.length, CHARSET); } private  STR(); }
STR { public static String read(InputStream is) throws IOException { Validators.notNull("is", is); int numChars = is.read(); if (numChars == -1) { throw new EOFException("length of InputStream is 0."); } if (numChars == 0) { return ""; } int numBytes = numChars * UINT16.SIZE_IN_BYTES; byte[] bytes = new byte[numBytes]; int numReadBytes = is.read(bytes); if (numReadBytes != numBytes) { String message = String.format(Locale.US, "NumChars is %d (= %d bytes), but actual data is %d bytes.", numChars, numBytes, numReadBytes); throw new EOFException(message); } return new String(bytes, 0, numBytes - NULL_TERMINATOR.length, CHARSET); } private  STR(); static byte[] toBytes(String str); static String read(InputStream is); }
STR { public static String read(InputStream is) throws IOException { Validators.notNull("is", is); int numChars = is.read(); if (numChars == -1) { throw new EOFException("length of InputStream is 0."); } if (numChars == 0) { return ""; } int numBytes = numChars * UINT16.SIZE_IN_BYTES; byte[] bytes = new byte[numBytes]; int numReadBytes = is.read(bytes); if (numReadBytes != numBytes) { String message = String.format(Locale.US, "NumChars is %d (= %d bytes), but actual data is %d bytes.", numChars, numBytes, numReadBytes); throw new EOFException(message); } return new String(bytes, 0, numBytes - NULL_TERMINATOR.length, CHARSET); } private  STR(); static byte[] toBytes(String str); static String read(InputStream is); static final int MIN_SIZE_IN_BYTES; }