NIOJISAutoDetectTest.java revision 2362
98N/A/*
98N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
910N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
753N/A *
98N/A * This code is free software; you can redistribute it and/or modify it
98N/A * under the terms of the GNU General Public License version 2 only, as
98N/A * published by the Free Software Foundation.
98N/A *
98N/A * This code is distributed in the hope that it will be useful, but WITHOUT
98N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
98N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
98N/A * version 2 for more details (a copy is included in the LICENSE file that
98N/A * accompanied this code).
98N/A *
98N/A * You should have received a copy of the GNU General Public License version
98N/A * 2 along with this work; if not, write to the Free Software Foundation,
98N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
98N/A *
98N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
98N/A * or visit www.oracle.com if you need additional information or have any
98N/A * questions.
98N/A */
98N/A
98N/A/*
98N/A * @test
98N/A * @bug 4831163 5053096 5056440
98N/A * @summary NIO charset basic verification of JISAutodetect decoder
98N/A * @author Martin Buchholz
98N/A */
98N/A
98N/Aimport java.io.*;
493N/Aimport java.nio.ByteBuffer;
493N/Aimport java.nio.CharBuffer;
98N/Aimport java.nio.charset.Charset;
98N/Aimport java.nio.charset.CharsetDecoder;
753N/Aimport java.nio.charset.CoderResult;
493N/Aimport static java.lang.System.*;
493N/A
493N/Apublic class NIOJISAutoDetectTest {
493N/A private static int failures = 0;
493N/A
98N/A private static void fail(String failureMsg) {
98N/A System.out.println(failureMsg);
493N/A failures++;
493N/A }
98N/A
98N/A private static void check(boolean cond, String msg) {
156N/A if (!cond) {
156N/A fail("test failed: " + msg);
98N/A new Exception().printStackTrace();
98N/A }
98N/A }
606N/A
606N/A private static String SJISName() throws Exception {
98N/A return detectingCharset(new byte[] {(byte)0xbb, (byte)0xdd,
98N/A (byte)0xcf, (byte)0xb2});
98N/A }
98N/A
98N/A private static String EUCJName() throws Exception {
606N/A return detectingCharset(new byte[] {(byte)0xa4, (byte)0xd2,
851N/A (byte)0xa4, (byte)0xe9});
493N/A }
98N/A
98N/A private static String detectingCharset(byte[] bytes) throws Exception {
851N/A //----------------------------------------------------------------
493N/A // Test special public methods of CharsetDecoder while we're here
231N/A //----------------------------------------------------------------
231N/A CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
851N/A check(cd.isAutoDetecting(), "isAutodecting()");
493N/A check(! cd.isCharsetDetected(), "isCharsetDetected");
98N/A cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
98N/A check(! cd.isCharsetDetected(), "isCharsetDetected");
493N/A try {
98N/A cd.detectedCharset();
98N/A fail("no IllegalStateException");
98N/A } catch (IllegalStateException e) {}
98N/A cd.decode(ByteBuffer.wrap(bytes));
98N/A check(cd.isCharsetDetected(), "isCharsetDetected");
98N/A Charset cs = cd.detectedCharset();
851N/A check(cs != null, "cs != null");
851N/A check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
659N/A return cs.name();
378N/A }
659N/A
378N/A public static void main(String[] argv) throws Exception {
606N/A //----------------------------------------------------------------
493N/A // Used to throw BufferOverflowException
606N/A //----------------------------------------------------------------
493N/A out.println(new String(new byte[] {0x61}, "JISAutoDetect"));
606N/A
493N/A //----------------------------------------------------------------
98N/A // InputStreamReader(...JISAutoDetect) used to infloop
493N/A //----------------------------------------------------------------
606N/A {
606N/A byte[] bytes = "ABCD\n".getBytes();
98N/A ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
493N/A InputStreamReader isr = new InputStreamReader(bais, "JISAutoDetect");
606N/A BufferedReader reader = new BufferedReader(isr);
606N/A check (reader.readLine().equals("ABCD"), "first read gets text");
98N/A // used to return "ABCD" on second and subsequent reads
98N/A check (reader.readLine() == null, "second read gets null");
493N/A }
606N/A
606N/A //----------------------------------------------------------------
98N/A // Check all Japanese chars for sanity
98N/A //----------------------------------------------------------------
493N/A String SJIS = SJISName();
98N/A String EUCJ = EUCJName();
606N/A out.printf("SJIS charset is %s%n", SJIS);
606N/A out.printf("EUCJ charset is %s%n", EUCJ);
231N/A
606N/A int cnt2022 = 0;
606N/A int cnteucj = 0;
231N/A int cntsjis = 0;
231N/A int cntBAD = 0;
606N/A for (char c = '\u0000'; c < '\uffff'; c++) {
606N/A if (c == '\u001b' || // ESC
231N/A c == '\u2014') // Em-Dash?
231N/A continue;
606N/A String s = new String (new char[] {c});
606N/A
98N/A //----------------------------------------------------------------
98N/A // 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);
//----------------------------------------------------------------
// tests for specific byte sequences
//----------------------------------------------------------------
test("ISO-2022-JP", new byte[] {'A', 'B', 'C'});
test("EUC-JP", new byte[] {'A', 'B', 'C'});
test("SJIS", new byte[] {'A', 'B', 'C'});
test("SJIS",
new byte[] { 'C', 'o', 'p', 'y', 'r', 'i', 'g', 'h', 't',
' ', (byte)0xa9, ' ', '1', '9', '9', '8' });
test("SJIS",
new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
(byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 });
test("EUC-JP",
new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,
(byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca });
test("SJIS",
new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde});
test("SJIS",
new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd });
test("SJIS",
new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa });
test("EUC-JP",
new byte[] { (byte)0x8f, (byte)0xc5, (byte)0xe0, (byte)0x20});
test("EUC-JP",
new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
(byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 });
test("ISO-2022-JP",
new byte[] { 0x1b, '$', 'B', '#', '4', '$', '5', 0x1b, '(', 'B' });
//----------------------------------------------------------------
// Check handling of ambiguous end-of-input in middle of first char
//----------------------------------------------------------------
{
CharsetDecoder dc = Charset.forName("x-JISAutoDetect").newDecoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CharBuffer cb = CharBuffer.allocate(128);
bb.put((byte)'A').put((byte)0x8f);
bb.flip();
CoderResult res = dc.decode(bb,cb,false);
check(res.isUnderflow(), "isUnderflow");
check(bb.position() == 1, "bb.position()");
check(cb.position() == 1, "cb.position()");
res = dc.decode(bb,cb,false);
check(res.isUnderflow(), "isUnderflow");
check(bb.position() == 1, "bb.position()");
check(cb.position() == 1, "cb.position()");
bb.compact();
bb.put((byte)0xa1);
bb.flip();
res = dc.decode(bb,cb,true);
check(res.isUnderflow(), "isUnderflow");
check(bb.position() == 2, "bb.position()");
check(cb.position() == 2, "cb.position()");
}
if (failures > 0)
throw new RuntimeException(failures + " tests failed");
}
static void checkCoderResult(CoderResult result) {
check(result.isUnderflow(),
"Unexpected coder result: " + result);
}
static void test(String expectedCharset, byte[] input) throws Exception {
Charset cs = Charset.forName("x-JISAutoDetect");
CharsetDecoder autoDetect = cs.newDecoder();
Charset cs2 = Charset.forName(expectedCharset);
CharsetDecoder decoder = cs2.newDecoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CharBuffer charOutput = CharBuffer.allocate(128);
CharBuffer charExpected = CharBuffer.allocate(128);
bb.put(input);
bb.flip();
bb.mark();
CoderResult result = autoDetect.decode(bb, charOutput, true);
checkCoderResult(result);
charOutput.flip();
String actual = charOutput.toString();
bb.reset();
result = decoder.decode(bb, charExpected, true);
checkCoderResult(result);
charExpected.flip();
String expected = charExpected.toString();
check(actual.equals(expected),
String.format("actual=%s expected=%s", actual, expected));
}
}