2914
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
|
2914
|
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 |
*
|
5506
|
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.
|
2914
|
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 |
char[] cc = getChars(newCS, oldCS);
|
|
98 |
System.out.printf(" Diff <%s> <%s>...%n", newCS.name(), oldCS.name());
|
|
99 |
|
|
100 |
byte[] bb1 = encode(cc, newCS);
|
|
101 |
byte[] bb2 = encode(cc, oldCS);
|
|
102 |
|
|
103 |
if (!Arrays.equals(bb1, bb2)) {
|
|
104 |
System.out.printf(" encoding failed!%n");
|
|
105 |
}
|
|
106 |
char[] cc1 = decode(bb1, newCS);
|
|
107 |
char[] cc2 = decode(bb1, oldCS);
|
|
108 |
if (!Arrays.equals(cc1, cc2)) {
|
|
109 |
for (int i = 0; i < cc1.length; i++) {
|
|
110 |
if (cc1[i] != cc2[i]) {
|
|
111 |
System.out.printf("i=%d, cc1=%x cc2=%x, bb=<%x%x>%n",
|
|
112 |
i,
|
|
113 |
cc1[i]&0xffff, cc2[i]&0xffff,
|
|
114 |
bb1[i*2]&0xff, bb1[i*2+1]&0xff);
|
|
115 |
}
|
|
116 |
|
|
117 |
}
|
|
118 |
|
|
119 |
System.out.printf(" decoding failed%n");
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
public static void main(String[] args) throws Exception {
|
|
124 |
compare(new sun.awt.motif.X11CNS11643P1(),
|
|
125 |
new X11CNS11643P1());
|
|
126 |
|
|
127 |
compare(new sun.awt.motif.X11CNS11643P2(),
|
|
128 |
new X11CNS11643P2());
|
|
129 |
|
|
130 |
compare(new sun.awt.motif.X11CNS11643P3(),
|
|
131 |
new X11CNS11643P3());
|
|
132 |
|
|
133 |
}
|
|
134 |
}
|