jdk/src/share/classes/sun/nio/cs/ext/ISO2022_CN.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2913 39a9cc073b84
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 2003-2006 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package sun.nio.cs.ext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.charset.CharacterCodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.nio.cs.HistoricallyNamedCharset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.nio.cs.US_ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class ISO2022_CN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    implements HistoricallyNamedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final byte ISO_ESC = 0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final byte ISO_SI = 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final byte ISO_SO = 0x0e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final byte ISO_SS2_7 = 0x4e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final byte ISO_SS3_7 = 0x4f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final byte MSB = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final char REPLACE_CHAR = '\uFFFD';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final byte SODesigGB = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final byte SODesigCNS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public ISO2022_CN() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        super("ISO-2022-CN", ExtendedCharsets.aliasesFor("ISO-2022-CN"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public String historicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return "ISO2022CN";
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 boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return ((cs instanceof EUC_CN)     // GB2312-80 repertoire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                || (cs instanceof US_ASCII)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                || (cs instanceof EUC_TW)  // CNS11643 repertoire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                || (cs instanceof ISO2022_CN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return new Decoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public boolean canEncode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static class Decoder extends CharsetDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        private boolean shiftOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        private byte currentSODesig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        private static final Charset gb2312 = new EUC_CN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        private static final Charset cns = new EUC_TW();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        private final EUC_CN.Decoder gb2312Decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        private final EUC_TW.Decoder cnsDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Decoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            super(cs, 1.0f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            shiftOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            gb2312Decoder = (EUC_CN.Decoder)gb2312.newDecoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            cnsDecoder = (EUC_TW.Decoder)cns.newDecoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        protected void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            shiftOut= false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        private char cnsDecode(byte byte1, byte byte2, byte SS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            byte1 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            byte2 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (SS == ISO_SS2_7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return cnsDecoder.convToUnicode(byte1, byte2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                                cnsDecoder.unicodeCNS2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else { //SS == ISO_SS3_7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                char[] outSurr = cnsDecoder.convToSurrogate(byte1, byte2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                                            cnsDecoder.unicodeCNS3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (outSurr == null || outSurr[0] != '\u0000')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    return REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return outSurr[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        private char SODecode(byte byte1, byte byte2, byte SOD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            byte1 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            byte2 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (SOD == SODesigGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                return gb2312Decoder.decodeDouble(byte1 & 0xff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                                  byte2 & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } else {    // SOD == SODesigCNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return cnsDecoder.convToUnicode(byte1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                                byte2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                                cnsDecoder.unicodeCNS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        private CoderResult decodeBufferLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                             CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            char c = REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    b1 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    while (b1 == ISO_ESC ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                           b1 == ISO_SO ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                           b1 == ISO_SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        if (b1 == ISO_ESC) {  // ESC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                            currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                            if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                            b2 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            if ((b2 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            if (b2 == (byte)0x24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                b3 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                if ((b3 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                    return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                if (b3 == 'A'){              // "$A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                    currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                } else if (b3 == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                    if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                    b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                    if (b4 == 'A'){          // "$)A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                        currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                    } else if (b4 == 'G'){   // "$)G"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                        currentSODesig = SODesigCNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                } else if (b3 == '*') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                    if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                    b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                    if (b4 != 'H') {         // "$*H"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                        //SS2Desig -> CNS-P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                } else if (b3 == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                    if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                    b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                    if (b4 != 'I'){          // "$+I"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                        //SS3Desig -> CNS-P2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            } else if (b2 == ISO_SS2_7 || b2 == ISO_SS3_7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                if (src.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                b3 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                inputSize += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                //SS2->CNS-P2, SS3->CNS-P3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                c = cnsDecode(b3, b4, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                    return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                dst.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        } else if (b1 == ISO_SO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            shiftOut = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        } else if (b1 == ISO_SI) { // shift back in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                            shiftOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        b1 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    if (!shiftOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        dst.put((char)(b1 & 0xff));  //clear the upper byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        b2 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        c = SODecode(b1, b2, currentSODesig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        dst.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        private CoderResult decodeArrayLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                            CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            char c = REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    b1 = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    while (b1 == ISO_ESC || b1 == ISO_SO || b1 == ISO_SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        if (b1 == ISO_ESC) {  // ESC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                            currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                            if (sp + 2 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            b2 = sa[sp + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            if ((b2 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            if (b2 == (byte)0x24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                if (sp + 3 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                b3 = sa[sp + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                if ((b3 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                    return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                if (b3 == 'A'){              // "$A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                    /* <ESC>$A is not a legal designator sequence for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                       ISO2022_CN, it is listed as an escape sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                       for GB2312 in ISO2022-JP-2. Keep it here just for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                       the sake of "compatibility".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                    currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                } else if (b3 == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                    if (sp + 4 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                    b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                    if (b4 == 'A'){          // "$)A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                        currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                    } else if (b4 == 'G'){   // "$)G"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                        currentSODesig = SODesigCNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                } else if (b3 == '*') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                    if (sp + 4 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                    b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                    if (b4 != 'H'){          // "$*H"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                } else if (b3 == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                    if (sp + 4 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                    b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                    if (b4 != 'I'){          // "$+I"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                            } else if (b2 == ISO_SS2_7 || b2 == ISO_SS3_7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                if (sp + 4 > sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                b3 = sa[sp + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                if (dl - dp < 1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                inputSize += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                c = cnsDecode(b3, b4, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                    return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                da[dp++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        } else if (b1 == ISO_SO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                            shiftOut = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        } else if (b1 == ISO_SI) { // shift back in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                            shiftOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        sp += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        if (sp + 1 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        b1 = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    if (dl - dp < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    if (!shiftOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        da[dp++] = (char)(b1 & 0xff);  //clear the upper byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        if (sp + 2 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        b2 = sa[sp + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        c = SODecode(b1, b2, currentSODesig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        da[dp++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    sp += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        protected CoderResult decodeLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                         CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                return decodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                return decodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}