/** * read utf-16 encoding str, use zero char to end str. * * @param in * @param strLen * @return * @throws IOException */ public static String readStringUTF16(TellableInputStream in, int strLen) throws IOException { String str = in.readStringUTF16(strLen); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c == 0) { return str.substring(0, i); } } return str; }
/** 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; } }