src/jdk.charsets/share/classes/sun/nio/cs/ext/ISO2022_CN.java
author mgronlun
Wed, 30 Oct 2019 11:28:38 +0100
branchJEP-349-branch
changeset 58854 a79e68ec829a
parent 47216 71c04702a3d5
permissions -rw-r--r--
stricly monotone nanos for chunkheader
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
     2
 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3052
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
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;
28969
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
    38
import sun.nio.cs.DoubleByte;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.nio.cs.HistoricallyNamedCharset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.nio.cs.US_ASCII;
28969
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
    41
import sun.nio.cs.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class ISO2022_CN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    implements HistoricallyNamedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final byte ISO_ESC = 0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final byte ISO_SI = 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final byte ISO_SO = 0x0e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final byte ISO_SS2_7 = 0x4e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final byte ISO_SS3_7 = 0x4f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final byte MSB = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final char REPLACE_CHAR = '\uFFFD';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final byte SODesigGB = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final byte SODesigCNS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public ISO2022_CN() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        super("ISO-2022-CN", ExtendedCharsets.aliasesFor("ISO-2022-CN"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public String historicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return "ISO2022CN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return ((cs instanceof EUC_CN)     // GB2312-80 repertoire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                || (cs instanceof US_ASCII)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                || (cs instanceof EUC_TW)  // CNS11643 repertoire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                || (cs instanceof ISO2022_CN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return new Decoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public boolean canEncode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static class Decoder extends CharsetDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        private boolean shiftOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        private byte currentSODesig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        private static final Charset gb2312 = new EUC_CN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        private static final Charset cns = new EUC_TW();
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2913
diff changeset
    91
        private final DoubleByte.Decoder gb2312Decoder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        private final EUC_TW.Decoder cnsDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        Decoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            super(cs, 1.0f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            shiftOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            currentSODesig = SODesigGB;
3052
5c9886498f31 6299219: euro sign failed to be printed in Console on Localized Windows platform with GBK encoding
sherman
parents: 2913
diff changeset
    98
            gb2312Decoder = (DoubleByte.Decoder)gb2312.newDecoder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            cnsDecoder = (EUC_TW.Decoder)cns.newDecoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        protected void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            shiftOut= false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        private char cnsDecode(byte byte1, byte byte2, byte SS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            byte1 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            byte2 |= MSB;
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   110
            int p = 0;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   111
            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
   112
                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
   113
            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
   114
                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
   115
            else
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   116
                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
   117
            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
   118
                                              byte2 & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   119
                                              p);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   120
            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
   121
                return REPLACE_CHAR;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   122
            return ret[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        private char SODecode(byte byte1, byte byte2, byte SOD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            byte1 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            byte2 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (SOD == SODesigGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                return gb2312Decoder.decodeDouble(byte1 & 0xff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                                  byte2 & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else {    // SOD == SODesigCNS
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   132
                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
   133
                                                  byte2 & 0xff,
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   134
                                                  0);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   135
                if (ret == null)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   136
                    return REPLACE_CHAR;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   137
                return ret[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        private CoderResult decodeBufferLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                             CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            char c = REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    b1 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    while (b1 == ISO_ESC ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                           b1 == ISO_SO ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                           b1 == ISO_SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        if (b1 == ISO_ESC) {  // ESC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                            if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            b2 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            if ((b2 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                            if (b2 == (byte)0x24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                b3 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                if ((b3 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                    return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                if (b3 == 'A'){              // "$A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                    currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                } else if (b3 == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                    if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                    b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                    if (b4 == 'A'){          // "$)A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                        currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                    } else if (b4 == 'G'){   // "$)G"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                        currentSODesig = SODesigCNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                } else if (b3 == '*') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                    if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                    b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                    if (b4 != 'H') {         // "$*H"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                        //SS2Desig -> CNS-P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                } else if (b3 == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                    if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                    b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                    if (b4 != 'I'){          // "$+I"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                        //SS3Desig -> CNS-P2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            } else if (b2 == ISO_SS2_7 || b2 == ISO_SS3_7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                if (src.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                b3 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                b4 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                inputSize += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                //SS2->CNS-P2, SS3->CNS-P3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                c = cnsDecode(b3, b4, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                    return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                dst.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        } else if (b1 == ISO_SO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                            shiftOut = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        } else if (b1 == ISO_SI) { // shift back in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                            shiftOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        b1 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    if (!shiftOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        dst.put((char)(b1 & 0xff));  //clear the upper byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        b2 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        c = SODecode(b1, b2, currentSODesig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                            return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        dst.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        private CoderResult decodeArrayLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                            CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            char c = REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    b1 = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    while (b1 == ISO_ESC || b1 == ISO_SO || b1 == ISO_SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        if (b1 == ISO_ESC) {  // ESC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            if (sp + 2 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            b2 = sa[sp + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            if ((b2 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                            if (b2 == (byte)0x24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                if (sp + 3 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                b3 = sa[sp + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                if ((b3 & (byte)0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                    return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                if (b3 == 'A'){              // "$A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                    /* <ESC>$A is not a legal designator sequence for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                       ISO2022_CN, it is listed as an escape sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                       for GB2312 in ISO2022-JP-2. Keep it here just for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                       the sake of "compatibility".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                    currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                } else if (b3 == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                    if (sp + 4 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                    b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                    if (b4 == 'A'){          // "$)A"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                        currentSODesig = SODesigGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                    } else if (b4 == 'G'){   // "$)G"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                        currentSODesig = SODesigCNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                } else if (b3 == '*') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                    if (sp + 4 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                    b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                    if (b4 != 'H'){          // "$*H"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                } else if (b3 == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                    if (sp + 4 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                    b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                    if (b4 != 'I'){          // "$+I"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                            } else if (b2 == ISO_SS2_7 || b2 == ISO_SS3_7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                if (sp + 4 > sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                b3 = sa[sp + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                b4 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                if (dl - dp < 1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                inputSize += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                c = cnsDecode(b3, b4, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                    return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                da[dp++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        } else if (b1 == ISO_SO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            shiftOut = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        } else if (b1 == ISO_SI) { // shift back in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                            shiftOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        sp += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        if (sp + 1 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        b1 = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    if (dl - dp < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    if (!shiftOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        da[dp++] = (char)(b1 & 0xff);  //clear the upper byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        if (sp + 2 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        b2 = sa[sp + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        c = SODecode(b1, b2, currentSODesig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        if (c == REPLACE_CHAR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                            return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        da[dp++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    sp += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        protected CoderResult decodeLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                         CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                return decodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                return decodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
}