796
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
|
796
|
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.
|
796
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
@bug 4896454
|
|
26 |
@summary Check GB18030 surrogate encoding/decoding handling
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.nio.*;
|
|
30 |
import java.nio.charset.*;
|
|
31 |
|
|
32 |
public class SurrogateGB18030Test {
|
|
33 |
public static void main(String[] args) throws Exception {
|
|
34 |
SurrogateGB18030Test test = new SurrogateGB18030Test();
|
|
35 |
|
|
36 |
test.roundtripTest();
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Valid Surrogate pair and 4 byte GB18030 representation
|
|
40 |
*/
|
|
41 |
|
|
42 |
String inputString = "\uD800\uDC00";
|
|
43 |
|
|
44 |
byte[] expectedBytes = { (byte)0x90,
|
|
45 |
(byte)0x30,
|
|
46 |
(byte)0x81,
|
|
47 |
(byte)0x30
|
|
48 |
};
|
|
49 |
test.encodeTest(inputString, expectedBytes);
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Vice-versa : check that 4 byte GB18030 value encodes correctly
|
|
53 |
*/
|
|
54 |
|
|
55 |
String expectedStr = "\uDBFF\uDFFF";
|
|
56 |
|
|
57 |
byte[] inputBytes = { (byte)0xe3,
|
|
58 |
(byte)0x32,
|
|
59 |
(byte)0x9a,
|
|
60 |
(byte)0x35
|
|
61 |
};
|
|
62 |
|
|
63 |
|
|
64 |
test.decodeTest(inputBytes, expectedStr);
|
|
65 |
|
|
66 |
}
|
|
67 |
|
|
68 |
private void roundtripTest() throws Exception
|
|
69 |
{
|
|
70 |
byte[] ba;
|
|
71 |
char[] pair = new char[2];
|
|
72 |
for (char high = '\ud800'; high <= '\udbff'; high++) {
|
|
73 |
for (char low = '\udc00'; low <= '\udfff'; low++) {
|
|
74 |
pair[0] = high;
|
|
75 |
pair[1] = low;
|
|
76 |
String s = new String(pair);
|
|
77 |
if (!s.equals(new String(s.getBytes("gb18030"), "gb18030")))
|
|
78 |
throw new Exception ("GB18030 roundtrip failure");
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
}
|
|
83 |
|
|
84 |
private void encodeTest(String inputString, byte[] expectedBytes)
|
|
85 |
throws Exception
|
|
86 |
{
|
|
87 |
byte[] encoded = inputString.getBytes("GB18030");
|
|
88 |
|
|
89 |
CharBuffer cb = CharBuffer.wrap(inputString.toCharArray());
|
|
90 |
ByteBuffer bb = ByteBuffer.allocate(4);
|
|
91 |
|
|
92 |
CharsetEncoder encoder = Charset.forName("GB18030").newEncoder();
|
|
93 |
encoder.encode(cb, bb, true);
|
|
94 |
|
|
95 |
bb.flip();
|
|
96 |
for (int i = 0 ; i < expectedBytes.length; i++) {
|
|
97 |
if (encoded[i] != expectedBytes[i]
|
|
98 |
|| bb.get() != expectedBytes[i])
|
|
99 |
throw new Exception ("GB18030 encode failure");
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
private void decodeTest(byte[] inputBytes, String expectedStr)
|
|
104 |
throws Exception
|
|
105 |
{
|
|
106 |
String s2 = new String(inputBytes, "GB18030");
|
|
107 |
|
|
108 |
CharsetDecoder decoder = Charset.forName("GB18030").newDecoder();
|
|
109 |
|
|
110 |
ByteBuffer bb = ByteBuffer.wrap(inputBytes);
|
|
111 |
CharBuffer cb = CharBuffer.allocate(2);
|
|
112 |
decoder.decode(bb, cb, true);
|
|
113 |
|
|
114 |
cb.flip();
|
|
115 |
for (int i = 0 ; i < expectedStr.length(); i++) {
|
|
116 |
if (expectedStr.charAt(i) != cb.get()
|
|
117 |
|| s2.charAt(i) != expectedStr.charAt(i))
|
|
118 |
throw new Exception ("GB18030 encode failure");
|
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|