📄 stringconverter.java
字号:
* * @return Java Unicode string */ public static String asciiToUnicode(byte[] s, int offset, int length) { if (length == 0) { return ""; } char[] b = new char[length]; int j = 0; for (int i = 0; i < length; i++) { byte c = s[offset + i]; if (c == '\\' && i < length - 5) { byte c1 = s[offset + i + 1]; if (c1 == 'u') { i++; // 4 characters read should always return 0-15 int k = HEXINDEX.indexOf(s[offset + (++i)]) << 12; k += HEXINDEX.indexOf(s[offset + (++i)]) << 8; k += HEXINDEX.indexOf(s[offset + (++i)]) << 4; k += HEXINDEX.indexOf(s[offset + (++i)]); b[j++] = (char) k; } else { b[j++] = (char) c; } } else { b[j++] = (char) c; } } return new String(b, 0, j); } public static String asciiToUnicode(String s) { if ((s == null) || (s.indexOf("\\u") == -1)) { return s; } int len = s.length(); char[] b = new char[len]; int j = 0; for (int i = 0; i < len; i++) { char c = s.charAt(i); if (c == '\\' && i < len - 5) { char c1 = s.charAt(i + 1); if (c1 == 'u') { i++; // 4 characters read should always return 0-15 int k = HEXINDEX.indexOf(s.charAt(++i)) << 12; k += HEXINDEX.indexOf(s.charAt(++i)) << 8; k += HEXINDEX.indexOf(s.charAt(++i)) << 4; k += HEXINDEX.indexOf(s.charAt(++i)); b[j++] = (char) k; } else { b[j++] = c; } } else { b[j++] = c; } } return new String(b, 0, j); } public static String readUTF(byte[] bytearr, int offset, int length) throws IOException { char[] buf = new char[length * 2]; return readUTF(bytearr, offset, length, buf); } public static String readUTF(byte[] bytearr, int offset, int length, char[] buf) throws IOException { int bcount = 0; int c, char2, char3; int count = 0; while (count < length) { c = (int) bytearr[offset + count]; if (bcount > buf.length - 4) { buf = (char[]) ArrayUtil.resizeArray(buf, buf.length + length); } if (c > 0) { /* 0xxxxxxx*/ count++; buf[bcount++] = (char) c; continue; } c &= 0xff; switch (c >> 4) { case 12 : case 13 : /* 110x xxxx 10xx xxxx*/ count += 2; if (count > length) { throw new UTFDataFormatException(); } char2 = (int) bytearr[offset + count - 1]; if ((char2 & 0xC0) != 0x80) { throw new UTFDataFormatException(); } buf[bcount++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F)); break; case 14 : /* 1110 xxxx 10xx xxxx 10xx xxxx */ count += 3; if (count > length) { throw new UTFDataFormatException(); } char2 = (int) bytearr[offset + count - 2]; char3 = (int) bytearr[offset + count - 1]; if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) { throw new UTFDataFormatException(); } buf[bcount++] = (char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); break; default : /* 10xx xxxx, 1111 xxxx */ throw new UTFDataFormatException(); } } // The number of chars produced may be less than length return new String(buf, 0, bcount); } /** * Writes a string to the specified DataOutput using UTF-8 encoding in a * machine-independent manner. * <p> * @param str a string to be written. * @param out destination to write to * @return The number of bytes written out. */ public static int writeUTF(String str, HsqlByteArrayOutputStream out) { int strlen = str.length(); int c, count = 0; for (int i = 0; i < strlen; i++) { c = str.charAt(i); if (c >= 0x0001 && c <= 0x007F) { out.write(c); count++; } else if (c > 0x07FF) { out.write(0xE0 | ((c >> 12) & 0x0F)); out.write(0x80 | ((c >> 6) & 0x3F)); out.write(0x80 | ((c >> 0) & 0x3F)); count += 3; } else { out.write(0xC0 | ((c >> 6) & 0x1F)); out.write(0x80 | ((c >> 0) & 0x3F)); count += 2; } } return count; } public static int getUTFSize(String s) { int len = (s == null) ? 0 : s.length(); int l = 0; for (int i = 0; i < len; i++) { int c = s.charAt(i); if ((c >= 0x0001) && (c <= 0x007F)) { l++; } else if (c > 0x07FF) { l += 3; } else { l += 2; } } return l; } /** * Using a Reader and a Writer, returns a String from an InputStream. */ public static String inputStreamToString(InputStream x, int length) throws IOException { InputStreamReader in = new InputStreamReader(x); StringWriter writer = new StringWriter(); int blocksize = 8 * 1024; char[] buffer = new char[blocksize]; for (int left = length; left > 0; ) { int read = in.read(buffer, 0, left > blocksize ? blocksize : left); if (read == -1) { break; } writer.write(buffer, 0, read); left -= read; } writer.close(); return writer.toString(); }// fredt@users 20020130 - patch 497872 by Nitin Chauhan - use byte[] of exact size /** * Returns the quoted version of the string using the quotechar argument. * doublequote argument indicates whether each instance of quotechar * inside the string is doubled.<p> * * null string argument returns null. If the caller needs the literal * "NULL" it should created it itself <p> * * The reverse conversion is handled in Tokenizer.java */ public static String toQuotedString(String s, char quoteChar, boolean extraQuote) { if (s == null) { return null; } int count = extraQuote ? count(s, quoteChar) : 0; int len = s.length(); char[] b = new char[2 + count + len]; int i = 0; int j = 0; b[j++] = quoteChar; for (; i < len; i++) { char c = s.charAt(i); b[j++] = c; if (extraQuote && c == quoteChar) { b[j++] = c; } } b[j] = quoteChar; return new String(b); } /** * Counts Character c in String s * * @param String s * * @return int count */ static int count(final String s, final char c) { int pos = 0; int count = 0; if (s != null) { while ((pos = s.indexOf(c, pos)) > -1) { count++; pos++; } } return count; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -