test/jdk/sun/nio/cs/OLD/TestIBMDB.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200 (2017-09-12)
changeset 47216 71c04702a3d5
parent 30820 jdk/test/sun/nio/cs/OLD/TestIBMDB.java@0d4717a011d3
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13366
diff changeset
     2
 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
     4
 *
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
     8
 *
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    13
 * accompanied this code).
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    14
 *
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
    21
 * questions.
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    22
 */
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    23
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    24
/*
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    25
 * @test
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    26
 * @bug 6843578
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
    27
 * @summary Test old and new implementation of db charsets
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
    28
 * @build IBM930_OLD IBM933_OLD IBM935_OLD IBM937_OLD IBM939_OLD IBM942_OLD IBM943_OLD IBM948_OLD IBM949_OLD IBM950_OLD IBM970_OLD IBM942C_OLD IBM943C_OLD IBM949C_OLD IBM1381_OLD IBM1383_OLD EUC_CN_OLD EUC_KR_OLD GBK_OLD Johab_OLD MS932_OLD MS936_OLD MS949_OLD MS950_OLD SJIS_OLD PCK_OLD EUC_JP_OLD EUC_JP_LINUX_OLD EUC_JP_Open_OLD
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 14342
diff changeset
    29
 * @modules java.base/sun.nio.cs jdk.charsets/sun.nio.cs.ext
12180
90b9045428bd 7152866: Tests not run because they are missing the @run tag
alanb
parents: 5506
diff changeset
    30
 * @run main TestIBMDB
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    31
 */
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    32
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    33
import java.nio.charset.*;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    34
import java.nio.*;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    35
import java.util.*;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    36
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    37
public class TestIBMDB {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    38
    static class Time {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    39
        long t;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    40
    }
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
    41
    static int iteration = 200;
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    42
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    43
    static char[] decode(byte[] bb, Charset cs, boolean testDirect, Time t)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    44
        throws Exception {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    45
        String csn = cs.name();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    46
        CharsetDecoder dec = cs.newDecoder();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    47
        ByteBuffer bbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    48
        CharBuffer cbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    49
        if (testDirect) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    50
            bbf = ByteBuffer.allocateDirect(bb.length);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    51
            cbf = ByteBuffer.allocateDirect(bb.length*2).asCharBuffer();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    52
            bbf.put(bb);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    53
        } else {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    54
            bbf = ByteBuffer.wrap(bb);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    55
            cbf = CharBuffer.allocate(bb.length);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    56
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    57
        CoderResult cr = null;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    58
        long t1 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    59
        for (int i = 0; i < iteration; i++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    60
            bbf.rewind();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    61
            cbf.clear();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    62
            dec.reset();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    63
            cr = dec.decode(bbf, cbf, true);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    64
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    65
        long t2 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    66
        t.t = (t2 - t1)/iteration;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    67
        if (cr != CoderResult.UNDERFLOW) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    68
            System.out.println("DEC-----------------");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    69
            int pos = bbf.position();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    70
            System.out.printf("  cr=%s, bbf.pos=%d, bb[pos]=%x,%x,%x,%x%n",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    71
                              cr.toString(), pos,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    72
                              bb[pos++]&0xff, bb[pos++]&0xff,bb[pos++]&0xff, bb[pos++]&0xff);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    73
            throw new RuntimeException("Decoding err: " + csn);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    74
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    75
        char[] cc = new char[cbf.position()];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    76
        cbf.flip(); cbf.get(cc);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    77
        return cc;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    78
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    79
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    80
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    81
    static CoderResult decodeCR(byte[] bb, Charset cs, boolean testDirect)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    82
        throws Exception {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    83
        CharsetDecoder dec = cs.newDecoder();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    84
        ByteBuffer bbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    85
        CharBuffer cbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    86
        if (testDirect) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    87
            bbf = ByteBuffer.allocateDirect(bb.length);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    88
            cbf = ByteBuffer.allocateDirect(bb.length*2).asCharBuffer();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    89
            bbf.put(bb).flip();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    90
        } else {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    91
            bbf = ByteBuffer.wrap(bb);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    92
            cbf = CharBuffer.allocate(bb.length);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    93
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    94
        CoderResult cr = null;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    95
        for (int i = 0; i < iteration; i++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    96
            bbf.rewind();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    97
            cbf.clear();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    98
            dec.reset();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
    99
            cr = dec.decode(bbf, cbf, true);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   100
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   101
        return cr;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   102
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   103
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   104
    static byte[] encode(char[] cc, Charset cs, boolean testDirect, Time t)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   105
        throws Exception {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   106
        ByteBuffer bbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   107
        CharBuffer cbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   108
        CharsetEncoder enc = cs.newEncoder();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   109
        String csn = cs.name();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   110
        if (testDirect) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   111
            bbf = ByteBuffer.allocateDirect(cc.length * 4);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   112
            cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   113
            cbf.put(cc).flip();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   114
        } else {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   115
            bbf = ByteBuffer.allocate(cc.length * 4);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   116
            cbf = CharBuffer.wrap(cc);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   117
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   118
        CoderResult cr = null;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   119
        long t1 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   120
        for (int i = 0; i < iteration; i++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   121
            cbf.rewind();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   122
            bbf.clear();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   123
            enc.reset();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   124
            cr = enc.encode(cbf, bbf, true);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   125
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   126
        long t2 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   127
        t.t = (t2 - t1)/iteration;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   128
        if (cr != CoderResult.UNDERFLOW) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   129
            System.out.println("ENC-----------------");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   130
            int pos = cbf.position();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   131
            System.out.printf("  cr=%s, cbf.pos=%d, cc[pos]=%x%n",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   132
                              cr.toString(), pos, cc[pos]&0xffff);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   133
            throw new RuntimeException("Encoding err: " + csn);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   134
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   135
        byte[] bb = new byte[bbf.position()];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   136
        bbf.flip(); bbf.get(bb);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   137
        return bb;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   138
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   139
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   140
    static CoderResult encodeCR(char[] cc, Charset cs, boolean testDirect)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   141
        throws Exception {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   142
        ByteBuffer bbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   143
        CharBuffer cbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   144
        CharsetEncoder enc = cs.newEncoder();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   145
        if (testDirect) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   146
            bbf = ByteBuffer.allocateDirect(cc.length * 4);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   147
            cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   148
            cbf.put(cc).flip();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   149
        } else {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   150
            bbf = ByteBuffer.allocate(cc.length * 4);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   151
            cbf = CharBuffer.wrap(cc);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   152
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   153
        CoderResult cr = null;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   154
        for (int i = 0; i < iteration; i++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   155
            cbf.rewind();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   156
            bbf.clear();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   157
            enc.reset();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   158
            cr = enc.encode(cbf, bbf, true);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   159
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   160
        return cr;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   161
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   162
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   163
    static void printEntry(char c, Charset cs) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   164
        byte[] bb = new String(new char[] {c}).getBytes(cs);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   165
        for (byte b:bb)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   166
            System.out.printf("%x", b&0xff);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   167
        System.out.printf("    %x", c & 0xffff);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   168
        String s2 = new String(bb, cs);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   169
        System.out.printf("    %x%n", s2.charAt(0) & 0xffff);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   170
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   171
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   172
    // check and compare canEncoding/Encoding
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   173
    static char[] checkEncoding(Charset oldCS, Charset newCS)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   174
        throws Exception {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   175
        System.out.printf("Encoding <%s> <%s>...%n", oldCS.name(), newCS.name());
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   176
        CharsetEncoder encOLD = oldCS.newEncoder();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   177
        CharsetEncoder encNew = newCS.newEncoder();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   178
        char[] cc = new char[0x10000];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   179
        int pos = 0;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   180
        boolean is970 = "x-IBM970-Old".equals(oldCS.name());
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   181
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   182
        for (char c = 0; c < 0xffff; c++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   183
            boolean canOld = encOLD.canEncode(c);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   184
            boolean canNew = encNew.canEncode(c);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   185
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   186
            if (is970 && c == 0x2299)
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   187
                continue;
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   188
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   189
            if (canOld != canNew) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   190
                if (canNew) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   191
                    System.out.printf("      NEW(only): ");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   192
                    printEntry(c, newCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   193
                } else {
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   194
                    if (is970) {
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   195
                        byte[] bb = new String(new char[] {c}).getBytes(oldCS);
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   196
                        if (bb.length == 2 && bb[0] == (byte)0xa2 && bb[1] == (byte)0xc1) {
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   197
                        // we know 970 has bogus nnnn -> a2c1 -> 2299
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   198
                            continue;
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   199
                        }
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   200
                    }
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   201
                    System.out.printf("      OLD(only): ");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   202
                    printEntry(c, oldCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   203
                }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   204
            } else if (canNew) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   205
                byte[] bbNew = new String(new char[] {c}).getBytes(newCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   206
                byte[] bbOld = new String(new char[] {c}).getBytes(oldCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   207
                if (!Arrays.equals(bbNew, bbOld)) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   208
                    System.out.printf("      c->b NEW: ");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   209
                    printEntry(c, newCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   210
                    System.out.printf("      c->b OLD: ");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   211
                    printEntry(c, oldCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   212
                } else {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   213
                    String sNew = new String(bbNew, newCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   214
                    String sOld = new String(bbOld, oldCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   215
                    if (!sNew.equals(sOld)) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   216
                        System.out.printf("      b2c NEW (c=%x):", c&0xffff);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   217
                        printEntry(sNew.charAt(0), newCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   218
                        System.out.printf("      b2c OLD:");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   219
                        printEntry(sOld.charAt(0), oldCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   220
                    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   221
                }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   222
            }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   223
            if (canNew & canOld) {  // added only both for now
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   224
                cc[pos++] = c;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   225
            }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   226
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   227
        return Arrays.copyOf(cc, pos);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   228
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   229
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   230
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   231
    // check and compare canEncoding/Encoding
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   232
    static void checkDecoding(Charset oldCS, Charset newCS)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   233
        throws Exception
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   234
    {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   235
        System.out.printf("Decoding <%s> <%s>...%n", oldCS.name(), newCS.name());
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   236
        boolean isEBCDIC = oldCS.name().startsWith("x-IBM93");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   237
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   238
        //Try singlebyte first
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   239
        byte[] bb = new byte[1];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   240
        System.out.printf("       trying SB...%n");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   241
        for (int b = 0; b < 0x100; b++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   242
            bb[0] = (byte)b;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   243
            String sOld = new String(bb, oldCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   244
            String sNew = new String(bb, newCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   245
            if (!sOld.equals(sNew)) {
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   246
                System.out.printf("        b=%x:  %x/%d(old)  %x/%d(new)%n",
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   247
                                  b& 0xff,
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   248
                                  sOld.charAt(0) & 0xffff, sOld.length(),
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   249
                                  sNew.charAt(0) & 0xffff, sNew.length());
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   250
            }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   251
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   252
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   253
        System.out.printf("       trying DB...%n");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   254
        bb = new byte[isEBCDIC?4:2];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   255
        int b1Min = 0x40;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   256
        int b1Max = 0xfe;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   257
        for (int b1 = 0x40; b1 < 0xff; b1++) {
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   258
            if (!isEBCDIC) {
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   259
                // decodable singlebyte b1
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   260
                bb[0] = (byte)b1;
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   261
                String sOld = new String(bb, oldCS);
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   262
                String sNew = new String(bb, newCS);
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   263
                if (!sOld.equals(sNew)) {
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   264
                    if (sOld.length() != 2 && sOld.charAt(0) != 0) {
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   265
                        // only prints we are NOT expected. above two are known issue
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   266
                        System.out.printf("        b1=%x:  %x/%d(old)  %x/%d(new)%n",
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   267
                                          b1 & 0xff,
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   268
                                          sOld.charAt(0) & 0xffff, sOld.length(),
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   269
                                          sNew.charAt(0) & 0xffff, sNew.length());
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   270
                        continue;
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   271
                    }
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   272
                }
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   273
            }
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   274
            for (int b2 = 0x40; b2 < 0xff; b2++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   275
                if (isEBCDIC) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   276
                    bb[0] = 0x0e;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   277
                    bb[1] = (byte)b1;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   278
                    bb[2] = (byte)b2;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   279
                    bb[3] = 0x0f;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   280
                } else {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   281
                    bb[0] = (byte)b1;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   282
                    bb[1] = (byte)b2;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   283
                }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   284
                String sOld = new String(bb, oldCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   285
                String sNew = new String(bb, newCS);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   286
                //if (!sOld.equals(sNew)) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   287
                if (sOld.charAt(0) != sNew.charAt(0)) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   288
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   289
if (sOld.charAt(0) == 0 && sNew.charAt(0) == 0xfffd)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   290
    continue; // known issude in old implementation
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   291
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   292
                    System.out.printf("        bb=<%x,%x>  c(old)=%x,  c(new)=%x%n",
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   293
                        b1, b2, sOld.charAt(0) & 0xffff, sNew.charAt(0) & 0xffff);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   294
                }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   295
            }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   296
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   297
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   298
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   299
    static void checkInit(String csn) throws Exception {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   300
        System.out.printf("Check init <%s>...%n", csn);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   301
        Charset.forName("Big5");    // load in the ExtendedCharsets
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   302
        long t1 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   303
        Charset cs = Charset.forName(csn);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   304
        long t2 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   305
        System.out.printf("    charset     :%d%n", t2 - t1);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   306
        t1 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   307
            cs.newDecoder();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   308
        t2 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   309
        System.out.printf("    new Decoder :%d%n", t2 - t1);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   310
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   311
        t1 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   312
            cs.newEncoder();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   313
        t2 = System.nanoTime()/1000;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   314
        System.out.printf("    new Encoder :%d%n", t2 - t1);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   315
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   316
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   317
    static void compare(Charset cs1, Charset cs2, char[] cc) throws Exception {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   318
        System.gc();    // enqueue finalizable objects
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   319
        Thread.sleep(1000);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   320
        System.gc();    // enqueue finalizable objects
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   321
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   322
        String csn1 = cs1.name();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   323
        String csn2 = cs2.name();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   324
        System.out.printf("Diff     <%s> <%s>...%n", csn1, csn2);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   325
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   326
        Time t1 = new Time();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   327
        Time t2 = new Time();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   328
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   329
        byte[] bb1 = encode(cc, cs1, false, t1);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   330
        byte[] bb2 = encode(cc, cs2, false, t2);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   331
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   332
        System.out.printf("    Encoding TimeRatio %s/%s: %d,%d :%f%n",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   333
                          csn2, csn1,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   334
                          t2.t, t1.t,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   335
                          (double)(t2.t)/(t1.t));
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   336
        if (!Arrays.equals(bb1, bb2)) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   337
            System.out.printf("        encoding failed%n");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   338
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   339
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   340
        char[] cc2 = decode(bb1, cs2, false, t2);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   341
        char[] cc1 = decode(bb1, cs1, false, t1);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   342
        System.out.printf("    Decoding TimeRatio %s/%s: %d,%d :%f%n",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   343
                          csn2, csn1,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   344
                          t2.t, t1.t,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   345
                          (double)(t2.t)/(t1.t));
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   346
        if (!Arrays.equals(cc1, cc2)) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   347
            System.out.printf("        decoding failed%n");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   348
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   349
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   350
        bb1 = encode(cc, cs1, true, t1);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   351
        bb2 = encode(cc, cs2, true, t2);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   352
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   353
        System.out.printf("    Encoding(dir) TimeRatio %s/%s: %d,%d :%f%n",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   354
                          csn2, csn1,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   355
                          t2.t, t1.t,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   356
                          (double)(t2.t)/(t1.t));
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   357
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   358
        if (!Arrays.equals(bb1, bb2))
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   359
            System.out.printf("        encoding (direct) failed%n");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   360
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   361
        cc1 = decode(bb1, cs1, true, t1);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   362
        cc2 = decode(bb1, cs2, true, t2);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   363
        System.out.printf("    Decoding(dir) TimeRatio %s/%s: %d,%d :%f%n",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   364
                          csn2, csn1,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   365
                          t2.t, t1.t,
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   366
                          (double)(t2.t)/(t1.t));
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   367
        if (!Arrays.equals(cc1, cc2)) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   368
            System.out.printf("        decoding (direct) failed%n");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   369
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   370
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   371
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   372
    /* The first byte is the length of malformed bytes
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   373
        byte[][] malformed = {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   374
            {5, (byte)0xF8, (byte)0x80, (byte)0x80, (byte)0x9F, (byte)0x80, (byte)0xC0 },
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   375
        };
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   376
    */
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   377
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   378
    static void checkMalformed(Charset cs, byte[][] malformed)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   379
        throws Exception
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   380
    {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   381
        boolean failed = false;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   382
        String csn = cs.name();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   383
        System.out.printf("Check malformed <%s>...%n", csn);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   384
        for (boolean direct: new boolean[] {false, true}) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   385
            for (byte[] bins : malformed) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   386
                int mlen = bins[0];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   387
                byte[] bin = Arrays.copyOfRange(bins, 1, bins.length);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   388
                CoderResult cr = decodeCR(bin, cs, direct);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   389
                String ashex = "";
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   390
                for (int i = 0; i < bin.length; i++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   391
                    if (i > 0) ashex += " ";
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   392
                        ashex += Integer.toString((int)bin[i] & 0xff, 16);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   393
                }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   394
                if (!cr.isMalformed()) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   395
                    System.out.printf("        FAIL(direct=%b): [%s] not malformed. -->cr=%s\n", direct, ashex, cr.toString());
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   396
                    failed = true;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   397
                } else if (cr.length() != mlen) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   398
                    System.out.printf("        FAIL(direct=%b): [%s] malformed[len=%d].\n", direct, ashex, cr.length());
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   399
                    failed = true;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   400
                }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   401
            }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   402
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   403
        if (failed)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   404
            throw new RuntimeException("Check malformed failed " + csn);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   405
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   406
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   407
    static boolean check(CharsetDecoder dec, byte[] bytes, boolean direct, int[] flow) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   408
        int inPos = flow[0];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   409
        int inLen = flow[1];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   410
        int outPos = flow[2];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   411
        int outLen = flow[3];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   412
        int expedInPos = flow[4];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   413
        int expedOutPos = flow[5];
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   414
        CoderResult expedCR = (flow[6]==0)?CoderResult.UNDERFLOW
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   415
                                          :CoderResult.OVERFLOW;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   416
        ByteBuffer bbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   417
        CharBuffer cbf;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   418
        if (direct) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   419
            bbf = ByteBuffer.allocateDirect(inPos + bytes.length);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   420
            cbf = ByteBuffer.allocateDirect((outPos + outLen)*2).asCharBuffer();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   421
        } else {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   422
            bbf = ByteBuffer.allocate(inPos + bytes.length);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   423
            cbf = CharBuffer.allocate(outPos + outLen);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   424
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   425
        bbf.position(inPos);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   426
        bbf.put(bytes).flip().position(inPos).limit(inPos + inLen);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   427
        cbf.position(outPos);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   428
        dec.reset();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   429
        CoderResult cr = dec.decode(bbf, cbf, false);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   430
        if (cr != expedCR ||
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   431
            bbf.position() != expedInPos ||
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   432
            cbf.position() != expedOutPos) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   433
            System.out.printf("Expected(direct=%5b): [", direct);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   434
            for (int i:flow) System.out.print(" " + i);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   435
            System.out.println("]  CR=" + cr +
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   436
                               ", inPos=" + bbf.position() +
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   437
                               ", outPos=" + cbf.position());
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   438
            return false;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   439
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   440
        return true;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   441
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   442
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   443
    static void checkUnderOverflow(Charset cs) throws Exception {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   444
        String csn = cs.name();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   445
        System.out.printf("Check under/overflow <%s>...%n", csn);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   446
        CharsetDecoder dec = cs.newDecoder();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   447
        boolean failed = false;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   448
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   449
        //7f, a1a1, 8ea2a1a1, 8ea3a1a1, 8ea7a1a1
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   450
        //0   1 2   3         7         11
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   451
        byte[] bytes = new String("\u007f\u3000\u4e42\u4e28\ud840\udc55").getBytes("EUC_TW");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   452
        int    inlen = bytes.length;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   453
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   454
        int MAXOFF = 20;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   455
        for (int inoff = 0; inoff < MAXOFF; inoff++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   456
            for (int outoff = 0; outoff < MAXOFF; outoff++) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   457
        int[][] Flows = {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   458
            //inpos, inLen, outPos,  outLen, inPosEP,    outposEP,   under(0)/over(1)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   459
            //overflow
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   460
            {inoff,  inlen, outoff,  1,      inoff + 1,  outoff + 1, 1},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   461
            {inoff,  inlen, outoff,  2,      inoff + 3,  outoff + 2, 1},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   462
            {inoff,  inlen, outoff,  3,      inoff + 7,  outoff + 3, 1},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   463
            {inoff,  inlen, outoff,  4,      inoff + 11, outoff + 4, 1},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   464
            {inoff,  inlen, outoff,  5,      inoff + 11, outoff + 4, 1},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   465
            {inoff,  inlen, outoff,  6,      inoff + 15, outoff + 6, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   466
            //underflow
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   467
            {inoff,  1,     outoff,  6,      inoff + 1,  outoff + 1, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   468
            {inoff,  2,     outoff,  6,      inoff + 1,  outoff + 1, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   469
            {inoff,  3,     outoff,  6,      inoff + 3,  outoff + 2, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   470
            {inoff,  4,     outoff,  6,      inoff + 3,  outoff + 2, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   471
            {inoff,  5,     outoff,  6,      inoff + 3,  outoff + 2, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   472
            {inoff,  8,     outoff,  6,      inoff + 7,  outoff + 3, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   473
            {inoff,  9,     outoff,  6,      inoff + 7,  outoff + 3, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   474
            {inoff, 10,     outoff,  6,      inoff + 7,  outoff + 3, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   475
            {inoff, 11,     outoff,  6,      inoff +11,  outoff + 4, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   476
            {inoff, 12,     outoff,  6,      inoff +11,  outoff + 4, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   477
            {inoff, 15,     outoff,  6,      inoff +15,  outoff + 6, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   478
            // 2-byte under/overflow
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   479
            {inoff,  2,     outoff,  1,      inoff + 1,  outoff + 1, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   480
            {inoff,  3,     outoff,  1,      inoff + 1,  outoff + 1, 1},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   481
            {inoff,  3,     outoff,  2,      inoff + 3,  outoff + 2, 0},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   482
        };
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   483
        for (boolean direct: new boolean[] {false, true}) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   484
            for (int[] flow: Flows) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   485
                if (!check(dec, bytes, direct, flow))
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   486
                    failed = true;
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   487
            }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   488
        }}}
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   489
        if (failed)
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   490
            throw new RuntimeException("Check under/overflow failed " + csn);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   491
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   492
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   493
    static String[] csnames = new String[] {
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
   494
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   495
        "IBM930",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   496
        "IBM933",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   497
        "IBM935",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   498
        "IBM937",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   499
        "IBM939",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   500
        "IBM942",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   501
        "IBM943",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   502
        "IBM948",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   503
        "IBM949",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   504
        "IBM950",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   505
        "IBM970",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   506
        "IBM942C",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   507
        "IBM943C",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   508
        "IBM949C",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   509
        "IBM1381",
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   510
        "IBM1383",
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   511
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   512
        "EUC_CN",
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   513
        "EUC_KR",
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   514
        "GBK",
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   515
        "Johab",
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   516
        "MS932",
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   517
        "MS936",
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   518
        "MS949",
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2921
diff changeset
   519
        "MS950",
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
   520
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
   521
        "EUC_JP",
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
   522
        "EUC_JP_LINUX",
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
   523
        "EUC_JP_Open",
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
   524
        "SJIS",
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
   525
        "PCK",
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   526
    };
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   527
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   528
    public static void main(String[] args) throws Exception {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   529
        for (String csname: csnames) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   530
            System.out.printf("-----------------------------------%n");
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   531
            String oldname = csname + "_OLD";
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
   532
            if ("EUC_JP_Open".equals(csname))
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 12180
diff changeset
   533
                csname = "eucjp-open";
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   534
            checkInit(csname);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   535
            Charset csOld = (Charset)Class.forName(oldname).newInstance();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   536
            Charset csNew = Charset.forName(csname);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   537
            char[] cc = checkEncoding(csOld, csNew);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   538
            checkDecoding(csOld, csNew);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   539
            compare(csNew, csOld, cc);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   540
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   541
            if (csname.startsWith("x-IBM93")) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   542
                //ecdbic
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   543
                checkMalformed(csNew, new byte[][] {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   544
                    {1, 0x26, 0x0f, 0x27},         // in SBSC, no SI
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   545
                    {1, 0x0e, 0x41, 0x41, 0xe},    // in DBSC, no SO
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   546
                    {2, 0x0e, 0x40, 0x41, 0xe},    // illegal DB
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   547
                });
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   548
            } else if (csname.equals("x-IBM970") ||
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   549
                       csname.equals("x-IBM1383")) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   550
                //euc_simple
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   551
                checkMalformed(csNew, new byte[][] {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   552
                    {1, 0x26, (byte)0x8f, 0x27},                   // SS2
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   553
                    {1, (byte)0xa1, (byte)0xa1, (byte)0x8e, 0x51}, // SS3
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   554
                });
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   555
            }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   556
        }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   557
    }
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents:
diff changeset
   558
}