jdk/test/sun/nio/cs/TestStringCoding.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 5970 d4e98bbfb0be
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
     2
 * Copyright (c) 2000, 2009, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    10
 *
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    11
 * 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
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    14
 * 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
    15
 * accompanied this code).
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    16
 *
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
    23
 * questions.
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    24
 */
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    25
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    26
/* @test
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    27
   @bug 6636323 6636319
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    28
   @summary Test if StringCoding and NIO result have the same de/encoding result
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    29
 * @run main/timeout=2000 TestStringCoding
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
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    39
        for (Boolean hasSM: new boolean[] { false, true }) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    40
            if (hasSM)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    41
                System.setSecurityManager(new PermissiveSecurityManger());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    42
            for (Charset cs:  Charset.availableCharsets().values()) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    43
                if ("ISO-2022-CN".equals(cs.name()) ||
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    44
                    "x-COMPOUND_TEXT".equals(cs.name()) ||
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    45
                    "x-JISAutoDetect".equals(cs.name()))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    46
                    continue;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    47
                System.out.printf("Testing(sm=%b) " + cs.name() + "....", hasSM);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    48
                // full bmp first
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    49
                char[] bmpCA = new char[0x10000];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    50
                for (int i = 0; i < 0x10000; i++) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    51
                     bmpCA[i] = (char)i;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    52
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    53
                byte[] sbBA = new byte[0x100];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    54
                for (int i = 0; i < 0x100; i++) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    55
                    sbBA[i] = (byte)i;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    56
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    57
                test(cs, bmpCA, sbBA);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    58
                // "randomed" sizes
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    59
                Random rnd = new Random();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    60
                for (int i = 0; i < 10; i++) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    61
                    int clen = rnd.nextInt(0x10000);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    62
                    int blen = rnd.nextInt(0x100);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    63
                    //System.out.printf("    blen=%d, clen=%d%n", blen, clen);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    64
                    test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen));
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    65
                    //add a pair of surrogates
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    66
                    int pos = clen / 2;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    67
                    if ((pos + 1) < blen) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    68
                        bmpCA[pos] = '\uD800';
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    69
                        bmpCA[pos+1] = '\uDC00';
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    70
                    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    71
                    test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen));
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    72
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    73
                System.out.println("done!");
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    74
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    75
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    76
    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    77
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    78
    static void test(Charset cs, char[] bmpCA, byte[] sbBA) throws Throwable {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    79
        String bmpStr = new String(bmpCA);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    80
        CharsetDecoder dec = cs.newDecoder()
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    81
            .onMalformedInput(CodingErrorAction.REPLACE)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    82
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    83
        CharsetEncoder enc = cs.newEncoder()
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    84
            .onMalformedInput(CodingErrorAction.REPLACE)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    85
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    86
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    87
        //getBytes(csn);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    88
        byte[] baSC = bmpStr.getBytes(cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    89
        ByteBuffer bf = enc.reset().encode(CharBuffer.wrap(bmpCA));
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    90
        byte[] baNIO = new byte[bf.limit()];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    91
        bf.get(baNIO, 0, baNIO.length);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    92
        if (!Arrays.equals(baSC, baNIO))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    93
            throw new RuntimeException("getBytes(csn) failed  -> " + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    94
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    95
        //getBytes(cs);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    96
        baSC = bmpStr.getBytes(cs);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    97
        if (!Arrays.equals(baSC, baNIO))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    98
            throw new RuntimeException("getBytes(cs) failed  -> " + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
    99
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   100
        //new String(csn);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   101
        String strSC = new String(sbBA, cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   102
        String strNIO = dec.reset().decode(ByteBuffer.wrap(sbBA)).toString();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   103
        if(!strNIO.equals(strSC))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   104
            throw new RuntimeException("new String(csn) failed  -> " + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   105
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   106
        //new String(cs);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   107
        strSC = new String(sbBA, cs);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   108
        if (!strNIO.equals(strSC))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   109
            throw new RuntimeException("new String(cs) failed  -> " + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   110
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   111
        //encode unmappable surrogates
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   112
        if (enc instanceof sun.nio.cs.ArrayEncoder &&
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   113
            cs.contains(Charset.forName("ASCII"))) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   114
            enc.replaceWith(new byte[] { (byte)'A'});
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   115
            sun.nio.cs.ArrayEncoder cae = (sun.nio.cs.ArrayEncoder)enc;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   116
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   117
            String str = "ab\uD800\uDC00\uD800\uDC00cd";
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   118
            byte[] ba = new byte[str.length() - 2];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   119
            int n = cae.encode(str.toCharArray(), 0, str.length(), ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   120
            if (n != 6 || !"abAAcd".equals(new String(ba, cs.name())))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   121
                throw new RuntimeException("encode1(surrogates) failed  -> "
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   122
                                           + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   123
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   124
            ba = new byte[str.length()];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   125
            n = cae.encode(str.toCharArray(), 0, str.length(), ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   126
            if (n != 6 || !"abAAcd".equals(new String(ba, 0, n,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   127
                                                     cs.name())))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   128
                throw new RuntimeException("encode2(surrogates) failed  -> "
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   129
                                           + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   130
            str = "ab\uD800B\uDC00Bcd";
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   131
            ba = new byte[str.length()];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   132
            n = cae.encode(str.toCharArray(), 0, str.length(), ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   133
            if (n != 8 || !"abABABcd".equals(new String(ba, 0, n,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   134
                                                       cs.name())))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   135
                throw new RuntimeException("encode3(surrogates) failed  -> "
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   136
                                           + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   137
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   138
            ba = new byte[str.length() - 1];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   139
            n = cae.encode(str.toCharArray(), 0, str.length(), ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   140
            if (n != 7 || !"abABABc".equals(new String(ba, 0, n,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   141
                                                      cs.name())))
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   142
                throw new RuntimeException("encode4(surrogates) failed  -> "
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   143
                                           + cs.name());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   144
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   145
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   146
    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   147
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   148
    static class PermissiveSecurityManger extends SecurityManager {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   149
        @Override public void checkPermission(java.security.Permission p) {}
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   150
    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents:
diff changeset
   151
}