ByteArrayDataInput extends InputStream implements DataInput, VersionedDataStream { @Override public String readUTF() throws IOException { final int utfLen = readUnsignedShort(); if (utfLen == 0) { return ""; } if ((pos + utfLen) <= nBytes) { String asciiString = readASCII(utfLen); if (asciiString != null) { return asciiString; } if (charBuf == null || charBuf.length < utfLen) { int charBufLength = (((utfLen / 2) + 1) * 3); charBuf = new char[charBufLength]; } final byte[] bytes = this.bytes; final char[] chars = charBuf; int index = pos; final int limit = index + utfLen; int nChars = 0; int char1, char2, char3; for (; index < limit; index++, nChars++) { char1 = (bytes[index] & 0xff); switch (char1 >> 5) { case 6: if ((index + 1) < limit) { char2 = bytes[++index]; if ((char2 & 0xc0) == 0x80) { chars[nChars] = (char) ((char1 & 0x1f) << 6 | (char2 & 0x3f)); } else { throwUTFEncodingError(index, char1, char2, null, 2); } } else { throw new UTFDataFormatException( "partial 2-byte character at end (char1=" + char1 + ')'); } break; case 7: if ((index + 2) < limit) { char2 = bytes[++index]; if ((char2 & 0xc0) == 0x80) { char3 = bytes[++index]; if ((char3 & 0xc0) == 0x80) { chars[nChars] = (char) (((char1 & 0x0f) << 12) | ((char2 & 0x3f) << 6) | (char3 & 0x3f)); } else { throwUTFEncodingError(index, char1, char2, char3, 3); } } else { throwUTFEncodingError(index, char1, char2, null, 3); } } else { throw new UTFDataFormatException( "partial 3-byte character at end (char1=" + char1 + ')'); } break; default: chars[nChars] = (char) char1; break; } } pos = limit; return new String(chars, 0, nChars); } else { throw new EOFException(); } } ByteArrayDataInput();  ByteArrayDataInput(byte[] bytes);  ByteArrayDataInput(byte[] bytes, KnownVersion version); void initialize(byte[] bytes, KnownVersion version); @Override KnownVersion getVersion(); @Override int read(); @Override int read(byte[] b, int off, int len); @Override long skip(long n); @Override int available(); int position(); void setPosition(int pos); @Override void readFully(byte[] b); @Override void readFully(byte[] b, int off, int len); @Override int skipBytes(int n); @Override boolean readBoolean(); @Override byte readByte(); @Override int readUnsignedByte(); @Override short readShort(); @Override int readUnsignedShort(); @Override char readChar(); @Override int readInt(); @Override long readLong(); @Override float readFloat(); @Override double readDouble(); @Override String readUTF(); @Override String readLine(); @Override void close(); @Override String toString();  }
@Test public void readUTFOnInputWithJustLengthThrowsEOF() { BufferDataOutputStream output = new BufferDataOutputStream(KnownVersion.CURRENT); output.writeShort(1); DataInput input = createDataInput(output.toByteArray()); Throwable t = catchThrowable(() -> input.readUTF()); assertThat(t).isInstanceOf(EOFException.class); }