jdk/src/share/classes/sun/nio/cs/ext/ISO2022_CN.java
author sherman
Tue, 19 May 2009 15:25:29 -0700
changeset 2913 39a9cc073b84
parent 2 90ce3da70b43
child 3052 5c9886498f31
permissions -rw-r--r--
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar 6229811: Several codepoints in EUC_TW failed in roundtrip conversion Summary: Re-write EUC_TW charset to address the size and roundtrip issue. Reviewed-by: alanb
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;
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   108
            int p = 0;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   109
            if (SS == ISO_SS2_7)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   110
                p = 1;    //plane 2, index -- 1
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   111
            else if (SS == ISO_SS3_7)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   112
                p = 2;    //plane 3, index -- 2
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   113
            else
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   114
                return REPLACE_CHAR;  //never happen.
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   115
            char[] ret = cnsDecoder.toUnicode(byte1 & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   116
                                              byte2 & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   117
                                              p);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   118
            if (ret == null || ret.length == 2)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   119
                return REPLACE_CHAR;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   120
            return ret[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        private char SODecode(byte byte1, byte byte2, byte SOD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            byte1 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            byte2 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (SOD == SODesigGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return gb2312Decoder.decodeDouble(byte1 & 0xff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                                  byte2 & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else {    // SOD == SODesigCNS
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   130
                char[] ret = cnsDecoder.toUnicode(byte1 & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   131
                                                  byte2 & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   132
                                                  0);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   133
                if (ret == null)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   134
                    return REPLACE_CHAR;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   135
                return ret[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        private CoderResult decodeBufferLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                             CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            char c = REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    b1 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    while (b1 == ISO_ESC ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                           b1 == ISO_SO ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                           b1 == ISO_SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        if (b1 == ISO_ESC) {  // ESC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                            currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            b2 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            if ((b2 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            if (b2 == (byte)0x24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                b3 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                if ((b3 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                    return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                if (b3 == 'A'){              // "$A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                    currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                } else if (b3 == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                    if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                    b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                    if (b4 == 'A'){          // "$)A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                        currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                    } else if (b4 == 'G'){   // "$)G"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                        currentSODesig = SODesigCNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                } else if (b3 == '*') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                    if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                    b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                    if (b4 != 'H') {         // "$*H"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                        //SS2Desig -> CNS-P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                } else if (b3 == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                    if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                    b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                    if (b4 != 'I'){          // "$+I"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                        //SS3Desig -> CNS-P2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                            } else if (b2 == ISO_SS2_7 || b2 == ISO_SS3_7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                if (src.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                b3 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                inputSize += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                //SS2->CNS-P2, SS3->CNS-P3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                c = cnsDecode(b3, b4, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                    return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                dst.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        } else if (b1 == ISO_SO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            shiftOut = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        } else if (b1 == ISO_SI) { // shift back in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                            shiftOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        b1 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        inputSize = 1;
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 (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    if (!shiftOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        dst.put((char)(b1 & 0xff));  //clear the upper byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        b2 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        c = SODecode(b1, b2, currentSODesig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        dst.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        private CoderResult decodeArrayLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                            CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            char c = REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    b1 = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    while (b1 == ISO_ESC || b1 == ISO_SO || b1 == ISO_SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        if (b1 == ISO_ESC) {  // ESC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            if (sp + 2 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            b2 = sa[sp + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            if ((b2 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            if (b2 == (byte)0x24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                if (sp + 3 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                b3 = sa[sp + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                if ((b3 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                    return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                if (b3 == 'A'){              // "$A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                    /* <ESC>$A is not a legal designator sequence for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                       ISO2022_CN, it is listed as an escape sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                       for GB2312 in ISO2022-JP-2. Keep it here just for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                       the sake of "compatibility".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                    currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                } else if (b3 == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                    if (sp + 4 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                    b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                    if (b4 == 'A'){          // "$)A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                        currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                    } else if (b4 == 'G'){   // "$)G"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                        currentSODesig = SODesigCNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                } else if (b3 == '*') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                    if (sp + 4 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                    b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                    if (b4 != 'H'){          // "$*H"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                } else if (b3 == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                    if (sp + 4 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                    b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                    if (b4 != 'I'){          // "$+I"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                            } else if (b2 == ISO_SS2_7 || b2 == ISO_SS3_7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                if (sp + 4 > sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                b3 = sa[sp + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                if (dl - dp < 1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                inputSize += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                c = cnsDecode(b3, b4, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                    return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                da[dp++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        } else if (b1 == ISO_SO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            shiftOut = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        } else if (b1 == ISO_SI) { // shift back in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            shiftOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        sp += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        if (sp + 1 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        b1 = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    if (dl - dp < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    if (!shiftOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        da[dp++] = (char)(b1 & 0xff);  //clear the upper byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        if (sp + 2 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        b2 = sa[sp + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        c = SODecode(b1, b2, currentSODesig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                            return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        da[dp++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    sp += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        protected CoderResult decodeLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                         CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                return decodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                return decodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
}