0N/A/*
2362N/A * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4085160 4139951 5005831
0N/A * @summary Test that required character encodings are supported
0N/A */
0N/A
0N/Aimport java.io.InputStreamReader;
0N/Aimport java.io.OutputStreamWriter;
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.nio.charset.Charset;
0N/Aimport java.nio.charset.UnsupportedCharsetException;
0N/A
0N/A
0N/Apublic class Encodings {
0N/A
0N/A
0N/A static boolean equals(byte[] a, byte[] b) {
0N/A if (a.length != b.length) return false;
0N/A for (int i = 0; i < a.length; i++)
0N/A if (a[i] != b[i]) return false;
0N/A return true;
0N/A }
0N/A
0N/A
0N/A static void go(String enc, String str, final byte[] bytes, boolean bidir)
0N/A throws Exception
0N/A {
0N/A final Charset charset = Charset.forName(enc);
0N/A
0N/A /* String(byte[] bs, String enc) */
0N/A if (!(new String(bytes, enc).equals(str)))
0N/A throw new Exception(enc + ": String constructor failed");
0N/A
0N/A /* String(byte[] bs, Charset charset) */
0N/A if (!(new String(bytes, charset).equals(str)))
0N/A throw new Exception(charset + ": String constructor failed");
0N/A
0N/A /* String(byte[] bs, int off, int len, Charset charset) */
0N/A String start = str.substring(0, 2);
0N/A String end = str.substring(2);
0N/A if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {
0N/A if (!(new String(bytes, 0, 4, charset).equals(start)))
0N/A throw new Exception(charset + ": String constructor failed");
0N/A if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))
0N/A throw new Exception(charset + ": String constructor failed");
0N/A } else if (enc.equals("UTF-16")) {
0N/A if (!(new String(bytes, 0, 6, charset).equals(start)))
0N/A throw new Exception(charset + ": String constructor failed");
0N/A } else {
0N/A if (!(new String(bytes, 0, 2, charset).equals(start)))
0N/A throw new Exception(charset + ": String constructor failed");
0N/A if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))
0N/A throw new Exception(charset + ": String constructor failed");
0N/A }
0N/A
0N/A /* InputStreamReader */
0N/A ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
0N/A InputStreamReader r = new InputStreamReader(bi, enc);
0N/A String inEnc = r.getEncoding();
0N/A int n = str.length();
0N/A char[] cs = new char[n];
0N/A for (int i = 0; i < n;) {
0N/A int m;
0N/A if ((m = r.read(cs, i, n - i)) < 0)
0N/A throw new Exception(enc + ": EOF on InputStreamReader");
0N/A i += m;
0N/A }
0N/A if (!(new String(cs).equals(str)))
0N/A throw new Exception(enc + ": InputStreamReader failed");
0N/A
0N/A if (!bidir) {
0N/A System.err.println(enc + " --> " + inEnc);
0N/A return;
0N/A }
0N/A
0N/A /* String.getBytes(String enc) */
0N/A byte[] bs = str.getBytes(enc);
0N/A if (!equals(bs, bytes))
0N/A throw new Exception(enc + ": String.getBytes failed");
0N/A
0N/A /* String.getBytes(Charset charset) */
0N/A bs = str.getBytes(charset);
0N/A if (!equals(bs, bytes))
0N/A throw new Exception(charset + ": String.getBytes failed");
0N/A
0N/A // Calls to String.getBytes(Charset) shouldn't automatically
0N/A // use the cached thread-local encoder.
0N/A if (charset.name().equals("UTF-16BE")) {
0N/A String s = new String(bytes, charset);
0N/A // Replace the thread-local encoder with this one.
0N/A byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));
0N/A if (bytes.length != bb.length) {
0N/A // Incidental test.
0N/A throw new RuntimeException("unequal length: "
0N/A + bytes.length + " != "
0N/A + bb.length);
0N/A } else {
0N/A boolean diff = false;
0N/A // Expect different byte[] between UTF-16LE and UTF-16BE
0N/A // even though encoder was previously cached by last call
0N/A // to getBytes().
0N/A for (int i = 0; i < bytes.length; i++) {
0N/A if (bytes[i] != bb[i])
0N/A diff = true;
0N/A }
0N/A if (!diff)
0N/A throw new RuntimeException("byte arrays equal");
0N/A }
0N/A }
0N/A
0N/A /* OutputStreamWriter */
0N/A ByteArrayOutputStream bo = new ByteArrayOutputStream();
0N/A OutputStreamWriter w = new OutputStreamWriter(bo, enc);
0N/A String outEnc = w.getEncoding();
0N/A w.write(str);
0N/A w.close();
0N/A bs = bo.toByteArray();
0N/A if (!equals(bs, bytes))
0N/A throw new Exception(enc + ": OutputStreamWriter failed");
0N/A
0N/A System.err.println(enc + " --> " + inEnc + " / " + outEnc);
0N/A }
0N/A
0N/A
0N/A static void go(String enc, String str, byte[] bytes) throws Exception {
0N/A go(enc, str, bytes, true);
0N/A }
0N/A
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A go("US-ASCII", "abc", new byte[] { 'a', 'b', 'c' });
0N/A go("us-ascii", "abc", new byte[] { 'a', 'b', 'c' });
0N/A go("ISO646-US", "abc", new byte[] { 'a', 'b', 'c' });
0N/A go("ISO-8859-1", "ab\u00c7", new byte[] { 'a', 'b', (byte)'\u00c7' });
0N/A go("UTF-8", "ab\u1e09",
0N/A new byte[] { 'a', 'b',
0N/A (byte)(0xe0 | (0x0f & (0x1e09 >> 12))),
0N/A (byte)(0x80 | (0x3f & (0x1e09 >> 6))),
0N/A (byte)(0x80 | (0x3f & 0x1e09)) });
0N/A go("UTF-16BE", "ab\u1e09",
0N/A new byte[] { 0, 'a', 0, 'b', 0x1e, 0x09 });
0N/A go("UTF-16LE", "ab\u1e09",
0N/A new byte[] { 'a', 0, 'b', 0, 0x09, 0x1e });
0N/A
0N/A /* UTF-16 accepts both byte orders on input but always uses big-endian
0N/A * on output, so test all three cases
0N/A */
0N/A go("UTF-16", "ab\u1e09",
0N/A new byte[] { (byte)0xfe, (byte)0xff, 0, 'a', 0, 'b', 0x1e, 0x09 });
0N/A go("UTF-16", "ab\u1e09",
0N/A new byte[] { (byte)0xff, (byte)0xfe, 'a', 0, 'b', 0, 0x09, 0x1e },
0N/A false);
0N/A }
0N/A
0N/A}