jdk/src/share/classes/sun/io/ByteToCharISO2022CN.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2001 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Tom Zhou
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class ByteToCharISO2022CN extends ByteToCharConverter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private String SODesignator[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private String SS2Designator[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private String SS3Designator[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private ByteToCharConverter SOConverter[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private ByteToCharConverter SS2Converter[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private ByteToCharConverter SS3Converter[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final byte ISO_ESC = 0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final byte ISO_SI = 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final byte ISO_SO = 0x0e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final byte ISO_SS2_7 = 0x4e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final byte ISO_SS3_7 = 0x4f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final byte MSB = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final char REPLACE_CHAR = '\uFFFD';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final byte maximumDesignatorLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final byte SOFlag = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final byte SS2Flag = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final byte SS3Flag = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final byte G0 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final byte G1 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private ByteToCharConverter tmpConverter[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int curSODes, curSS2Des, curSS3Des;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean shiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private byte remainByte[] = new byte[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int remainIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private byte state, firstByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        shiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        state = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        firstByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        curSODes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        curSS2Des = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        curSS3Des = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        charOff = byteOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        remainIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        for(i = 0; i < remainByte.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            remainByte[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public int flush(char[] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        throws MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (state != G0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            badInputLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private byte[] savetyGetSrc(byte[] input, int inOff, int inEnd, int nbytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        byte tmp[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if(inOff <= (inEnd-nbytes+1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            tmp = new byte[nbytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            tmp = new byte[inEnd-inOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for(i = 0; i < tmp.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            tmp[i] = input[inOff+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private char getUnicode(byte byte1, byte byte2, byte shiftFlag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        byte1 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        byte2 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        char[] tmpChar = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        switch(shiftFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        case SOFlag:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                byte[] tmpByte = {byte1,byte2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                SOConverter[curSODes].convert(tmpByte, 0, 2, tmpChar, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        case SS2Flag:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                byte[] tmpByte = {(byte)0x8e, (byte)0xa2, byte1, byte2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                SS2Converter[curSS2Des].convert(tmpByte, 0, 4, tmpChar, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            catch (Exception e){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        case SS3Flag:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                byte[] tmpByte = {(byte)0x8e, (byte)0xa3, byte1,byte2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                SS3Converter[curSS3Des].convert(tmpByte, 0, 4, tmpChar, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            catch (Exception e){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            tmpChar[0] = REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return tmpChar[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public final int convert(byte[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                             char[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                             throws ConversionBufferFullException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                    MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int DesignatorLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        charOff  =  outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        byteOff  =  inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // Loop until we hit the end of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        while (byteOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            // If we don't have room for the output, throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (charOff >= outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if(remainIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                remainByte[0] = input[byteOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                remainIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                byteOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            switch (remainByte[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            case ISO_SO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                shiftout = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if(remainIndex > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    System.arraycopy(remainByte, 1, remainByte, 0, remainIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                remainIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            case ISO_SI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                shiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if(remainIndex > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    System.arraycopy(remainByte, 1, remainByte, 0, remainIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                remainIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
             case ISO_ESC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                byte tmp[] = savetyGetSrc(input, byteOff, inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                               (maximumDesignatorLength-remainIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                System.arraycopy(tmp, 0, remainByte, remainIndex+1, tmp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                remainIndex += tmp.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                byteOff += tmp.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                if(tmp.length<(maximumDesignatorLength-remainIndex))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                String tmpString = new String(remainByte, 1, remainIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                for (i = 0; i < SODesignator.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    if(tmpString.indexOf(SODesignator[i]) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        curSODes = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        DesignatorLength = SODesignator[i].length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (DesignatorLength == 0) { // designator not recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                   badInputLength = tmp.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                   throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (i == SODesignator.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    for (i = 0; i < SS2Designator.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        if(tmpString.indexOf(SS2Designator[i]) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                            curSS2Des = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            DesignatorLength = SS2Designator[i].length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    if(i == SS2Designator.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        for(i = 0; i < SS3Designator.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                            if (tmpString.indexOf(SS3Designator[i]) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                curSS3Des = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                DesignatorLength = SS3Designator[i].length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        if (i == SS3Designator.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                            switch(remainByte[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                            case ISO_SS2_7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                output[charOff] = getUnicode(remainByte[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                                          remainByte[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                                          SS2Flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                charOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                DesignatorLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            case ISO_SS3_7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                output[charOff] = getUnicode(remainByte[2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                                          remainByte[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                                          SS3Flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                charOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                DesignatorLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                DesignatorLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if (remainIndex > DesignatorLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    for(i = 0; i < remainIndex-DesignatorLength; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        remainByte[i] = remainByte[DesignatorLength+1+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    remainIndex = i-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    remainIndex = -1;
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
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if (!shiftout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    output[charOff] = (char)remainByte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    charOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    case G0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        firstByte = remainByte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        state = G1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    case G1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        output[charOff] = getUnicode(firstByte, remainByte[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                                  SOFlag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        charOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        state = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if (remainIndex > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    System.arraycopy(remainByte, 1, remainByte, 0, remainIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                remainIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return charOff - outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public ByteToCharISO2022CN()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        SODesignator = new String[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        SODesignator[0] = "$A";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        SODesignator[1] = "$)A";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        SODesignator[2] = "$)G";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        SS2Designator = new String[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        SS2Designator[0] = "$*H";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        SS3Designator = new String[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        SS3Designator[0] = "$+I";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        SOConverter = new ByteToCharConverter[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        SS2Converter = new ByteToCharConverter[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        SS3Converter = new ByteToCharConverter[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            SOConverter[0] = SOConverter[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        = ByteToCharConverter.getConverter("GB2312");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            SOConverter[2] = SS2Converter[0] = SS3Converter[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        = ByteToCharConverter.getConverter("CNS11643");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } catch (Exception e) {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    // Return the character set id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public String getCharacterEncoding()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return "ISO2022CN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}