/** read string from input stream. if get EOF before read enough data, throw IOException. */ public static String readString(TellableInputStream in, StringEncoding encoding) throws IOException { if (encoding == StringEncoding.UTF8) { // The lengths are encoded in the same way as for the 16-bit format // but using 8-bit rather than 16-bit integers. int strLen = readLen(in); int bytesLen = readLen(in); byte[] bytes = in.readBytes(bytesLen); String str = new String(bytes, "UTF-8"); // zero int trailling = in.readUByte(); return str; } else { // The length is encoded as either one or two 16-bit integers as per the commentRef... int strLen = readLen16(in); String str = in.readStringUTF16(strLen); // zero int trailling = in.readUShort(); return str; } }