2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @bug 4085160 4139951 5005831
|
|
26 |
* @summary Test that required character encodings are supported
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.io.InputStreamReader;
|
|
30 |
import java.io.OutputStreamWriter;
|
|
31 |
import java.io.ByteArrayInputStream;
|
|
32 |
import java.io.ByteArrayOutputStream;
|
|
33 |
import java.nio.charset.Charset;
|
|
34 |
import java.nio.charset.UnsupportedCharsetException;
|
|
35 |
|
|
36 |
|
|
37 |
public class Encodings {
|
|
38 |
|
|
39 |
|
|
40 |
static boolean equals(byte[] a, byte[] b) {
|
|
41 |
if (a.length != b.length) return false;
|
|
42 |
for (int i = 0; i < a.length; i++)
|
|
43 |
if (a[i] != b[i]) return false;
|
|
44 |
return true;
|
|
45 |
}
|
|
46 |
|
|
47 |
|
|
48 |
static void go(String enc, String str, final byte[] bytes, boolean bidir)
|
|
49 |
throws Exception
|
|
50 |
{
|
|
51 |
final Charset charset = Charset.forName(enc);
|
|
52 |
|
|
53 |
/* String(byte[] bs, String enc) */
|
|
54 |
if (!(new String(bytes, enc).equals(str)))
|
|
55 |
throw new Exception(enc + ": String constructor failed");
|
|
56 |
|
|
57 |
/* String(byte[] bs, Charset charset) */
|
|
58 |
if (!(new String(bytes, charset).equals(str)))
|
|
59 |
throw new Exception(charset + ": String constructor failed");
|
|
60 |
|
|
61 |
/* String(byte[] bs, int off, int len, Charset charset) */
|
|
62 |
String start = str.substring(0, 2);
|
|
63 |
String end = str.substring(2);
|
|
64 |
if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
|
|
65 |
if (!(new String(bytes, 0, 4, charset).equals(start)))
|
|
66 |
throw new Exception(charset + ": String constructor failed");
|
|
67 |
if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
|
|
68 |
throw new Exception(charset + ": String constructor failed");
|
|
69 |
} else if (enc.equals("UTF-16")) {
|
|
70 |
if (!(new String(bytes, 0, 6, charset).equals(start)))
|
|
71 |
throw new Exception(charset + ": String constructor failed");
|
|
72 |
} else {
|
|
73 |
if (!(new String(bytes, 0, 2, charset).equals(start)))
|
|
74 |
throw new Exception(charset + ": String constructor failed");
|
|
75 |
if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
|
|
76 |
throw new Exception(charset + ": String constructor failed");
|
|
77 |
}
|
|
78 |
|
|
79 |
/* InputStreamReader */
|
|
80 |
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
|
|
81 |
InputStreamReader r = new InputStreamReader(bi, enc);
|
|
82 |
String inEnc = r.getEncoding();
|
|
83 |
int n = str.length();
|
|
84 |
char[] cs = new char[n];
|
|
85 |
for (int i = 0; i < n;) {
|
|
86 |
int m;
|
|
87 |
if ((m = r.read(cs, i, n - i)) < 0)
|
|
88 |
throw new Exception(enc + ": EOF on InputStreamReader");
|
|
89 |
i += m;
|
|
90 |
}
|
|
91 |
if (!(new String(cs).equals(str)))
|
|
92 |
throw new Exception(enc + ": InputStreamReader failed");
|
|
93 |
|
|
94 |
if (!bidir) {
|
|
95 |
System.err.println(enc + " --> " + inEnc);
|
|
96 |
return;
|
|
97 |
}
|
|
98 |
|
|
99 |
/* String.getBytes(String enc) */
|
|
100 |
byte[] bs = str.getBytes(enc);
|
|
101 |
if (!equals(bs, bytes))
|
|
102 |
throw new Exception(enc + ": String.getBytes failed");
|
|
103 |
|
|
104 |
/* String.getBytes(Charset charset) */
|
|
105 |
bs = str.getBytes(charset);
|
|
106 |
if (!equals(bs, bytes))
|
|
107 |
throw new Exception(charset + ": String.getBytes failed");
|
|
108 |
|
|
109 |
// Calls to String.getBytes(Charset) shouldn't automatically
|
|
110 |
// use the cached thread-local encoder.
|
|
111 |
if (charset.name().equals("UTF-16BE")) {
|
|
112 |
String s = new String(bytes, charset);
|
|
113 |
// Replace the thread-local encoder with this one.
|
|
114 |
byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
|
|
115 |
if (bytes.length != bb.length) {
|
|
116 |
// Incidental test.
|
|
117 |
throw new RuntimeException("unequal length: "
|
|
118 |
+ bytes.length + " != "
|
|
119 |
+ bb.length);
|
|
120 |
} else {
|
|
121 |
boolean diff = false;
|
|
122 |
// Expect different byte[] between UTF-16LE and UTF-16BE
|
|
123 |
// even though encoder was previously cached by last call
|
|
124 |
// to getBytes().
|
|
125 |
for (int i = 0; i < bytes.length; i++) {
|
|
126 |
if (bytes[i] != bb[i])
|
|
127 |
diff = true;
|
|
128 |
}
|
|
129 |
if (!diff)
|
|
130 |
throw new RuntimeException("byte arrays equal");
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
/* OutputStreamWriter */
|
|
135 |
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
|
136 |
OutputStreamWriter w = new OutputStreamWriter(bo, enc);
|
|
137 |
String outEnc = w.getEncoding();
|
|
138 |
w.write(str);
|
|
139 |
w.close();
|
|
140 |
bs = bo.toByteArray();
|
|
141 |
if (!equals(bs, bytes))
|
|
142 |
throw new Exception(enc + ": OutputStreamWriter failed");
|
|
143 |
|
|
144 |
System.err.println(enc + " --> " + inEnc + " / " + outEnc);
|
|
145 |
}
|
|
146 |
|
|
147 |
|
|
148 |
static void go(String enc, String str, byte[] bytes) throws Exception {
|
|
149 |
go(enc, str, bytes, true);
|
|
150 |
}
|
|
151 |
|
|
152 |
|
|
153 |
public static void main(String[] args) throws Exception {
|
|
154 |
|
|
155 |
go("US-ASCII", "abc", new byte[] { 'a', 'b', 'c' });
|
|
156 |
go("us-ascii", "abc", new byte[] { 'a', 'b', 'c' });
|
|
157 |
go("ISO646-US", "abc", new byte[] { 'a', 'b', 'c' });
|
|
158 |
go("ISO-8859-1", "ab\u00c7", new byte[] { 'a', 'b', (byte)'\u00c7' });
|
|
159 |
go("UTF-8", "ab\u1e09",
|
|
160 |
new byte[] { 'a', 'b',
|
|
161 |
(byte)(0xe0 | (0x0f & (0x1e09 >> 12))),
|
|
162 |
(byte)(0x80 | (0x3f & (0x1e09 >> 6))),
|
|
163 |
(byte)(0x80 | (0x3f & 0x1e09)) });
|
|
164 |
go("UTF-16BE", "ab\u1e09",
|
|
165 |
new byte[] { 0, 'a', 0, 'b', 0x1e, 0x09 });
|
|
166 |
go("UTF-16LE", "ab\u1e09",
|
|
167 |
new byte[] { 'a', 0, 'b', 0, 0x09, 0x1e });
|
|
168 |
|
|
169 |
/* UTF-16 accepts both byte orders on input but always uses big-endian
|
|
170 |
* on output, so test all three cases
|
|
171 |
*/
|
|
172 |
go("UTF-16", "ab\u1e09",
|
|
173 |
new byte[] { (byte)0xfe, (byte)0xff, 0, 'a', 0, 'b', 0x1e, 0x09 });
|
|
174 |
go("UTF-16", "ab\u1e09",
|
|
175 |
new byte[] { (byte)0xff, (byte)0xfe, 'a', 0, 'b', 0, 0x09, 0x1e },
|
|
176 |
false);
|
|
177 |
}
|
|
178 |
|
|
179 |
}
|