1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
* Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4085160 4139951 5005831
* @summary Test that required character encodings are supported
*/
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
public class Encodings {
static boolean equals(byte[] a, byte[] b) {
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++)
if (a[i] != b[i]) return false;
return true;
}
static void go(String enc, String str, final byte[] bytes, boolean bidir)
throws Exception
{
final Charset charset = Charset.forName(enc);
/* String(byte[] bs, String enc) */
if (!(new String(bytes, enc).equals(str)))
throw new Exception(enc + ": String constructor failed");
/* String(byte[] bs, Charset charset) */
if (!(new String(bytes, charset).equals(str)))
throw new Exception(charset + ": String constructor failed");
/* String(byte[] bs, int off, int len, Charset charset) */
String start = str.substring(0, 2);
String end = str.substring(2);
if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
if (!(new String(bytes, 0, 4, charset).equals(start)))
throw new Exception(charset + ": String constructor failed");
if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
throw new Exception(charset + ": String constructor failed");
} else if (enc.equals("UTF-16")) {
if (!(new String(bytes, 0, 6, charset).equals(start)))
throw new Exception(charset + ": String constructor failed");
} else {
if (!(new String(bytes, 0, 2, charset).equals(start)))
throw new Exception(charset + ": String constructor failed");
if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
throw new Exception(charset + ": String constructor failed");
}
/* InputStreamReader */
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
InputStreamReader r = new InputStreamReader(bi, enc);
String inEnc = r.getEncoding();
int n = str.length();
char[] cs = new char[n];
for (int i = 0; i < n;) {
int m;
if ((m = r.read(cs, i, n - i)) < 0)
throw new Exception(enc + ": EOF on InputStreamReader");
i += m;
}
if (!(new String(cs).equals(str)))
throw new Exception(enc + ": InputStreamReader failed");
if (!bidir) {
System.err.println(enc + " --> " + inEnc);
return;
}
/* String.getBytes(String enc) */
byte[] bs = str.getBytes(enc);
if (!equals(bs, bytes))
throw new Exception(enc + ": String.getBytes failed");
/* String.getBytes(Charset charset) */
bs = str.getBytes(charset);
if (!equals(bs, bytes))
throw new Exception(charset + ": String.getBytes failed");
// Calls to String.getBytes(Charset) shouldn't automatically
// use the cached thread-local encoder.
if (charset.name().equals("UTF-16BE")) {
String s = new String(bytes, charset);
// Replace the thread-local encoder with this one.
byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
if (bytes.length != bb.length) {
// Incidental test.
throw new RuntimeException("unequal length: "
+ bytes.length + " != "
+ bb.length);
} else {
boolean diff = false;
// Expect different byte[] between UTF-16LE and UTF-16BE
// even though encoder was previously cached by last call
// to getBytes().
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] != bb[i])
diff = true;
}
if (!diff)
throw new RuntimeException("byte arrays equal");
}
}
/* OutputStreamWriter */
ByteArrayOutputStream bo = new ByteArrayOutputStream();
OutputStreamWriter w = new OutputStreamWriter(bo, enc);
String outEnc = w.getEncoding();
w.write(str);
w.close();
bs = bo.toByteArray();
if (!equals(bs, bytes))
throw new Exception(enc + ": OutputStreamWriter failed");
System.err.println(enc + " --> " + inEnc + " / " + outEnc);
}
static void go(String enc, String str, byte[] bytes) throws Exception {
go(enc, str, bytes, true);
}
public static void main(String[] args) throws Exception {
go("US-ASCII", "abc", new byte[] { 'a', 'b', 'c' });
go("us-ascii", "abc", new byte[] { 'a', 'b', 'c' });
go("ISO646-US", "abc", new byte[] { 'a', 'b', 'c' });
go("ISO-8859-1", "ab\u00c7", new byte[] { 'a', 'b', (byte)'\u00c7' });
go("UTF-8", "ab\u1e09",
new byte[] { 'a', 'b',
(byte)(0xe0 | (0x0f & (0x1e09 >> 12))),
(byte)(0x80 | (0x3f & (0x1e09 >> 6))),
(byte)(0x80 | (0x3f & 0x1e09)) });
go("UTF-16BE", "ab\u1e09",
new byte[] { 0, 'a', 0, 'b', 0x1e, 0x09 });
go("UTF-16LE", "ab\u1e09",
new byte[] { 'a', 0, 'b', 0, 0x09, 0x1e });
/* UTF-16 accepts both byte orders on input but always uses big-endian
* on output, so test all three cases
*/
go("UTF-16", "ab\u1e09",
new byte[] { (byte)0xfe, (byte)0xff, 0, 'a', 0, 'b', 0x1e, 0x09 });
go("UTF-16", "ab\u1e09",
new byte[] { (byte)0xff, (byte)0xfe, 'a', 0, 'b', 0, 0x09, 0x1e },
false);
}
}
|