jdk/test/sun/nio/cs/TestEUC_TW.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 30820 0d4717a011d3
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2914
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2921
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
2914
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
     4
 *
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
     8
 *
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    13
 * accompanied this code).
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    14
 *
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2921
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2921
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2921
diff changeset
    21
 * questions.
2914
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    22
 */
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    23
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    24
/*
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    25
 * @test
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    26
 * @bug 6831794 6229811
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    27
 * @summary Test EUC_TW charset
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    28
 */
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    29
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    30
import java.nio.charset.*;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    31
import java.nio.*;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    32
import java.util.*;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    33
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    34
public class TestEUC_TW {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    35
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    36
    static class Time {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    37
        long t;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    38
    }
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2914
diff changeset
    39
    static int iteration = 100;
2914
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    40
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    41
    static char[] decode(byte[] bb, Charset cs, boolean testDirect, Time t)
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    42
        throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    43
        String csn = cs.name();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    44
        CharsetDecoder dec = cs.newDecoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    45
        ByteBuffer bbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    46
        CharBuffer cbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    47
        if (testDirect) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    48
            bbf = ByteBuffer.allocateDirect(bb.length);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    49
            cbf = ByteBuffer.allocateDirect(bb.length*2).asCharBuffer();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    50
            bbf.put(bb);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    51
        } else {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    52
            bbf = ByteBuffer.wrap(bb);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    53
            cbf = CharBuffer.allocate(bb.length);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    54
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    55
        CoderResult cr = null;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    56
        long t1 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    57
        for (int i = 0; i < iteration; i++) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    58
            bbf.rewind();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    59
            cbf.clear();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    60
            dec.reset();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    61
            cr = dec.decode(bbf, cbf, true);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    62
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    63
        long t2 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    64
        if (t != null)
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    65
        t.t = (t2 - t1)/iteration;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    66
        if (cr != CoderResult.UNDERFLOW) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    67
            System.out.println("DEC-----------------");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    68
            int pos = bbf.position();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    69
            System.out.printf("  cr=%s, bbf.pos=%d, bb[pos]=%x,%x,%x,%x%n",
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    70
                              cr.toString(), pos,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    71
                              bb[pos++]&0xff, bb[pos++]&0xff,bb[pos++]&0xff, bb[pos++]&0xff);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    72
            throw new RuntimeException("Decoding err: " + csn);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    73
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    74
        char[] cc = new char[cbf.position()];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    75
        cbf.flip(); cbf.get(cc);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    76
        return cc;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    77
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    78
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    79
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    80
    static CoderResult decodeCR(byte[] bb, Charset cs, boolean testDirect)
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    81
        throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    82
        CharsetDecoder dec = cs.newDecoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    83
        ByteBuffer bbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    84
        CharBuffer cbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    85
        if (testDirect) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    86
            bbf = ByteBuffer.allocateDirect(bb.length);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    87
            cbf = ByteBuffer.allocateDirect(bb.length*2).asCharBuffer();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    88
            bbf.put(bb).flip();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    89
        } else {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    90
            bbf = ByteBuffer.wrap(bb);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    91
            cbf = CharBuffer.allocate(bb.length);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    92
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    93
        return dec.decode(bbf, cbf, true);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    94
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    95
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    96
    static byte[] encode(char[] cc, Charset cs, boolean testDirect, Time t)
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    97
        throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    98
        ByteBuffer bbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
    99
        CharBuffer cbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   100
        CharsetEncoder enc = cs.newEncoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   101
        String csn = cs.name();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   102
        if (testDirect) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   103
            bbf = ByteBuffer.allocateDirect(cc.length * 4);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   104
            cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   105
            cbf.put(cc).flip();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   106
        } else {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   107
            bbf = ByteBuffer.allocate(cc.length * 4);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   108
            cbf = CharBuffer.wrap(cc);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   109
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   110
        CoderResult cr = null;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   111
        long t1 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   112
        for (int i = 0; i < iteration; i++) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   113
            cbf.rewind();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   114
            bbf.clear();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   115
            enc.reset();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   116
            cr = enc.encode(cbf, bbf, true);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   117
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   118
        long t2 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   119
        if (t != null)
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   120
        t.t = (t2 - t1)/iteration;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   121
        if (cr != CoderResult.UNDERFLOW) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   122
            System.out.println("ENC-----------------");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   123
            int pos = cbf.position();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   124
            System.out.printf("  cr=%s, cbf.pos=%d, cc[pos]=%x%n",
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   125
                              cr.toString(), pos, cc[pos]&0xffff);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   126
            throw new RuntimeException("Encoding err: " + csn);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   127
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   128
        byte[] bb = new byte[bbf.position()];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   129
        bbf.flip(); bbf.get(bb);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   130
        return bb;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   131
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   132
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   133
    static CoderResult encodeCR(char[] cc, Charset cs, boolean testDirect)
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   134
        throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   135
        ByteBuffer bbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   136
        CharBuffer cbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   137
        CharsetEncoder enc = cs.newEncoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   138
        if (testDirect) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   139
            bbf = ByteBuffer.allocateDirect(cc.length * 4);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   140
            cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   141
            cbf.put(cc).flip();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   142
        } else {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   143
            bbf = ByteBuffer.allocate(cc.length * 4);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   144
            cbf = CharBuffer.wrap(cc);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   145
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   146
        return enc.encode(cbf, bbf, true);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   147
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   148
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   149
    static char[] getEUC_TWChars(boolean skipNR) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   150
        //CharsetEncoder encOLD = Charset.forName("EUC_TW_OLD").newEncoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   151
        CharsetEncoder encOLD = new EUC_TW_OLD().newEncoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   152
        CharsetEncoder enc = Charset.forName("EUC_TW").newEncoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   153
        char[] cc = new char[0x20000];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   154
        char[] c2 = new char[2];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   155
        int pos = 0;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   156
        int i = 0;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   157
        //bmp
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   158
        for (i = 0; i < 0x10000; i++) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   159
            //SKIP these 3 NR codepoints if compared to EUC_TW
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   160
            if (skipNR && (i == 0x4ea0 || i == 0x51ab || i == 0x52f9))
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   161
                continue;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   162
            if (encOLD.canEncode((char)i) != enc.canEncode((char)i)) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   163
                System.out.printf("  Err i=%x:  old=%b new=%b%n", i,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   164
                                  encOLD.canEncode((char)i),
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   165
                                  enc.canEncode((char)i));
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   166
                throw new RuntimeException("canEncode() err!");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   167
            }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   168
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   169
            if (enc.canEncode((char)i)) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   170
                cc[pos++] = (char)i;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   171
            }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   172
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   173
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   174
        //supp
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   175
        CharBuffer cb = CharBuffer.wrap(new char[2]);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   176
        for (i = 0x20000; i < 0x30000; i++) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   177
            Character.toChars(i, c2, 0);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   178
            cb.clear();cb.put(c2[0]);cb.put(c2[1]);cb.flip();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   179
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   180
            if (encOLD.canEncode(cb) != enc.canEncode(cb)) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   181
                throw new RuntimeException("canEncode() err!");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   182
            }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   183
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   184
            if (enc.canEncode(cb)) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   185
                //System.out.printf("cp=%x,  (%x, %x) %n", i, c2[0] & 0xffff, c2[1] & 0xffff);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   186
                cc[pos++] = c2[0];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   187
                cc[pos++] = c2[1];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   188
            }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   189
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   190
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   191
        return Arrays.copyOf(cc, pos);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   192
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   193
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   194
    static void checkRoundtrip(Charset cs) throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   195
        char[] cc = getEUC_TWChars(false);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   196
        System.out.printf("Check roundtrip <%s>...", cs.name());
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   197
        byte[] bb = encode(cc, cs, false, null);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   198
        char[] ccO = decode(bb, cs, false, null);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   199
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   200
        if (!Arrays.equals(cc, ccO)) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   201
            System.out.printf("    non-direct failed");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   202
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   203
        bb = encode(cc, cs, true, null);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   204
        ccO = decode(bb, cs, true, null);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   205
        if (!Arrays.equals(cc, ccO)) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   206
            System.out.printf("    (direct) failed");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   207
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   208
        System.out.println();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   209
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   210
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   211
    static void checkInit(String csn) throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   212
        System.out.printf("Check init <%s>...%n", csn);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   213
        Charset.forName("Big5");    // load in the ExtendedCharsets
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   214
        long t1 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   215
        Charset cs = Charset.forName(csn);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   216
        long t2 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   217
        System.out.printf("    charset     :%d%n", t2 - t1);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   218
        t1 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   219
            cs.newDecoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   220
        t2 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   221
        System.out.printf("    new Decoder :%d%n", t2 - t1);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   222
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   223
        t1 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   224
            cs.newEncoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   225
        t2 = System.nanoTime()/1000;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   226
        System.out.printf("    new Encoder :%d%n", t2 - t1);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   227
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   228
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   229
    static void compare(Charset cs1, Charset cs2) throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   230
        char[] cc = getEUC_TWChars(true);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   231
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   232
        String csn1 = cs1.name();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   233
        String csn2 = cs2.name();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   234
        System.out.printf("Diff     <%s> <%s>...%n", csn1, csn2);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   235
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   236
        Time t1 = new Time();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   237
        Time t2 = new Time();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   238
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   239
        byte[] bb1 = encode(cc, cs1, false, t1);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   240
        byte[] bb2 = encode(cc, cs2, false, t2);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   241
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   242
        System.out.printf("    Encoding TimeRatio %s/%s: %d,%d :%f%n",
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   243
                          csn2, csn1,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   244
                          t2.t, t1.t,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   245
                          (double)(t2.t)/(t1.t));
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   246
        if (!Arrays.equals(bb1, bb2)) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   247
            System.out.printf("        encoding failed%n");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   248
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   249
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   250
        char[] cc2 = decode(bb1, cs2, false, t2);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   251
        char[] cc1 = decode(bb1, cs1, false, t1);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   252
        System.out.printf("    Decoding TimeRatio %s/%s: %d,%d :%f%n",
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   253
                          csn2, csn1,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   254
                          t2.t, t1.t,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   255
                          (double)(t2.t)/(t1.t));
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   256
        if (!Arrays.equals(cc1, cc2)) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   257
            System.out.printf("        decoding failed%n");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   258
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   259
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   260
        bb1 = encode(cc, cs1, true, t1);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   261
        bb2 = encode(cc, cs2, true, t2);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   262
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   263
        System.out.printf("    Encoding(dir) TimeRatio %s/%s: %d,%d :%f%n",
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   264
                          csn2, csn1,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   265
                          t2.t, t1.t,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   266
                          (double)(t2.t)/(t1.t));
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   267
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   268
        if (!Arrays.equals(bb1, bb2))
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   269
            System.out.printf("        encoding (direct) failed%n");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   270
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   271
        cc1 = decode(bb1, cs1, true, t1);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   272
        cc2 = decode(bb1, cs2, true, t2);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   273
        System.out.printf("    Decoding(dir) TimeRatio %s/%s: %d,%d :%f%n",
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   274
                          csn2, csn1,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   275
                          t2.t, t1.t,
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   276
                          (double)(t2.t)/(t1.t));
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   277
        if (!Arrays.equals(cc1, cc2)) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   278
            System.out.printf("        decoding (direct) failed%n");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   279
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   280
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   281
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   282
    // The first byte is the length of malformed bytes
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   283
    static byte[][] malformed = {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   284
        //{5, (byte)0xF8, (byte)0x80, (byte)0x80, (byte)0x9F, (byte)0x80, (byte)0xC0 },
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   285
    };
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   286
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   287
    static void checkMalformed(Charset cs) throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   288
        boolean failed = false;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   289
        String csn = cs.name();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   290
        System.out.printf("Check malformed <%s>...%n", csn);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   291
        for (boolean direct: new boolean[] {false, true}) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   292
            for (byte[] bins : malformed) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   293
                int mlen = bins[0];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   294
                byte[] bin = Arrays.copyOfRange(bins, 1, bins.length);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   295
                CoderResult cr = decodeCR(bin, cs, direct);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   296
                String ashex = "";
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   297
                for (int i = 0; i < bin.length; i++) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   298
                    if (i > 0) ashex += " ";
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   299
                        ashex += Integer.toBinaryString((int)bin[i] & 0xff);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   300
                }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   301
                if (!cr.isMalformed()) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   302
                    System.out.printf("        FAIL(direct=%b): [%s] not malformed.\n", direct, ashex);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   303
                    failed = true;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   304
                } else if (cr.length() != mlen) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   305
                    System.out.printf("        FAIL(direct=%b): [%s] malformed[len=%d].\n", direct, ashex, cr.length());
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   306
                    failed = true;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   307
                }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   308
            }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   309
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   310
        if (failed)
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   311
            throw new RuntimeException("Check malformed failed " + csn);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   312
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   313
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   314
    static boolean check(CharsetDecoder dec, byte[] bytes, boolean direct, int[] flow) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   315
        int inPos = flow[0];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   316
        int inLen = flow[1];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   317
        int outPos = flow[2];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   318
        int outLen = flow[3];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   319
        int expedInPos = flow[4];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   320
        int expedOutPos = flow[5];
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   321
        CoderResult expedCR = (flow[6]==0)?CoderResult.UNDERFLOW
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   322
                                          :CoderResult.OVERFLOW;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   323
        ByteBuffer bbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   324
        CharBuffer cbf;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   325
        if (direct) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   326
            bbf = ByteBuffer.allocateDirect(inPos + bytes.length);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   327
            cbf = ByteBuffer.allocateDirect((outPos + outLen)*2).asCharBuffer();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   328
        } else {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   329
            bbf = ByteBuffer.allocate(inPos + bytes.length);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   330
            cbf = CharBuffer.allocate(outPos + outLen);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   331
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   332
        bbf.position(inPos);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   333
        bbf.put(bytes).flip().position(inPos).limit(inPos + inLen);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   334
        cbf.position(outPos);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   335
        dec.reset();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   336
        CoderResult cr = dec.decode(bbf, cbf, false);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   337
        if (cr != expedCR ||
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   338
            bbf.position() != expedInPos ||
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   339
            cbf.position() != expedOutPos) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   340
            System.out.printf("Expected(direct=%5b): [", direct);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   341
            for (int i:flow) System.out.print(" " + i);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   342
            System.out.println("]  CR=" + cr +
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   343
                               ", inPos=" + bbf.position() +
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   344
                               ", outPos=" + cbf.position());
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   345
            return false;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   346
        }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   347
        return true;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   348
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   349
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   350
    static void checkUnderOverflow(Charset cs) throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   351
        String csn = cs.name();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   352
        System.out.printf("Check under/overflow <%s>...%n", csn);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   353
        CharsetDecoder dec = cs.newDecoder();
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   354
        boolean failed = false;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   355
        //7f, a1a1, 8ea2a1a1, 8ea3a1a1, 8ea7a1a1
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   356
        //0   1 2   3         7         11
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   357
        byte[] bytes = new String("\u007f\u3000\u4e42\u4e28\ud840\udc55").getBytes("EUC_TW");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   358
        int    inlen = bytes.length;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   359
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   360
        int MAXOFF = 20;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   361
        for (int inoff = 0; inoff < MAXOFF; inoff++) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   362
            for (int outoff = 0; outoff < MAXOFF; outoff++) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   363
        int[][] Flows = {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   364
            //inpos, inLen, outPos,  outLen, inPosEP,    outposEP,   under(0)/over(1)
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   365
            //overflow
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   366
            {inoff,  inlen, outoff,  1,      inoff + 1,  outoff + 1, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   367
            {inoff,  inlen, outoff,  2,      inoff + 3,  outoff + 2, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   368
            {inoff,  inlen, outoff,  3,      inoff + 7,  outoff + 3, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   369
            {inoff,  inlen, outoff,  4,      inoff + 11, outoff + 4, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   370
            {inoff,  inlen, outoff,  5,      inoff + 11, outoff + 4, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   371
            {inoff,  inlen, outoff,  6,      inoff + 15, outoff + 6, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   372
            //underflow
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   373
            {inoff,  1,     outoff,  6,      inoff + 1,  outoff + 1, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   374
            {inoff,  2,     outoff,  6,      inoff + 1,  outoff + 1, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   375
            {inoff,  3,     outoff,  6,      inoff + 3,  outoff + 2, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   376
            {inoff,  4,     outoff,  6,      inoff + 3,  outoff + 2, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   377
            {inoff,  5,     outoff,  6,      inoff + 3,  outoff + 2, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   378
            {inoff,  8,     outoff,  6,      inoff + 7,  outoff + 3, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   379
            {inoff,  9,     outoff,  6,      inoff + 7,  outoff + 3, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   380
            {inoff, 10,     outoff,  6,      inoff + 7,  outoff + 3, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   381
            {inoff, 11,     outoff,  6,      inoff +11,  outoff + 4, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   382
            {inoff, 12,     outoff,  6,      inoff +11,  outoff + 4, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   383
            {inoff, 15,     outoff,  6,      inoff +15,  outoff + 6, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   384
            // 2-byte under/overflow
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   385
            {inoff,  2,     outoff,  1,      inoff + 1,  outoff + 1, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   386
            {inoff,  3,     outoff,  1,      inoff + 1,  outoff + 1, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   387
            {inoff,  3,     outoff,  2,      inoff + 3,  outoff + 2, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   388
            // 4-byte  under/overflow
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   389
            {inoff,  4,     outoff,  2,      inoff + 3,  outoff + 2, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   390
            {inoff,  5,     outoff,  2,      inoff + 3,  outoff + 2, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   391
            {inoff,  6,     outoff,  2,      inoff + 3,  outoff + 2, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   392
            {inoff,  7,     outoff,  2,      inoff + 3,  outoff + 2, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   393
            {inoff,  7,     outoff,  3,      inoff + 7,  outoff + 3, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   394
            // 4-byte  under/overflow
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   395
            {inoff,  8,     outoff,  3,      inoff + 7,  outoff + 3, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   396
            {inoff,  9,     outoff,  3,      inoff + 7,  outoff + 3, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   397
            {inoff, 10,     outoff,  3,      inoff + 7,  outoff + 3, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   398
            {inoff, 11,     outoff,  3,      inoff + 7,  outoff + 3, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   399
            {inoff, 11,     outoff,  4,      inoff +11,  outoff + 4, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   400
            // 4-byte/supp  under/overflow
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   401
            {inoff, 11,     outoff,  4,      inoff +11,  outoff + 4, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   402
            {inoff, 12,     outoff,  4,      inoff +11,  outoff + 4, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   403
            {inoff, 13,     outoff,  4,      inoff +11,  outoff + 4, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   404
            {inoff, 14,     outoff,  4,      inoff +11,  outoff + 4, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   405
            {inoff, 15,     outoff,  4,      inoff +11,  outoff + 4, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   406
            {inoff, 15,     outoff,  5,      inoff +11,  outoff + 4, 1},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   407
            {inoff, 15,     outoff,  6,      inoff +15,  outoff + 6, 0},
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   408
        };
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   409
        for (boolean direct: new boolean[] {false, true}) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   410
            for (int[] flow: Flows) {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   411
                if (!check(dec, bytes, direct, flow))
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   412
                    failed = true;
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   413
            }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   414
        }}}
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   415
        if (failed)
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   416
            throw new RuntimeException("Check under/overflow failed " + csn);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   417
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   418
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   419
    public static void main(String[] args) throws Exception {
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   420
        // be the first one
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   421
        //checkInit("EUC_TW_OLD");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   422
        checkInit("EUC_TW");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   423
        Charset euctw = Charset.forName("EUC_TW");
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   424
        checkRoundtrip(euctw);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   425
        compare(euctw, new EUC_TW_OLD());
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   426
        checkMalformed(euctw);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   427
        checkUnderOverflow(euctw);
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   428
    }
1375969fd02e 6843079: Putback for the new EUC_TW is not complete
sherman
parents:
diff changeset
   429
}