Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/nio/cs/NIOJISAutoDetectTest.java
38839 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4831163 5053096 505644026* @summary NIO charset basic verification of JISAutodetect decoder27* @author Martin Buchholz28*/2930import java.io.*;31import java.nio.ByteBuffer;32import java.nio.CharBuffer;33import java.nio.charset.Charset;34import java.nio.charset.CharsetDecoder;35import java.nio.charset.CoderResult;36import static java.lang.System.*;3738public class NIOJISAutoDetectTest {39private static int failures = 0;4041private static void fail(String failureMsg) {42System.out.println(failureMsg);43failures++;44}4546private static void check(boolean cond, String msg) {47if (!cond) {48fail("test failed: " + msg);49new Exception().printStackTrace();50}51}5253private static String SJISName() throws Exception {54return detectingCharset(new byte[] {(byte)0xbb, (byte)0xdd,55(byte)0xcf, (byte)0xb2});56}5758private static String EUCJName() throws Exception {59return detectingCharset(new byte[] {(byte)0xa4, (byte)0xd2,60(byte)0xa4, (byte)0xe9});61}6263private static String detectingCharset(byte[] bytes) throws Exception {64//----------------------------------------------------------------65// Test special public methods of CharsetDecoder while we're here66//----------------------------------------------------------------67CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();68check(cd.isAutoDetecting(), "isAutodecting()");69check(! cd.isCharsetDetected(), "isCharsetDetected");70cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));71check(! cd.isCharsetDetected(), "isCharsetDetected");72try {73cd.detectedCharset();74fail("no IllegalStateException");75} catch (IllegalStateException e) {}76cd.decode(ByteBuffer.wrap(bytes));77check(cd.isCharsetDetected(), "isCharsetDetected");78Charset cs = cd.detectedCharset();79check(cs != null, "cs != null");80check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");81return cs.name();82}8384public static void main(String[] argv) throws Exception {85//----------------------------------------------------------------86// Used to throw BufferOverflowException87//----------------------------------------------------------------88out.println(new String(new byte[] {0x61}, "JISAutoDetect"));8990//----------------------------------------------------------------91// InputStreamReader(...JISAutoDetect) used to infloop92//----------------------------------------------------------------93{94byte[] bytes = "ABCD\n".getBytes();95ByteArrayInputStream bais = new ByteArrayInputStream(bytes);96InputStreamReader isr = new InputStreamReader(bais, "JISAutoDetect");97BufferedReader reader = new BufferedReader(isr);98check (reader.readLine().equals("ABCD"), "first read gets text");99// used to return "ABCD" on second and subsequent reads100check (reader.readLine() == null, "second read gets null");101}102103//----------------------------------------------------------------104// Check all Japanese chars for sanity105//----------------------------------------------------------------106String SJIS = SJISName();107String EUCJ = EUCJName();108out.printf("SJIS charset is %s%n", SJIS);109out.printf("EUCJ charset is %s%n", EUCJ);110111int cnt2022 = 0;112int cnteucj = 0;113int cntsjis = 0;114int cntBAD = 0;115for (char c = '\u0000'; c < '\uffff'; c++) {116if (c == '\u001b' || // ESC117c == '\u2014') // Em-Dash?118continue;119String s = new String (new char[] {c});120121//----------------------------------------------------------------122// JISAutoDetect can handle all chars that EUC-JP can,123// unless there is an ambiguity with SJIS.124//----------------------------------------------------------------125byte[] beucj = s.getBytes(EUCJ);126String seucj = new String(beucj, EUCJ);127if (seucj.equals(s)) {128cnteucj++;129String sauto = new String(beucj, "JISAutoDetect");130131if (! sauto.equals(seucj)) {132cntBAD++;133String ssjis = new String(beucj, SJIS);134if (! sauto.equals(ssjis)) {135fail("Autodetection agrees with neither EUC nor SJIS");136}137}138} else139continue; // Optimization140141//----------------------------------------------------------------142// JISAutoDetect can handle all chars that ISO-2022-JP can.143//----------------------------------------------------------------144byte[] b2022 = s.getBytes("ISO-2022-JP");145if (new String(b2022, "ISO-2022-JP").equals(s)) {146cnt2022++;147check(new String(b2022,"JISAutoDetect").equals(s),148"ISO2022 autodetection");149}150151//----------------------------------------------------------------152// JISAutoDetect can handle almost all chars that SJIS can.153//----------------------------------------------------------------154byte[] bsjis = s.getBytes(SJIS);155if (new String(bsjis, SJIS).equals(s)) {156cntsjis++;157check(new String(bsjis,"JISAutoDetect").equals(s),158"SJIS autodetection");159}160}161out.printf("There are %d ISO-2022-JP-encodable characters.%n", cnt2022);162out.printf("There are %d SJIS-encodable characters.%n", cntsjis);163out.printf("There are %d EUC-JP-encodable characters.%n", cnteucj);164out.printf("There are %d characters that are " +165"misdetected as SJIS after being EUC-encoded.%n", cntBAD);166167168//----------------------------------------------------------------169// tests for specific byte sequences170//----------------------------------------------------------------171test("ISO-2022-JP", new byte[] {'A', 'B', 'C'});172test("EUC-JP", new byte[] {'A', 'B', 'C'});173test("SJIS", new byte[] {'A', 'B', 'C'});174175test("SJIS",176new byte[] { 'C', 'o', 'p', 'y', 'r', 'i', 'g', 'h', 't',177' ', (byte)0xa9, ' ', '1', '9', '9', '8' });178179test("SJIS",180new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,181(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,182(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,183(byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 });184185test("EUC-JP",186new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,187(byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca });188189test("SJIS",190new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,191(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,192(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde});193194test("SJIS",195new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,196(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,197(byte)0xc3, (byte)0xd1, (byte)0xbd });198199test("SJIS",200new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa });201202test("EUC-JP",203new byte[] { (byte)0x8f, (byte)0xc5, (byte)0xe0, (byte)0x20});204205test("EUC-JP",206new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,207(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,208(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,209(byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 });210211test("ISO-2022-JP",212new byte[] { 0x1b, '$', 'B', '#', '4', '$', '5', 0x1b, '(', 'B' });213214215//----------------------------------------------------------------216// Check handling of ambiguous end-of-input in middle of first char217//----------------------------------------------------------------218{219CharsetDecoder dc = Charset.forName("x-JISAutoDetect").newDecoder();220ByteBuffer bb = ByteBuffer.allocate(128);221CharBuffer cb = CharBuffer.allocate(128);222bb.put((byte)'A').put((byte)0x8f);223bb.flip();224CoderResult res = dc.decode(bb,cb,false);225check(res.isUnderflow(), "isUnderflow");226check(bb.position() == 1, "bb.position()");227check(cb.position() == 1, "cb.position()");228res = dc.decode(bb,cb,false);229check(res.isUnderflow(), "isUnderflow");230check(bb.position() == 1, "bb.position()");231check(cb.position() == 1, "cb.position()");232bb.compact();233bb.put((byte)0xa1);234bb.flip();235res = dc.decode(bb,cb,true);236check(res.isUnderflow(), "isUnderflow");237check(bb.position() == 2, "bb.position()");238check(cb.position() == 2, "cb.position()");239}240241242if (failures > 0)243throw new RuntimeException(failures + " tests failed");244}245246static void checkCoderResult(CoderResult result) {247check(result.isUnderflow(),248"Unexpected coder result: " + result);249}250251static void test(String expectedCharset, byte[] input) throws Exception {252Charset cs = Charset.forName("x-JISAutoDetect");253CharsetDecoder autoDetect = cs.newDecoder();254255Charset cs2 = Charset.forName(expectedCharset);256CharsetDecoder decoder = cs2.newDecoder();257258ByteBuffer bb = ByteBuffer.allocate(128);259CharBuffer charOutput = CharBuffer.allocate(128);260CharBuffer charExpected = CharBuffer.allocate(128);261262bb.put(input);263bb.flip();264bb.mark();265266CoderResult result = autoDetect.decode(bb, charOutput, true);267checkCoderResult(result);268charOutput.flip();269String actual = charOutput.toString();270271bb.reset();272273result = decoder.decode(bb, charExpected, true);274checkCoderResult(result);275charExpected.flip();276String expected = charExpected.toString();277278check(actual.equals(expected),279String.format("actual=%s expected=%s", actual, expected));280}281}282283284