test/jdk/sun/nio/cs/TestStringCoding.java
author jiangli
Wed, 27 Sep 2017 17:55:20 -0400
changeset 47548 664b9d44db74
parent 47216 71c04702a3d5
permissions -rw-r--r--
8068314: "Java fields that are currently set during shared space dumping" comment is incorrect Summary: CDS dump time should also initialize preallocated out_of_memory error messages. Reviewed-by: iklam, hseigel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21596
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
     4
 *
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 14342
diff changeset
     7
 * published by the Free Software Foundation.
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
     8
 *
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    13
 * accompanied this code).
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    14
 *
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
    21
 * questions.
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    22
 */
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    23
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    24
/* @test
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    25
 * @bug 6636323 6636319 7040220 7096080 7183053 8080248 8054307
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 30691
diff changeset
    26
 * @summary Test if StringCoding and NIO result have the same de/encoding result
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 30691
diff changeset
    27
 * @modules java.base/sun.nio.cs
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    28
 * @run main/othervm/timeout=2000 TestStringCoding
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 23010
diff changeset
    29
 * @key randomness
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    30
 */
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    31
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    32
import java.util.*;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    33
import java.nio.*;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    34
import java.nio.charset.*;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    35
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    36
public class TestStringCoding {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    37
    public static void main(String[] args) throws Throwable {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    38
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    39
        // full bmp first
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    40
        char[] bmp = new char[0x10000];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    41
        for (int i = 0; i < 0x10000; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    42
            bmp[i] = (char)i;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    43
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    44
        char[] latin = Arrays.copyOf(bmp, 0x100);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    45
        char[] ascii =  Arrays.copyOf(bmp, 0x80);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    46
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    47
        byte[] latinBA = new byte[0x100];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    48
        for (int i = 0; i < 0x100; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    49
            latinBA[i] = (byte)i;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    50
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    51
        byte[] asciiBA =  Arrays.copyOf(latinBA, 0x80);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    52
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    53
        for (Boolean hasSM: new boolean[] { false, true }) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    54
            if (hasSM) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    55
                System.setSecurityManager(new PermissiveSecurityManger());
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    56
            }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    57
            for (Charset cs:  Charset.availableCharsets().values()) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    58
                if ("ISO-2022-CN".equals(cs.name()) ||
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    59
                    "x-COMPOUND_TEXT".equals(cs.name()) ||
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    60
                    "x-JISAutoDetect".equals(cs.name()))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    61
                    continue;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    62
                System.out.printf("Testing(sm=%b) " + cs.name() + "....", hasSM);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    63
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    64
                testNewString(cs, testGetBytes(cs, new String(bmp)));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    65
                testNewString(cs, testGetBytes(cs, new String(latin)));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    66
                testNewString(cs, testGetBytes(cs, new String(ascii)));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    67
                testGetBytes(cs, testNewString(cs, latinBA));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    68
                testGetBytes(cs, testNewString(cs, asciiBA));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    69
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    70
                // "randomed" sizes
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    71
                Random rnd = new Random();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    72
                for (int i = 0; i < 10; i++) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    73
                    //System.out.printf("    blen=%d, clen=%d%n", blen, clen);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    74
                    char[] bmp0 = Arrays.copyOf(bmp, rnd.nextInt(0x10000));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    75
                    testNewString(cs, testGetBytes(cs, new String(bmp0)));
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    76
                    //add a pair of surrogates
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    77
                    int pos = bmp0.length / 2;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    78
                    if ((pos + 1) < bmp0.length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    79
                        bmp0[pos] = '\uD800';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    80
                        bmp0[pos+1] = '\uDC00';
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    81
                    }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    82
                    testNewString(cs, testGetBytes(cs, new String(bmp0)));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    83
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    84
                    char[] latin0 = Arrays.copyOf(latin, rnd.nextInt(0x100));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    85
                    char[] ascii0 = Arrays.copyOf(ascii, rnd.nextInt(0x80));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    86
                    byte[] latinBA0 = Arrays.copyOf(latinBA, rnd.nextInt(0x100));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    87
                    byte[] asciiBA0 = Arrays.copyOf(asciiBA, rnd.nextInt(0x80));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    88
                    testNewString(cs, testGetBytes(cs, new String(latin0)));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    89
                    testNewString(cs, testGetBytes(cs, new String(ascii0)));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    90
                    testGetBytes(cs, testNewString(cs, latinBA0));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    91
                    testGetBytes(cs, testNewString(cs, asciiBA0));
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    92
                }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
    93
                testSurrogates(cs);
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
    94
                testMixed(cs);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    95
                System.out.println("done!");
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    96
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    97
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    98
    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    99
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   100
    static void testMixed(Charset cs) throws Throwable {
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   101
        CharsetDecoder dec = cs.newDecoder()
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   102
            .onMalformedInput(CodingErrorAction.REPLACE)
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   103
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   104
        CharsetEncoder enc = cs.newEncoder()
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   105
            .onMalformedInput(CodingErrorAction.REPLACE)
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   106
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   107
        List<Integer> cps = new ArrayList<>(0x10000);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   108
        int off = 0;
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   109
        int cp = 0;
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   110
        while (cp < 0x10000) {
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   111
            if (enc.canEncode((char)cp)) {
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   112
               cps.add(cp);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   113
            }
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   114
            cp++;
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   115
        }
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   116
        Collections.shuffle(cps);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   117
        char[] bmpCA = new char[cps.size()];
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   118
        for (int i = 0; i < cps.size(); i++)
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   119
            bmpCA[i] = (char)(int)cps.get(i);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   120
        String bmpStr = new String(bmpCA);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   121
        //getBytes(csn);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   122
        byte[] bmpBA = bmpStr.getBytes(cs.name());
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   123
        ByteBuffer bf = enc.reset().encode(CharBuffer.wrap(bmpCA));
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   124
        byte[] baNIO = new byte[bf.limit()];
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   125
        bf.get(baNIO, 0, baNIO.length);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   126
        if (!Arrays.equals(bmpBA, baNIO)) {
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   127
            throw new RuntimeException("getBytes(csn) failed  -> " + cs.name());
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   128
        }
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   129
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   130
        //getBytes(cs);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   131
        bmpBA = bmpStr.getBytes(cs);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   132
        if (!Arrays.equals(bmpBA, baNIO)) {
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   133
            throw new RuntimeException("getBytes(cs) failed  -> " + cs.name());
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   134
        }
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   135
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   136
        //new String(csn);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   137
        String strSC = new String(bmpBA, cs.name());
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   138
        String strNIO = dec.reset().decode(ByteBuffer.wrap(bmpBA)).toString();
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   139
        if(!strNIO.equals(strSC)) {
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   140
            throw new RuntimeException("new String(csn) failed  -> " + cs.name());
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   141
        }
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   142
        //new String(cs);
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   143
        strSC = new String(bmpBA, cs);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   144
        if (!strNIO.equals(strSC)) {
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   145
            throw new RuntimeException("new String(cs) failed  -> " + cs.name());
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   146
        }
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   147
    }
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   148
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   149
    static byte[] getBytes(CharsetEncoder enc, String str) throws Throwable {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   150
        ByteBuffer bf = enc.reset().encode(CharBuffer.wrap(str.toCharArray()));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   151
        byte[] ba = new byte[bf.limit()];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   152
        bf.get(ba, 0, ba.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   153
        return ba;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   154
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   155
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   156
    static byte[] testGetBytes(Charset cs, String str) throws Throwable {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   157
        CharsetEncoder enc = cs.newEncoder()
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   158
            .onMalformedInput(CodingErrorAction.REPLACE)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   159
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   160
        //getBytes(csn);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   161
        byte[] baSC = str.getBytes(cs.name());
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   162
        byte[] baNIO = getBytes(enc, str);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   163
        if (!Arrays.equals(baSC, baNIO)) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   164
            throw new RuntimeException("getBytes(csn) failed  -> " + cs.name());
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   165
        }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   166
        //getBytes(cs);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   167
        baSC = str.getBytes(cs);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   168
        if (!Arrays.equals(baSC, baNIO)) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   169
            throw new RuntimeException("getBytes(cs) failed  -> " + cs.name());
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   170
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   171
        return baSC;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   172
    }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   173
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   174
    static String testNewString(Charset cs, byte[] ba) throws Throwable {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   175
        CharsetDecoder dec = cs.newDecoder()
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   176
            .onMalformedInput(CodingErrorAction.REPLACE)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   177
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   178
        //new String(csn);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   179
        String strSC = new String(ba, cs.name());
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   180
        String strNIO = dec.reset().decode(ByteBuffer.wrap(ba)).toString();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   181
        if(!strNIO.equals(strSC)) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   182
            throw new RuntimeException("new String(csn) failed  -> " + cs.name());
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   183
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   184
        //new String(cs);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   185
        strSC = new String(ba, cs);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   186
        if (!strNIO.equals(strSC)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   187
            throw new RuntimeException("new String(cs)/bmp failed  -> " + cs.name());
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   188
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   189
        return strSC;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   190
    }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   191
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   192
    static void testSurrogates(Charset cs) throws Throwable {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   193
        //encode unmappable surrogates
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   194
        CharsetEncoder enc = cs.newEncoder()
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   195
            .onMalformedInput(CodingErrorAction.REPLACE)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 30820
diff changeset
   196
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   197
        if (enc instanceof sun.nio.cs.ArrayEncoder &&
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   198
            cs.contains(Charset.forName("ASCII"))) {
10898
01a232fa2ddb 7096080: UTF8 update and new CESU-8 charset
sherman
parents: 9547
diff changeset
   199
            if (cs.name().equals("UTF-8") ||     // utf8 handles surrogates
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   200
                cs.name().equals("CESU-8"))      // utf8 handles surrogates
9547
454881baaca0 7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents: 7668
diff changeset
   201
                return;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   202
            enc.replaceWith(new byte[] { (byte)'A'});
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   203
            sun.nio.cs.ArrayEncoder cae = (sun.nio.cs.ArrayEncoder)enc;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   204
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   205
            String str = "ab\uD800\uDC00\uD800\uDC00cd";
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   206
            byte[] ba = new byte[str.length() - 2];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   207
            int n = cae.encode(str.toCharArray(), 0, str.length(), ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   208
            if (n != 6 || !"abAAcd".equals(new String(ba, cs.name())))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   209
                throw new RuntimeException("encode1(surrogates) failed  -> "
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   210
                                           + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   211
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   212
            ba = new byte[str.length()];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   213
            n = cae.encode(str.toCharArray(), 0, str.length(), ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   214
            if (n != 6 || !"abAAcd".equals(new String(ba, 0, n,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   215
                                                     cs.name())))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   216
                throw new RuntimeException("encode2(surrogates) failed  -> "
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   217
                                           + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   218
            str = "ab\uD800B\uDC00Bcd";
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   219
            ba = new byte[str.length()];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   220
            n = cae.encode(str.toCharArray(), 0, str.length(), ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   221
            if (n != 8 || !"abABABcd".equals(new String(ba, 0, n,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   222
                                                       cs.name())))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   223
                throw new RuntimeException("encode3(surrogates) failed  -> "
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   224
                                           + cs.name());
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   225
            /* sun.nio.cs.ArrayDeEncoder works on the assumption that the
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   226
               invoker (StringCoder) allocates enough output buf, utf8
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   227
               and double-byte coder does not check the output buffer limit.
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   228
            ba = new byte[str.length() - 1];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   229
            n = cae.encode(str.toCharArray(), 0, str.length(), ba);
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   230
            if (n != 7 || !"abABABc".equals(new String(ba, 0, n, cs.name()))) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   231
                throw new RuntimeException("encode4(surrogates) failed  -> "
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   232
                                           + cs.name());
13257
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   233
            }
5cf4ff2cbb37 7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
sherman
parents: 10898
diff changeset
   234
            */
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   235
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   236
30691
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   237
        //encode mappable surrogates for hkscs
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   238
        if (cs.name().equals("Big5-HKSCS") || cs.name().equals("x-MS950-HKSCS")) {
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   239
            String str = "ab\uD840\uDD0Ccd";
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   240
            byte[] expected = new byte[] {(byte)'a', (byte)'b',
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   241
                (byte)0x88, (byte)0x45, (byte)'c', (byte)'d' };
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   242
            if (!Arrays.equals(str.getBytes(cs.name()), expected) ||
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   243
                !Arrays.equals(str.getBytes(cs), expected)) {
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   244
                throw new RuntimeException("encode(surrogates) failed  -> "
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   245
                                           + cs.name());
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   246
            }
8b8579faebd4 8080248: Coding regression in HKSCS charsets
sherman
parents: 30046
diff changeset
   247
        }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   248
    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   249
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   250
    static class PermissiveSecurityManger extends SecurityManager {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   251
        @Override public void checkPermission(java.security.Permission p) {}
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   252
    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   253
}