1 /* 2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6831794 27 * @summary Test X11CNS charset 28 */ 29 30 import java.nio.charset.*; 31 import java.nio.*; 32 import java.util.*; 33 34 public class TestX11CNS { 35 static char[] decode(byte[] bb, Charset cs) 36 throws Exception { 37 CharsetDecoder dec = cs.newDecoder(); 38 ByteBuffer bbf = ByteBuffer.wrap(bb); 39 CharBuffer cbf = CharBuffer.allocate(bb.length); 40 CoderResult cr = dec.decode(bbf, cbf, true); 41 if (cr != CoderResult.UNDERFLOW) { 42 System.out.println("DEC-----------------"); 43 int pos = bbf.position(); 44 System.out.printf(" cr=%s, bbf.pos=%d, bb[pos]=%x,%x,%x,%x%n", 45 cr.toString(), pos, 46 bb[pos++]&0xff, bb[pos++]&0xff,bb[pos++]&0xff, bb[pos++]&0xff); 47 throw new RuntimeException("Decoding err: " + cs.name()); 48 } 49 char[] cc = new char[cbf.position()]; 50 cbf.flip(); cbf.get(cc); 51 return cc; 52 53 } 54 55 static byte[] encode(char[] cc, Charset cs) 56 throws Exception { 57 ByteBuffer bbf = ByteBuffer.allocate(cc.length * 4); 58 CharBuffer cbf = CharBuffer.wrap(cc); 59 CharsetEncoder enc = cs.newEncoder(); 60 61 CoderResult cr = enc.encode(cbf, bbf, true); 62 if (cr != CoderResult.UNDERFLOW) { 63 System.out.println("ENC-----------------"); 64 int pos = cbf.position(); 65 System.out.printf(" cr=%s, cbf.pos=%d, cc[pos]=%x%n", 66 cr.toString(), pos, cc[pos]&0xffff); 67 throw new RuntimeException("Encoding err: " + cs.name()); 68 } 69 byte[] bb = new byte[bbf.position()]; 70 bbf.flip(); bbf.get(bb); 71 return bb; 72 } 73 74 static char[] getChars(Charset newCS, Charset oldCS) { 75 CharsetEncoder enc = oldCS.newEncoder(); 76 CharsetEncoder encNew = newCS.newEncoder(); 77 char[] cc = new char[0x10000]; 78 int pos = 0; 79 int i = 0; 80 while (i < 0x10000) { 81 if (i == 0x4ea0 || i == 0x51ab || i == 0x52f9) { 82 i++;continue; 83 } 84 if (enc.canEncode((char)i) != encNew.canEncode((char)i)) { 85 System.out.printf(" Err i=%x%n", i); 86 //throw new RuntimeException("canEncode() err!"); 87 } 88 if (enc.canEncode((char)i)) { 89 cc[pos++] = (char)i; 90 } 91 i++; 92 } 93 return Arrays.copyOf(cc, pos); 94 } 95 96 static void compare(Charset newCS, Charset oldCS) throws Exception { 97 if (newCS == null) 98 return; // does not exist on this platform 99 char[] cc = getChars(newCS, oldCS); 100 System.out.printf(" Diff <%s> <%s>...%n", newCS.name(), oldCS.name()); 101 102 byte[] bb1 = encode(cc, newCS); 103 byte[] bb2 = encode(cc, oldCS); 104 105 if (!Arrays.equals(bb1, bb2)) { 106 System.out.printf(" encoding failed!%n"); 107 } 108 char[] cc1 = decode(bb1, newCS); 109 char[] cc2 = decode(bb1, oldCS); 110 if (!Arrays.equals(cc1, cc2)) { 111 for (int i = 0; i < cc1.length; i++) { 112 if (cc1[i] != cc2[i]) { 113 System.out.printf("i=%d, cc1=%x cc2=%x, bb=<%x%x>%n", 114 i, 115 cc1[i]&0xffff, cc2[i]&0xffff, 116 bb1[i*2]&0xff, bb1[i*2+1]&0xff); 117 } 118 119 } 120 121 System.out.printf(" decoding failed%n"); 122 } 123 } 124 125 private static Charset getCharset(String czName) 126 throws Exception { 127 try { 128 return (Charset)Class.forName(czName).newInstance(); 129 } catch (ClassNotFoundException e){} 130 return null; // does not exist 131 } 132 133 public static void main(String[] args) throws Exception { 134 compare(getCharset("sun.awt.motif.X11CNS11643P1"), 135 new X11CNS11643P1()); 136 137 compare(getCharset("sun.awt.motif.X11CNS11643P2"), 138 new X11CNS11643P2()); 139 140 compare(getCharset("sun.awt.motif.X11CNS11643P3"), 141 new X11CNS11643P3()); 142 143 } 144 }