jdk/src/share/classes/sun/io/ByteToCharISO2022JP.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5785 5dfabe612d10
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5785
diff changeset
     2
 * Copyright (c) 1996, 2010, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
public class ByteToCharISO2022JP extends ByteToCharJIS0208 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
    private static final int ASCII = 0;                 // ESC ( B
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    private static final int JISX0201_1976 = 1;         // ESC ( J
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    private static final int JISX0208_1978 = 2;         // ESC $ @
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private static final int JISX0208_1983 = 3;         // ESC $ B
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static final int JISX0201_1976_KANA = 4;    // ESC ( I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static final int SHIFTOUT = 5;              // SO (0x0e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private int currentState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private int savedSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private byte[] savedBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public ByteToCharISO2022JP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        savedBytes = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        currentState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        savedSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public int flush(char [] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        throws MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (savedSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            savedSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            currentState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            badInputLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Character conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public int convert(byte[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                       char[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        throws UnknownCharacterException, MalformedInputException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        int previousState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        char outputChar = '\uFFFD';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // readOff keeps the actual buffer's pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // byteOff keeps original buffer's pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        int readOff = byteOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (savedSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (savedBytes[0] == 0x1b) { // ESC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if ((savedSize == 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                     (savedBytes[1] == 0x28 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                      input[0] != 'B' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                      input[0] != 'J' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                      input[0] != 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                     (savedBytes[1] == 0x24 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                      input[0] != '@' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                      input[0] != 'B')) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    ((savedSize == 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                     (input[0] != 0x28 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                      input[0] != 0x24))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    badInputLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if ((inEnd - inOff) == 1 && savedSize == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    savedBytes[0] == 0x1b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    savedSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    savedBytes[1] = input[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    byteOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            byte[] newBuf = new byte[inEnd - inOff + savedSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            for (int i = 0; i < savedSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                newBuf[i] = savedBytes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            System.arraycopy(input, inOff, newBuf, savedSize, inEnd - inOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            byteOff -= savedSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            input = newBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            inOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            inEnd = newBuf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            savedSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        charOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        readOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        while(readOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int byte1, byte2, byte3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            boolean noOutput = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // Is there room in the output buffer for the result?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (charOff >= outEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // Get the input byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            byte1 = input[readOff] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if ((byte1 & (byte)0x80) != 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // Is this a escape sequence?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            while (byte1 == 0x1b || byte1 == 0x0e || byte1 == 0x0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (byte1 == 0x1b){  // ESC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    if (readOff + inputSize + 1 >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        if (readOff + inputSize >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                            savedSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                            savedBytes[0] = (byte)byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            savedSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                            savedBytes[0] = (byte)byte1;
5785
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   144
                            savedBytes[1] = input[readOff + inputSize];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    byte2 = input[readOff + inputSize] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    if ((byte2 & (byte)0x80) != 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        badInputLength = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    if (byte2 == 0x28){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        byte3 = input[readOff + inputSize] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        if (byte3 == 'B'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                            currentState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        } else if (byte3 == 'J'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            currentState = JISX0201_1976;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        } else if (byte3 == 'I'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            currentState = JISX0201_1976_KANA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            // illegal ESC sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            badInputLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    } else if (byte2 == '$'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        byte3 = input[readOff + inputSize] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        if ((byte3 & (byte)0x80) != 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            badInputLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                            throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        if (byte3 == '@'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                            currentState = JISX0208_1978;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        } else if (byte3 == 'B'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            currentState = JISX0208_1983;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            // illegal ESC sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            badInputLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        // illegal ESC sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        badInputLength = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    if (readOff + inputSize >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        noOutput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        byte1 = input[readOff + inputSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                } else if (byte1 == 0x0e){  // shift out for one byte kana
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    previousState = currentState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    currentState = SHIFTOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    if (readOff + inputSize >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        noOutput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    byte1 = input[readOff + inputSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    if ((byte1 & (byte)0x80) != 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                } else if (byte1 == 0x0f){  // shift in for previous mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    currentState = previousState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    if (readOff + inputSize >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        noOutput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    byte1 = input[readOff + inputSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    if ((byte1 & (byte)0x80) != 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (noOutput || savedSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            noOutput = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            switch (currentState){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
              case ASCII:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                outputChar = (char)(byte1 & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
              case JISX0201_1976:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                switch (byte1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                  case 0x5c:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    outputChar = '\u00a5';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                  case 0x7e:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    outputChar = '\u203e';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                  default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    outputChar = (char)byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
              case JISX0208_1978:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
              case JISX0208_1983:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                if (readOff + inputSize >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    savedSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    savedBytes[0] = (byte)byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                byte2 = input[readOff + inputSize] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                if ((byte2 & (byte)0x80) != 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                // jisx0208Chars table convert FULLWIDTH_REVERSE_SOLIDUS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                // 0x2140 to REVERSE_SOLIDUS (BACKSLASH) 0x5c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                // This behavior causes problem because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                // 0x5c is special escape character for java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (byte1 == 0x21 && byte2 == 0x40) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    outputChar = '\uFF3C';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        outputChar = getUnicode(byte1, byte2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        outputChar = '\uFFFD';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
              case JISX0201_1976_KANA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
              case SHIFTOUT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if (byte1 > 0x60) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                outputChar = (char)(byte1 + 0xff40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (savedSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (outputChar == '\uFFFD') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (subMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    outputChar = subChars[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    badInputLength = inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            readOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            output[charOff++] = outputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return charOff - outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        currentState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        savedSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public String getCharacterEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return "ISO2022JP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}