test/jdk/sun/nio/cs/NIOJISAutoDetectTest.java
author jiangli
Wed, 27 Sep 2017 17:55:20 -0400
changeset 47548 664b9d44db74
parent 47216 71c04702a3d5
permissions -rw-r--r--
8068314: "Java fields that are currently set during shared space dumping" comment is incorrect Summary: CDS dump time should also initialize preallocated out_of_memory error messages. Reviewed-by: iklam, hseigel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
32038
911671c0c520 8022224: Rare bug in JISAutodetect charset detected by FindDecoderBugs test
sherman
parents: 5506
diff changeset
    26
 * @bug 4831163 5053096 5056440 8022224
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    27
 * @summary NIO charset basic verification of JISAutodetect decoder
44115
bb4e971bf5d4 8176195: Fix misc module dependencies in jdk_core tests
xiaofeya
parents: 32038
diff changeset
    28
 * @modules jdk.charsets
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    29
 * @author Martin Buchholz
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    30
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    31
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    32
import java.io.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    33
import java.nio.ByteBuffer;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    34
import java.nio.CharBuffer;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    35
import java.nio.charset.Charset;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    36
import java.nio.charset.CharsetDecoder;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    37
import java.nio.charset.CoderResult;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    38
import static java.lang.System.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    39
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    40
public class NIOJISAutoDetectTest {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    41
    private static int failures = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    42
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    43
    private static void fail(String failureMsg) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    44
        System.out.println(failureMsg);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    45
        failures++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    46
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    47
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    48
    private static void check(boolean cond, String msg) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    49
        if (!cond) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    50
            fail("test failed: " + msg);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    51
            new Exception().printStackTrace();
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
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    55
    private static String SJISName() throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    56
        return detectingCharset(new byte[] {(byte)0xbb, (byte)0xdd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    57
                                            (byte)0xcf, (byte)0xb2});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    58
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    59
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    60
    private static String EUCJName() throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    61
        return detectingCharset(new byte[] {(byte)0xa4, (byte)0xd2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    62
                                            (byte)0xa4, (byte)0xe9});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    63
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    64
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    65
    private static String detectingCharset(byte[] bytes) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    66
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    67
        // Test special public methods of CharsetDecoder while we're here
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    68
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    69
        CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    70
        check(cd.isAutoDetecting(), "isAutodecting()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    71
        check(! cd.isCharsetDetected(), "isCharsetDetected");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    72
        cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    73
        check(! cd.isCharsetDetected(), "isCharsetDetected");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    74
        try {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    75
            cd.detectedCharset();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    76
            fail("no IllegalStateException");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    77
        } catch (IllegalStateException e) {}
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    78
        cd.decode(ByteBuffer.wrap(bytes));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    79
        check(cd.isCharsetDetected(), "isCharsetDetected");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    80
        Charset cs = cd.detectedCharset();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    81
        check(cs != null, "cs != null");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    82
        check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    83
        return cs.name();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    84
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    85
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    86
    public static void main(String[] argv) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    87
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    88
        // Used to throw BufferOverflowException
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    89
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    90
        out.println(new String(new byte[] {0x61}, "JISAutoDetect"));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    91
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    92
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    93
        // InputStreamReader(...JISAutoDetect) used to infloop
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    94
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    95
        {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    96
            byte[] bytes = "ABCD\n".getBytes();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    97
            ByteArrayInputStream bais = new  ByteArrayInputStream(bytes);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    98
            InputStreamReader isr = new InputStreamReader(bais, "JISAutoDetect");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    99
            BufferedReader reader = new BufferedReader(isr);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   100
            check (reader.readLine().equals("ABCD"), "first read gets text");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   101
            // used to return "ABCD" on second and subsequent reads
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   102
            check (reader.readLine() == null, "second read gets null");
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
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   106
        // Check all Japanese chars for sanity
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   107
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   108
        String SJIS = SJISName();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   109
        String EUCJ = EUCJName();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   110
        out.printf("SJIS charset is %s%n", SJIS);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   111
        out.printf("EUCJ charset is %s%n", EUCJ);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   112
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   113
        int cnt2022 = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   114
        int cnteucj = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   115
        int cntsjis = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   116
        int cntBAD  = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   117
        for (char c = '\u0000'; c < '\uffff'; c++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   118
            if (c == '\u001b' || // ESC
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   119
                c == '\u2014')   // Em-Dash?
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   120
                continue;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   121
            String s = new String (new char[] {c});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   122
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   123
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   124
            // JISAutoDetect can handle all chars that EUC-JP can,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   125
            // unless there is an ambiguity with SJIS.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   126
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   127
            byte[] beucj = s.getBytes(EUCJ);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   128
            String seucj = new String(beucj, EUCJ);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   129
            if (seucj.equals(s)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   130
                cnteucj++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   131
                String sauto = new String(beucj, "JISAutoDetect");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   132
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   133
                if (! sauto.equals(seucj)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   134
                    cntBAD++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   135
                    String ssjis = new String(beucj, SJIS);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   136
                    if (! sauto.equals(ssjis)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   137
                        fail("Autodetection agrees with neither EUC nor SJIS");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   138
                    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   139
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   140
            } else
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   141
                continue; // Optimization
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   142
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   143
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   144
            // JISAutoDetect can handle all chars that ISO-2022-JP can.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   145
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   146
            byte[] b2022 = s.getBytes("ISO-2022-JP");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   147
            if (new String(b2022, "ISO-2022-JP").equals(s)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   148
                cnt2022++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   149
                check(new String(b2022,"JISAutoDetect").equals(s),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   150
                      "ISO2022 autodetection");
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
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   154
            // JISAutoDetect can handle almost all chars that SJIS can.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   155
            //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   156
            byte[] bsjis = s.getBytes(SJIS);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   157
            if (new String(bsjis, SJIS).equals(s)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   158
                cntsjis++;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   159
                check(new String(bsjis,"JISAutoDetect").equals(s),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   160
                      "SJIS autodetection");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   161
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   162
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   163
        out.printf("There are %d ISO-2022-JP-encodable characters.%n", cnt2022);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   164
        out.printf("There are %d SJIS-encodable characters.%n",        cntsjis);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   165
        out.printf("There are %d EUC-JP-encodable characters.%n",      cnteucj);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   166
        out.printf("There are %d characters that are " +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   167
                   "misdetected as SJIS after being EUC-encoded.%n", cntBAD);
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
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   171
        // tests for specific byte sequences
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   172
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   173
        test("ISO-2022-JP", new byte[] {'A', 'B', 'C'});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   174
        test("EUC-JP",      new byte[] {'A', 'B', 'C'});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   175
        test("SJIS",        new byte[] {'A', 'B', 'C'});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   176
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   177
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   178
             new byte[] { 'C', 'o', 'p',  'y',  'r', 'i', 'g',  'h', 't',
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   179
                          ' ', (byte)0xa9, ' ', '1', '9', '9',  '8' });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   180
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   181
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   182
             new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   183
                          (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   184
                          (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   185
                          (byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   186
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   187
        test("EUC-JP",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   188
             new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   189
                          (byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   190
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   191
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   192
             new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   193
                          (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   194
                          (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   195
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   196
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   197
             new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   198
                          (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   199
                          (byte)0xc3, (byte)0xd1, (byte)0xbd });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   200
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   201
        test("SJIS",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   202
             new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   203
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   204
        test("EUC-JP",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   205
             new byte[] { (byte)0x8f, (byte)0xc5, (byte)0xe0, (byte)0x20});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   206
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   207
        test("EUC-JP",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   208
             new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   209
                          (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   210
                          (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   211
                          (byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   212
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   213
        test("ISO-2022-JP",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   214
             new byte[] { 0x1b, '$', 'B', '#', '4', '$', '5', 0x1b, '(', 'B' });
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
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   218
        // Check handling of ambiguous end-of-input in middle of first char
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   219
        //----------------------------------------------------------------
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   220
        {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   221
            CharsetDecoder dc = Charset.forName("x-JISAutoDetect").newDecoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   222
            ByteBuffer bb = ByteBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   223
            CharBuffer cb = CharBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   224
            bb.put((byte)'A').put((byte)0x8f);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   225
            bb.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   226
            CoderResult res = dc.decode(bb,cb,false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   227
            check(res.isUnderflow(), "isUnderflow");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   228
            check(bb.position() == 1, "bb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   229
            check(cb.position() == 1, "cb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   230
            res = dc.decode(bb,cb,false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   231
            check(res.isUnderflow(), "isUnderflow");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   232
            check(bb.position() == 1, "bb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   233
            check(cb.position() == 1, "cb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   234
            bb.compact();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   235
            bb.put((byte)0xa1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   236
            bb.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   237
            res = dc.decode(bb,cb,true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   238
            check(res.isUnderflow(), "isUnderflow");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   239
            check(bb.position() == 2, "bb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   240
            check(cb.position() == 2, "cb.position()");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   241
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   242
32038
911671c0c520 8022224: Rare bug in JISAutodetect charset detected by FindDecoderBugs test
sherman
parents: 5506
diff changeset
   243
        // test #8022224
911671c0c520 8022224: Rare bug in JISAutodetect charset detected by FindDecoderBugs test
sherman
parents: 5506
diff changeset
   244
        Charset cs = Charset.forName("x-JISAutoDetect");
911671c0c520 8022224: Rare bug in JISAutodetect charset detected by FindDecoderBugs test
sherman
parents: 5506
diff changeset
   245
        ByteBuffer bb = ByteBuffer.wrap(new byte[] { 'a', 0x1b, 0x24, 0x40 });
911671c0c520 8022224: Rare bug in JISAutodetect charset detected by FindDecoderBugs test
sherman
parents: 5506
diff changeset
   246
        CharBuffer cb = CharBuffer.wrap(new char[10]);
911671c0c520 8022224: Rare bug in JISAutodetect charset detected by FindDecoderBugs test
sherman
parents: 5506
diff changeset
   247
        CoderResult cr = cs.newDecoder().decode(bb, cb, false);
911671c0c520 8022224: Rare bug in JISAutodetect charset detected by FindDecoderBugs test
sherman
parents: 5506
diff changeset
   248
        bb.rewind();
911671c0c520 8022224: Rare bug in JISAutodetect charset detected by FindDecoderBugs test
sherman
parents: 5506
diff changeset
   249
        cb.clear().limit(1);
911671c0c520 8022224: Rare bug in JISAutodetect charset detected by FindDecoderBugs test
sherman
parents: 5506
diff changeset
   250
        check(cr == cs.newDecoder().decode(bb, cb, false), "#8022224");
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   251
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   252
        if (failures > 0)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   253
            throw new RuntimeException(failures + " tests failed");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   254
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   255
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   256
    static void checkCoderResult(CoderResult result) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   257
        check(result.isUnderflow(),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   258
              "Unexpected coder result: " + result);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   259
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   260
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   261
    static void test(String expectedCharset, byte[] input) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   262
        Charset cs = Charset.forName("x-JISAutoDetect");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   263
        CharsetDecoder autoDetect = cs.newDecoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   264
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   265
        Charset cs2 = Charset.forName(expectedCharset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   266
        CharsetDecoder decoder = cs2.newDecoder();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   267
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   268
        ByteBuffer bb = ByteBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   269
        CharBuffer charOutput = CharBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   270
        CharBuffer charExpected = CharBuffer.allocate(128);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   271
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   272
        bb.put(input);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   273
        bb.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   274
        bb.mark();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   275
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   276
        CoderResult result = autoDetect.decode(bb, charOutput, true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   277
        checkCoderResult(result);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   278
        charOutput.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   279
        String actual = charOutput.toString();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   280
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   281
        bb.reset();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   282
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   283
        result = decoder.decode(bb, charExpected, true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   284
        checkCoderResult(result);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   285
        charExpected.flip();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   286
        String expected = charExpected.toString();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   287
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   288
        check(actual.equals(expected),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   289
              String.format("actual=%s expected=%s", actual, expected));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   290
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   291
}