/* * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
privatestatic String detectingCharset(byte[] bytes) throws Exception { //---------------------------------------------------------------- // Test special public methods of CharsetDecoder while we're here //----------------------------------------------------------------
CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
check(cd.isAutoDetecting(), "isAutodecting()");
check(! cd.isCharsetDetected(), "isCharsetDetected");
cd.decode(ByteBuffer.wrap(newbyte[] {(byte)'A'}));
check(! cd.isCharsetDetected(), "isCharsetDetected"); try {
cd.detectedCharset();
fail("no IllegalStateException");
} catch (IllegalStateException e) {}
cd.decode(ByteBuffer.wrap(bytes));
check(cd.isCharsetDetected(), "isCharsetDetected");
Charset cs = cd.detectedCharset();
check(cs != null, "cs != null");
check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()"); return cs.name();
}
publicstaticvoid main(String[] argv) throws Exception { //---------------------------------------------------------------- // Used to throw BufferOverflowException //----------------------------------------------------------------
out.println(new String(newbyte[] {0x61}, "JISAutoDetect"));
//---------------------------------------------------------------- // InputStreamReader(...JISAutoDetect) used to infloop //----------------------------------------------------------------
{ byte[] bytes = "ABCD\n".getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InputStreamReader isr = new InputStreamReader(bais, "JISAutoDetect");
BufferedReader reader = new BufferedReader(isr);
check (reader.readLine().equals("ABCD"), "first read gets text"); // used to return "ABCD" on second and subsequent reads
check (reader.readLine() == null, "second read gets null");
}
//---------------------------------------------------------------- // Check all Japanese chars for sanity //----------------------------------------------------------------
String SJIS = SJISName();
String EUCJ = EUCJName();
out.printf("SJIS charset is %s%n", SJIS);
out.printf("EUCJ charset is %s%n", EUCJ);
int cnt2022 = 0; int cnteucj = 0; int cntsjis = 0; int cntBAD = 0; for (char c = '\u0000'; c < '\uffff'; c++) { if (c == '\u001b' || // ESC
c == '\u2014') // Em-Dash? continue;
String s = new String (newchar[] {c});
//---------------------------------------------------------------- // JISAutoDetect can handle all chars that EUC-JP can, // unless there is an ambiguity with SJIS. //---------------------------------------------------------------- byte[] beucj = s.getBytes(EUCJ);
String seucj = new String(beucj, EUCJ); if (seucj.equals(s)) {
cnteucj++;
String sauto = new String(beucj, "JISAutoDetect");
if (! sauto.equals(seucj)) {
cntBAD++;
String ssjis = new String(beucj, SJIS); if (! sauto.equals(ssjis)) {
fail("Autodetection agrees with neither EUC nor SJIS");
}
}
} else continue; // Optimization
//---------------------------------------------------------------- // JISAutoDetect can handle all chars that ISO-2022-JP can. //---------------------------------------------------------------- byte[] b2022 = s.getBytes("ISO-2022-JP"); if (new String(b2022, "ISO-2022-JP").equals(s)) {
cnt2022++;
check(new String(b2022,"JISAutoDetect").equals(s), "ISO2022 autodetection");
}
//---------------------------------------------------------------- // JISAutoDetect can handle almost all chars that SJIS can. //---------------------------------------------------------------- byte[] bsjis = s.getBytes(SJIS); if (new String(bsjis, SJIS).equals(s)) {
cntsjis++;
check(new String(bsjis,"JISAutoDetect").equals(s), "SJIS autodetection");
}
}
out.printf("There are %d ISO-2022-JP-encodable characters.%n", cnt2022);
out.printf("There are %d SJIS-encodable characters.%n", cntsjis);
out.printf("There are %d EUC-JP-encodable characters.%n", cnteucj);
out.printf("There are %d characters that are " + "misdetected as SJIS after being EUC-encoded.%n", cntBAD);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.