Path: blob/master/test/jdk/java/lang/String/Encodings.java
41152 views
/*1* Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4085160 4139951 500583125* @summary Test that required character encodings are supported26*/2728import java.io.InputStreamReader;29import java.io.OutputStreamWriter;30import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.nio.charset.Charset;33import java.nio.charset.UnsupportedCharsetException;343536public class Encodings {373839static boolean equals(byte[] a, byte[] b) {40if (a.length != b.length) return false;41for (int i = 0; i < a.length; i++)42if (a[i] != b[i]) return false;43return true;44}454647static void go(String enc, String str, final byte[] bytes, boolean bidir)48throws Exception49{50final Charset charset = Charset.forName(enc);5152/* String(byte[] bs, String enc) */53if (!(new String(bytes, enc).equals(str)))54throw new Exception(enc + ": String constructor failed");5556/* String(byte[] bs, Charset charset) */57if (!(new String(bytes, charset).equals(str)))58throw new Exception(charset + ": String constructor failed");5960/* String(byte[] bs, int off, int len, Charset charset) */61String start = str.substring(0, 2);62String end = str.substring(2);63if (enc.equals("UTF-16BE") || enc.equals("UTF-16LE")) {64if (!(new String(bytes, 0, 4, charset).equals(start)))65throw new Exception(charset + ": String constructor failed");66if (!(new String(bytes, 4, bytes.length - 4, charset).equals(end)))67throw new Exception(charset + ": String constructor failed");68} else if (enc.equals("UTF-16")) {69if (!(new String(bytes, 0, 6, charset).equals(start)))70throw new Exception(charset + ": String constructor failed");71} else {72if (!(new String(bytes, 0, 2, charset).equals(start)))73throw new Exception(charset + ": String constructor failed");74if (!(new String(bytes, 2, bytes.length - 2, charset).equals(end)))75throw new Exception(charset + ": String constructor failed");76}7778/* InputStreamReader */79ByteArrayInputStream bi = new ByteArrayInputStream(bytes);80InputStreamReader r = new InputStreamReader(bi, enc);81String inEnc = r.getEncoding();82int n = str.length();83char[] cs = new char[n];84for (int i = 0; i < n;) {85int m;86if ((m = r.read(cs, i, n - i)) < 0)87throw new Exception(enc + ": EOF on InputStreamReader");88i += m;89}90if (!(new String(cs).equals(str)))91throw new Exception(enc + ": InputStreamReader failed");9293if (!bidir) {94System.err.println(enc + " --> " + inEnc);95return;96}9798/* String.getBytes(String enc) */99byte[] bs = str.getBytes(enc);100if (!equals(bs, bytes))101throw new Exception(enc + ": String.getBytes failed");102103/* String.getBytes(Charset charset) */104bs = str.getBytes(charset);105if (!equals(bs, bytes))106throw new Exception(charset + ": String.getBytes failed");107108// Calls to String.getBytes(Charset) shouldn't automatically109// use the cached thread-local encoder.110if (charset.name().equals("UTF-16BE")) {111String s = new String(bytes, charset);112// Replace the thread-local encoder with this one.113byte[] bb = s.getBytes(Charset.forName("UTF-16LE"));114if (bytes.length != bb.length) {115// Incidental test.116throw new RuntimeException("unequal length: "117+ bytes.length + " != "118+ bb.length);119} else {120boolean diff = false;121// Expect different byte[] between UTF-16LE and UTF-16BE122// even though encoder was previously cached by last call123// to getBytes().124for (int i = 0; i < bytes.length; i++) {125if (bytes[i] != bb[i])126diff = true;127}128if (!diff)129throw new RuntimeException("byte arrays equal");130}131}132133/* OutputStreamWriter */134ByteArrayOutputStream bo = new ByteArrayOutputStream();135OutputStreamWriter w = new OutputStreamWriter(bo, enc);136String outEnc = w.getEncoding();137w.write(str);138w.close();139bs = bo.toByteArray();140if (!equals(bs, bytes))141throw new Exception(enc + ": OutputStreamWriter failed");142143System.err.println(enc + " --> " + inEnc + " / " + outEnc);144}145146147static void go(String enc, String str, byte[] bytes) throws Exception {148go(enc, str, bytes, true);149}150151152public static void main(String[] args) throws Exception {153154go("US-ASCII", "abc", new byte[] { 'a', 'b', 'c' });155go("us-ascii", "abc", new byte[] { 'a', 'b', 'c' });156go("ISO646-US", "abc", new byte[] { 'a', 'b', 'c' });157go("ISO-8859-1", "ab\u00c7", new byte[] { 'a', 'b', (byte)'\u00c7' });158go("UTF-8", "ab\u1e09",159new byte[] { 'a', 'b',160(byte)(0xe0 | (0x0f & (0x1e09 >> 12))),161(byte)(0x80 | (0x3f & (0x1e09 >> 6))),162(byte)(0x80 | (0x3f & 0x1e09)) });163go("UTF-16BE", "ab\u1e09",164new byte[] { 0, 'a', 0, 'b', 0x1e, 0x09 });165go("UTF-16LE", "ab\u1e09",166new byte[] { 'a', 0, 'b', 0, 0x09, 0x1e });167168/* UTF-16 accepts both byte orders on input but always uses big-endian169* on output, so test all three cases170*/171go("UTF-16", "ab\u1e09",172new byte[] { (byte)0xfe, (byte)0xff, 0, 'a', 0, 'b', 0x1e, 0x09 });173go("UTF-16", "ab\u1e09",174new byte[] { (byte)0xff, (byte)0xfe, 'a', 0, 'b', 0, 0x09, 0x1e },175false);176}177178}179180181