1238N/A/*
3261N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
1238N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1238N/A *
1238N/A * This code is free software; you can redistribute it and/or modify it
1238N/A * under the terms of the GNU General Public License version 2 only, as
1238N/A * published by the Free Software Foundation.
1238N/A *
1238N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1238N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1238N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1238N/A * version 2 for more details (a copy is included in the LICENSE file that
1238N/A * accompanied this code).
1238N/A *
1238N/A * You should have received a copy of the GNU General Public License version
1238N/A * 2 along with this work; if not, write to the Free Software Foundation,
1238N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1238N/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.
1238N/A */
1238N/A
1238N/A/*
1238N/A * @test
1238N/A * @bug 6831794
1238N/A * @summary Test X11CNS charset
1238N/A */
1238N/A
1238N/Aimport java.nio.charset.*;
1238N/Aimport java.nio.*;
1238N/Aimport java.util.*;
1238N/A
1238N/Apublic class TestX11CNS {
1238N/A static char[] decode(byte[] bb, Charset cs)
1238N/A throws Exception {
1238N/A CharsetDecoder dec = cs.newDecoder();
1238N/A ByteBuffer bbf = ByteBuffer.wrap(bb);
1238N/A CharBuffer cbf = CharBuffer.allocate(bb.length);
1238N/A CoderResult cr = dec.decode(bbf, cbf, true);
1238N/A if (cr != CoderResult.UNDERFLOW) {
1238N/A System.out.println("DEC-----------------");
1238N/A int pos = bbf.position();
1238N/A System.out.printf(" cr=%s, bbf.pos=%d, bb[pos]=%x,%x,%x,%x%n",
1238N/A cr.toString(), pos,
1238N/A bb[pos++]&0xff, bb[pos++]&0xff,bb[pos++]&0xff, bb[pos++]&0xff);
1238N/A throw new RuntimeException("Decoding err: " + cs.name());
1238N/A }
1238N/A char[] cc = new char[cbf.position()];
1238N/A cbf.flip(); cbf.get(cc);
1238N/A return cc;
1238N/A
1238N/A }
1238N/A
1238N/A static byte[] encode(char[] cc, Charset cs)
1238N/A throws Exception {
1238N/A ByteBuffer bbf = ByteBuffer.allocate(cc.length * 4);
1238N/A CharBuffer cbf = CharBuffer.wrap(cc);
1238N/A CharsetEncoder enc = cs.newEncoder();
1238N/A
1238N/A CoderResult cr = enc.encode(cbf, bbf, true);
1238N/A if (cr != CoderResult.UNDERFLOW) {
1238N/A System.out.println("ENC-----------------");
1238N/A int pos = cbf.position();
1238N/A System.out.printf(" cr=%s, cbf.pos=%d, cc[pos]=%x%n",
1238N/A cr.toString(), pos, cc[pos]&0xffff);
1238N/A throw new RuntimeException("Encoding err: " + cs.name());
1238N/A }
1238N/A byte[] bb = new byte[bbf.position()];
1238N/A bbf.flip(); bbf.get(bb);
1238N/A return bb;
1238N/A }
1238N/A
1238N/A static char[] getChars(Charset newCS, Charset oldCS) {
1238N/A CharsetEncoder enc = oldCS.newEncoder();
1238N/A CharsetEncoder encNew = newCS.newEncoder();
1238N/A char[] cc = new char[0x10000];
1238N/A int pos = 0;
1238N/A int i = 0;
1238N/A while (i < 0x10000) {
1238N/A if (i == 0x4ea0 || i == 0x51ab || i == 0x52f9) {
1238N/A i++;continue;
1238N/A }
1238N/A if (enc.canEncode((char)i) != encNew.canEncode((char)i)) {
1238N/A System.out.printf(" Err i=%x%n", i);
1238N/A //throw new RuntimeException("canEncode() err!");
1238N/A }
1238N/A if (enc.canEncode((char)i)) {
1238N/A cc[pos++] = (char)i;
1238N/A }
1238N/A i++;
1238N/A }
1238N/A return Arrays.copyOf(cc, pos);
1238N/A }
1238N/A
1238N/A static void compare(Charset newCS, Charset oldCS) throws Exception {
2538N/A if (newCS == null)
2538N/A return; // does not exist on this platform
1238N/A char[] cc = getChars(newCS, oldCS);
1238N/A System.out.printf(" Diff <%s> <%s>...%n", newCS.name(), oldCS.name());
1238N/A
1238N/A byte[] bb1 = encode(cc, newCS);
1238N/A byte[] bb2 = encode(cc, oldCS);
1238N/A
1238N/A if (!Arrays.equals(bb1, bb2)) {
1238N/A System.out.printf(" encoding failed!%n");
1238N/A }
1238N/A char[] cc1 = decode(bb1, newCS);
1238N/A char[] cc2 = decode(bb1, oldCS);
1238N/A if (!Arrays.equals(cc1, cc2)) {
1238N/A for (int i = 0; i < cc1.length; i++) {
1238N/A if (cc1[i] != cc2[i]) {
1238N/A System.out.printf("i=%d, cc1=%x cc2=%x, bb=<%x%x>%n",
1238N/A i,
1238N/A cc1[i]&0xffff, cc2[i]&0xffff,
1238N/A bb1[i*2]&0xff, bb1[i*2+1]&0xff);
1238N/A }
1238N/A
1238N/A }
1238N/A
1238N/A System.out.printf(" decoding failed%n");
1238N/A }
1238N/A }
1238N/A
2538N/A private static Charset getCharset(String czName)
2538N/A throws Exception {
2538N/A try {
2538N/A return (Charset)Class.forName(czName).newInstance();
2538N/A } catch (ClassNotFoundException e){}
2538N/A return null; // does not exist
2538N/A }
2538N/A
1238N/A public static void main(String[] args) throws Exception {
2538N/A compare(getCharset("sun.awt.motif.X11CNS11643P1"),
1238N/A new X11CNS11643P1());
1238N/A
2538N/A compare(getCharset("sun.awt.motif.X11CNS11643P2"),
1238N/A new X11CNS11643P2());
1238N/A
2538N/A compare(getCharset("sun.awt.motif.X11CNS11643P3"),
1238N/A new X11CNS11643P3());
1238N/A
1238N/A }
1238N/A}