public boolean isValidMultiBytes( final byte[] b, final int from, final int limit, final String encoding) { // FIXME: magic number if (encoding.equalsIgnoreCase("Big5")) { return Convertor.isValidBig5(b, from, limit); } else if (encoding.equalsIgnoreCase("UTF-8")) { return Convertor.isValidUTF8(b, from, limit); } else { // TODO: 其他的編碼 System.out.println("Unknown Encoding: " + encoding); return true; } }
private Convertor() { int i1, i2; this.ucs2bytes = new byte[64 * 1024]; this.big5bytes = new byte[128 * 1024]; Convertor.readFile("conv/ucs2.txt", this.ucs2bytes); Convertor.readFile("conv/big5.txt", this.big5bytes); this.ucs2chars = new char[this.ucs2bytes.length / 2]; // 把讀進來的 ucs2 bytes 處理成標準的 (ucs2) char for (int i = 0; i < this.ucs2bytes.length; i += 2) { i1 = (this.ucs2bytes[i] < 0 ? 256 : 0) + this.ucs2bytes[i]; i2 = (this.ucs2bytes[i + 1] < 0 ? 256 : 0) + this.ucs2bytes[i + 1]; this.ucs2chars[i / 2] = (char) (i1 << 8 | i2); } }
/** * Check if a message contains any wide characters. * * @param message the message to be checked * @return true if the message contains wide characters; false, otherwise. */ public static boolean containsWideChar(String message) { for (int i = 0; i < message.length(); i++) { final char c = message.charAt(i); if (Convertor.isWideChar(c)) { return true; } } return false; }