964N/A/*
3261N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
964N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
964N/A *
964N/A * This code is free software; you can redistribute it and/or modify it
964N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
964N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
964N/A *
964N/A * This code is distributed in the hope that it will be useful, but WITHOUT
964N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
964N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
964N/A * version 2 for more details (a copy is included in the LICENSE file that
964N/A * accompanied this code).
964N/A *
964N/A * You should have received a copy of the GNU General Public License version
964N/A * 2 along with this work; if not, write to the Free Software Foundation,
964N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
964N/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.
964N/A */
964N/A
964N/A/* @test
4150N/A @bug 6636323 6636319 7040220
964N/A @summary Test if StringCoding and NIO result have the same de/encoding result
2546N/A * @run main/othervm/timeout=2000 TestStringCoding
964N/A */
964N/A
964N/Aimport java.util.*;
964N/Aimport java.nio.*;
964N/Aimport java.nio.charset.*;
964N/A
964N/Apublic class TestStringCoding {
964N/A public static void main(String[] args) throws Throwable {
964N/A
964N/A for (Boolean hasSM: new boolean[] { false, true }) {
964N/A if (hasSM)
964N/A System.setSecurityManager(new PermissiveSecurityManger());
964N/A for (Charset cs: Charset.availableCharsets().values()) {
964N/A if ("ISO-2022-CN".equals(cs.name()) ||
964N/A "x-COMPOUND_TEXT".equals(cs.name()) ||
964N/A "x-JISAutoDetect".equals(cs.name()))
964N/A continue;
964N/A System.out.printf("Testing(sm=%b) " + cs.name() + "....", hasSM);
964N/A // full bmp first
964N/A char[] bmpCA = new char[0x10000];
964N/A for (int i = 0; i < 0x10000; i++) {
964N/A bmpCA[i] = (char)i;
964N/A }
964N/A byte[] sbBA = new byte[0x100];
964N/A for (int i = 0; i < 0x100; i++) {
964N/A sbBA[i] = (byte)i;
964N/A }
964N/A test(cs, bmpCA, sbBA);
964N/A // "randomed" sizes
964N/A Random rnd = new Random();
964N/A for (int i = 0; i < 10; i++) {
964N/A int clen = rnd.nextInt(0x10000);
964N/A int blen = rnd.nextInt(0x100);
964N/A //System.out.printf(" blen=%d, clen=%d%n", blen, clen);
964N/A test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen));
964N/A //add a pair of surrogates
964N/A int pos = clen / 2;
964N/A if ((pos + 1) < blen) {
964N/A bmpCA[pos] = '\uD800';
964N/A bmpCA[pos+1] = '\uDC00';
964N/A }
964N/A test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen));
964N/A }
964N/A System.out.println("done!");
964N/A }
964N/A }
964N/A }
964N/A
964N/A static void test(Charset cs, char[] bmpCA, byte[] sbBA) throws Throwable {
964N/A String bmpStr = new String(bmpCA);
964N/A CharsetDecoder dec = cs.newDecoder()
964N/A .onMalformedInput(CodingErrorAction.REPLACE)
964N/A .onUnmappableCharacter(CodingErrorAction.REPLACE);
964N/A CharsetEncoder enc = cs.newEncoder()
964N/A .onMalformedInput(CodingErrorAction.REPLACE)
964N/A .onUnmappableCharacter(CodingErrorAction.REPLACE);
964N/A
964N/A //getBytes(csn);
964N/A byte[] baSC = bmpStr.getBytes(cs.name());
964N/A ByteBuffer bf = enc.reset().encode(CharBuffer.wrap(bmpCA));
964N/A byte[] baNIO = new byte[bf.limit()];
964N/A bf.get(baNIO, 0, baNIO.length);
964N/A if (!Arrays.equals(baSC, baNIO))
964N/A throw new RuntimeException("getBytes(csn) failed -> " + cs.name());
964N/A
964N/A //getBytes(cs);
964N/A baSC = bmpStr.getBytes(cs);
964N/A if (!Arrays.equals(baSC, baNIO))
964N/A throw new RuntimeException("getBytes(cs) failed -> " + cs.name());
964N/A
964N/A //new String(csn);
964N/A String strSC = new String(sbBA, cs.name());
964N/A String strNIO = dec.reset().decode(ByteBuffer.wrap(sbBA)).toString();
964N/A if(!strNIO.equals(strSC))
964N/A throw new RuntimeException("new String(csn) failed -> " + cs.name());
964N/A
964N/A //new String(cs);
964N/A strSC = new String(sbBA, cs);
964N/A if (!strNIO.equals(strSC))
964N/A throw new RuntimeException("new String(cs) failed -> " + cs.name());
964N/A
964N/A //encode unmappable surrogates
964N/A if (enc instanceof sun.nio.cs.ArrayEncoder &&
964N/A cs.contains(Charset.forName("ASCII"))) {
4150N/A if (cs.name().equals("UTF-8")) // utf8 handles surrogates
4150N/A return;
964N/A enc.replaceWith(new byte[] { (byte)'A'});
964N/A sun.nio.cs.ArrayEncoder cae = (sun.nio.cs.ArrayEncoder)enc;
964N/A
964N/A String str = "ab\uD800\uDC00\uD800\uDC00cd";
964N/A byte[] ba = new byte[str.length() - 2];
964N/A int n = cae.encode(str.toCharArray(), 0, str.length(), ba);
964N/A if (n != 6 || !"abAAcd".equals(new String(ba, cs.name())))
964N/A throw new RuntimeException("encode1(surrogates) failed -> "
964N/A + cs.name());
964N/A
964N/A ba = new byte[str.length()];
964N/A n = cae.encode(str.toCharArray(), 0, str.length(), ba);
964N/A if (n != 6 || !"abAAcd".equals(new String(ba, 0, n,
964N/A cs.name())))
964N/A throw new RuntimeException("encode2(surrogates) failed -> "
964N/A + cs.name());
964N/A str = "ab\uD800B\uDC00Bcd";
964N/A ba = new byte[str.length()];
964N/A n = cae.encode(str.toCharArray(), 0, str.length(), ba);
964N/A if (n != 8 || !"abABABcd".equals(new String(ba, 0, n,
964N/A cs.name())))
964N/A throw new RuntimeException("encode3(surrogates) failed -> "
964N/A + cs.name());
964N/A
964N/A ba = new byte[str.length() - 1];
964N/A n = cae.encode(str.toCharArray(), 0, str.length(), ba);
964N/A if (n != 7 || !"abABABc".equals(new String(ba, 0, n,
964N/A cs.name())))
964N/A throw new RuntimeException("encode4(surrogates) failed -> "
964N/A + cs.name());
964N/A }
964N/A
964N/A }
964N/A
964N/A static class PermissiveSecurityManger extends SecurityManager {
964N/A @Override public void checkPermission(java.security.Permission p) {}
964N/A }
964N/A}