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