jdk/src/share/classes/sun/io/ByteToCharEUC_TW.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2913 39a9cc073b84
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2913
diff changeset
     2
 * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2913
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2913
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2913
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2913
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2913
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.nio.cs.ext.EUC_TW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author Limin Shi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class ByteToCharEUC_TW extends ByteToCharConverter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private final byte G0 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private final byte G1 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private final byte G2 = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private final byte G3 = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private final byte G4 = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private final byte MSB = (byte) 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final byte SS2 = (byte) 0x8E;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private byte firstByte = 0, state = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private int cnsPlane = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    46
    private EUC_TW.Decoder dec = (EUC_TW.Decoder)(new EUC_TW().newDecoder());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public ByteToCharEUC_TW() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public int flush(char[] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        throws MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (state != G0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            state = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            firstByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            badInputLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void reset() {
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    65
        dec.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        state = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        firstByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Character conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public int convert(byte[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                       char[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        throws UnknownCharacterException, MalformedInputException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int inputSize = 0;
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    80
        char[] c1 = new char[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        byteOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        charOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        cnsPlane = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        while (byteOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (charOff >= outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                throw new ConversionBufferFullException();
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    89
            char[] outputChar = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            case G0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if ( (input[byteOff] & MSB) == 0) {     // ASCII
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    93
                    outputChar = c1;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    94
                    outputChar[0] = (char) input[byteOff];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                } else if (input[byteOff] == SS2) {     // Codeset 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    state = G2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                } else {                                // Codeset 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    firstByte = input[byteOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    state = G1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            case G1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                inputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                if ( (input[byteOff] & MSB) != 0) {     // 2nd byte
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   105
                    cnsPlane = 0;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   106
                    outputChar = dec.toUnicode(firstByte & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   107
                                               input[byteOff] & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   108
                                               cnsPlane);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                } else {                                // Error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                firstByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                state = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            case G2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                cnsPlane = (input[byteOff] & (byte)0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                // Adjust String array index for plan 15
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                cnsPlane = (cnsPlane == 15)? 8 : cnsPlane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                if (cnsPlane < 15) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                     state = G3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    badInputLength = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            case G3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if ( (input[byteOff] & MSB) != 0) {     // 1st byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    firstByte = input[byteOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    state = G4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                } else {                                // Error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    state = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    badInputLength = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            case G4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if ( (input[byteOff] & MSB) != 0) {     // 2nd byte
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   141
                    outputChar = dec.toUnicode(firstByte & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   142
                                               input[byteOff] & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   143
                                               cnsPlane - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                } else {                                // Error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    badInputLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                firstByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                state = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            byteOff++;
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   153
            if (state == G0) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   154
                if (outputChar == null) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   155
                    if (subMode) {               // substitution enabled
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   156
                        outputChar = c1;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   157
                        outputChar[0] = subChars[0];
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   158
                    } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        badInputLength = inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   163
                output[charOff++] = outputChar[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return charOff - outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Return the character set ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public String getCharacterEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return "EUC_TW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}