jdk/test/sun/nio/cs/NIOJISAutoDetectTest.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 32038 911671c0c520
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     4
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     8
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    13
 * accompanied this code).
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    14
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    21
 * questions.
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    22
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    23
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    24
/*
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    25
 * @test
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    26
 * @bug 4831163 5053096 5056440
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    27
 * @summary NIO charset basic verification of JISAutodetect decoder
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    28
 * @author Martin Buchholz
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    29
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    30
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    31
import java.io.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    32
import java.nio.ByteBuffer;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    33
import java.nio.CharBuffer;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    34
import java.nio.charset.Charset;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    35
import java.nio.charset.CharsetDecoder;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    36
import java.nio.charset.CoderResult;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    37
import static java.lang.System.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    38
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    39
public class NIOJISAutoDetectTest {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    40
    private static int failures = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    41
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    42
    private static void fail(String failureMsg) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    43
        System.out.println(failureMsg);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    44
        failures++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    45
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    46
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    47
    private static void check(boolean cond, String msg) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    48
        if (!cond) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    49
            fail("test failed: " + msg);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    50
            new Exception().printStackTrace();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    51
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    52
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    53
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    54
    private static String SJISName() throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    55
        return detectingCharset(new byte[] {(byte)0xbb, (byte)0xdd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    56
                                            (byte)0xcf, (byte)0xb2});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    57
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    58
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    59
    private static String EUCJName() throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    60
        return detectingCharset(new byte[] {(byte)0xa4, (byte)0xd2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    61
                                            (byte)0xa4, (byte)0xe9});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    62
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    63
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    64
    private static String detectingCharset(byte[] bytes) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    65
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    66
        // Test special public methods of CharsetDecoder while we're here
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    67
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    68
        CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    69
        check(cd.isAutoDetecting(), "isAutodecting()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    70
        check(! cd.isCharsetDetected(), "isCharsetDetected");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    71
        cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    72
        check(! cd.isCharsetDetected(), "isCharsetDetected");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    73
        try {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    74
            cd.detectedCharset();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    75
            fail("no IllegalStateException");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    76
        } catch (IllegalStateException e) {}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    77
        cd.decode(ByteBuffer.wrap(bytes));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    78
        check(cd.isCharsetDetected(), "isCharsetDetected");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    79
        Charset cs = cd.detectedCharset();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    80
        check(cs != null, "cs != null");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    81
        check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    82
        return cs.name();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    83
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    84
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    85
    public static void main(String[] argv) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    86
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    87
        // Used to throw BufferOverflowException
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    88
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    89
        out.println(new String(new byte[] {0x61}, "JISAutoDetect"));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    90
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    91
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    92
        // InputStreamReader(...JISAutoDetect) used to infloop
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    93
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    94
        {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    95
            byte[] bytes = "ABCD\n".getBytes();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    96
            ByteArrayInputStream bais = new  ByteArrayInputStream(bytes);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    97
            InputStreamReader isr = new InputStreamReader(bais, "JISAutoDetect");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    98
            BufferedReader reader = new BufferedReader(isr);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    99
            check (reader.readLine().equals("ABCD"), "first read gets text");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   100
            // used to return "ABCD" on second and subsequent reads
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   101
            check (reader.readLine() == null, "second read gets null");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   102
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   103
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   104
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   105
        // Check all Japanese chars for sanity
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   106
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   107
        String SJIS = SJISName();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   108
        String EUCJ = EUCJName();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   109
        out.printf("SJIS charset is %s%n", SJIS);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   110
        out.printf("EUCJ charset is %s%n", EUCJ);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   111
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   112
        int cnt2022 = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   113
        int cnteucj = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   114
        int cntsjis = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   115
        int cntBAD  = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   116
        for (char c = '\u0000'; c < '\uffff'; c++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   117
            if (c == '\u001b' || // ESC
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   118
                c == '\u2014')   // Em-Dash?
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   119
                continue;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   120
            String s = new String (new char[] {c});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   121
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   122
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   123
            // JISAutoDetect can handle all chars that EUC-JP can,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   124
            // unless there is an ambiguity with SJIS.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   125
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   126
            byte[] beucj = s.getBytes(EUCJ);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   127
            String seucj = new String(beucj, EUCJ);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   128
            if (seucj.equals(s)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   129
                cnteucj++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   130
                String sauto = new String(beucj, "JISAutoDetect");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   131
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   132
                if (! sauto.equals(seucj)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   133
                    cntBAD++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   134
                    String ssjis = new String(beucj, SJIS);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   135
                    if (! sauto.equals(ssjis)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   136
                        fail("Autodetection agrees with neither EUC nor SJIS");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   137
                    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   138
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   139
            } else
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   140
                continue; // Optimization
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   141
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   142
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   143
            // JISAutoDetect can handle all chars that ISO-2022-JP can.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   144
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   145
            byte[] b2022 = s.getBytes("ISO-2022-JP");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   146
            if (new String(b2022, "ISO-2022-JP").equals(s)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   147
                cnt2022++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   148
                check(new String(b2022,"JISAutoDetect").equals(s),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   149
                      "ISO2022 autodetection");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   150
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   151
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   152
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   153
            // JISAutoDetect can handle almost all chars that SJIS can.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   154
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   155
            byte[] bsjis = s.getBytes(SJIS);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   156
            if (new String(bsjis, SJIS).equals(s)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   157
                cntsjis++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   158
                check(new String(bsjis,"JISAutoDetect").equals(s),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   159
                      "SJIS autodetection");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   160
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   161
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   162
        out.printf("There are %d ISO-2022-JP-encodable characters.%n", cnt2022);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   163
        out.printf("There are %d SJIS-encodable characters.%n",        cntsjis);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   164
        out.printf("There are %d EUC-JP-encodable characters.%n",      cnteucj);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   165
        out.printf("There are %d characters that are " +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   166
                   "misdetected as SJIS after being EUC-encoded.%n", cntBAD);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   167
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   168
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   169
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   170
        // tests for specific byte sequences
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   171
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   172
        test("ISO-2022-JP", new byte[] {'A', 'B', 'C'});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   173
        test("EUC-JP",      new byte[] {'A', 'B', 'C'});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   174
        test("SJIS",        new byte[] {'A', 'B', 'C'});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   175
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   176
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   177
             new byte[] { 'C', 'o', 'p',  'y',  'r', 'i', 'g',  'h', 't',
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   178
                          ' ', (byte)0xa9, ' ', '1', '9', '9',  '8' });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   179
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   180
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   181
             new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   182
                          (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   183
                          (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   184
                          (byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   185
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   186
        test("EUC-JP",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   187
             new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   188
                          (byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   189
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   190
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   191
             new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   192
                          (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   193
                          (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   194
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   195
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   196
             new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   197
                          (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   198
                          (byte)0xc3, (byte)0xd1, (byte)0xbd });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   199
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   200
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   201
             new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   202
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   203
        test("EUC-JP",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   204
             new byte[] { (byte)0x8f, (byte)0xc5, (byte)0xe0, (byte)0x20});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   205
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   206
        test("EUC-JP",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   207
             new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   208
                          (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   209
                          (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   210
                          (byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   211
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   212
        test("ISO-2022-JP",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   213
             new byte[] { 0x1b, '$', 'B', '#', '4', '$', '5', 0x1b, '(', 'B' });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   214
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   215
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   216
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   217
        // Check handling of ambiguous end-of-input in middle of first char
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   218
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   219
        {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   220
            CharsetDecoder dc = Charset.forName("x-JISAutoDetect").newDecoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   221
            ByteBuffer bb = ByteBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   222
            CharBuffer cb = CharBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   223
            bb.put((byte)'A').put((byte)0x8f);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   224
            bb.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   225
            CoderResult res = dc.decode(bb,cb,false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   226
            check(res.isUnderflow(), "isUnderflow");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   227
            check(bb.position() == 1, "bb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   228
            check(cb.position() == 1, "cb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   229
            res = dc.decode(bb,cb,false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   230
            check(res.isUnderflow(), "isUnderflow");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   231
            check(bb.position() == 1, "bb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   232
            check(cb.position() == 1, "cb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   233
            bb.compact();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   234
            bb.put((byte)0xa1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   235
            bb.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   236
            res = dc.decode(bb,cb,true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   237
            check(res.isUnderflow(), "isUnderflow");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   238
            check(bb.position() == 2, "bb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   239
            check(cb.position() == 2, "cb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   240
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   241
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   242
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   243
        if (failures > 0)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   244
            throw new RuntimeException(failures + " tests failed");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   245
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   246
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   247
    static void checkCoderResult(CoderResult result) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   248
        check(result.isUnderflow(),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   249
              "Unexpected coder result: " + result);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   250
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   251
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   252
    static void test(String expectedCharset, byte[] input) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   253
        Charset cs = Charset.forName("x-JISAutoDetect");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   254
        CharsetDecoder autoDetect = cs.newDecoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   255
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   256
        Charset cs2 = Charset.forName(expectedCharset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   257
        CharsetDecoder decoder = cs2.newDecoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   258
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   259
        ByteBuffer bb = ByteBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   260
        CharBuffer charOutput = CharBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   261
        CharBuffer charExpected = CharBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   262
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   263
        bb.put(input);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   264
        bb.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   265
        bb.mark();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   266
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   267
        CoderResult result = autoDetect.decode(bb, charOutput, true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   268
        checkCoderResult(result);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   269
        charOutput.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   270
        String actual = charOutput.toString();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   271
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   272
        bb.reset();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   273
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   274
        result = decoder.decode(bb, charExpected, true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   275
        checkCoderResult(result);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   276
        charExpected.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   277
        String expected = charExpected.toString();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   278
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   279
        check(actual.equals(expected),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   280
              String.format("actual=%s expected=%s", actual, expected));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   281
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   282
}