jdk/src/share/classes/sun/nio/cs/ext/ISO2022.java
author sherman
Tue, 19 May 2009 15:25:29 -0700
changeset 2913 39a9cc073b84
parent 2 90ce3da70b43
child 3623 4e71b4e83158
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 2002-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 sun.nio.cs.Surrogate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
abstract class ISO2022
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final byte ISO_ESC = 0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final byte ISO_SI = 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final byte ISO_SO = 0x0e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final byte ISO_SS2_7 = 0x4e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final byte ISO_SS3_7 = 0x4f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final byte MSB = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final char REPLACE_CHAR = '\uFFFD';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final byte minDesignatorLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public ISO2022(String csname, String[] aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super(csname, aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return new Decoder(this);
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 CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return new Encoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected static class Decoder extends CharsetDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Value to be filled by subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        protected byte SODesig[][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        protected byte SS2Desig[][] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        protected byte SS3Desig[][] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        protected CharsetDecoder SODecoder[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        protected CharsetDecoder SS2Decoder[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        protected CharsetDecoder SS3Decoder[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        private static final byte SOFlag = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        private static final byte SS2Flag = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        private static final byte SS3Flag = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        private int curSODes, curSS2Des, curSS3Des;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        private boolean shiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        private CharsetDecoder tmpDecoder[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        protected Decoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            super(cs, 1.0f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        protected void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            curSODes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            curSS2Des = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            curSS3Des = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            shiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        private char decode(byte byte1, byte byte2, byte shiftFlag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            byte1 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            byte2 |= MSB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            byte[] tmpByte = { byte1,byte2 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            char[] tmpChar = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            int     i = 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    tmpIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            switch(shiftFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            case SOFlag:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                tmpIndex = curSODes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                tmpDecoder = (CharsetDecoder [])SODecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            case SS2Flag:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                tmpIndex = curSS2Des;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                tmpDecoder = (CharsetDecoder [])SS2Decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            case SS3Flag:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                tmpIndex = curSS3Des;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                tmpDecoder = (CharsetDecoder [])SS3Decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (tmpDecoder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                for(i = 0; i < tmpDecoder.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    if(tmpIndex == i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            ByteBuffer bb = ByteBuffer.wrap(tmpByte,0,2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            CharBuffer cc = CharBuffer.wrap(tmpChar,0,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                            tmpDecoder[i].decode(bb, cc, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            cc.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            return cc.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        private int findDesig(byte[] in, int sp, int sl, byte[][] desigs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (desigs == null) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            while (i < desigs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                if (desigs[i] != null && sl - sp >= desigs[i].length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    while (j < desigs[i].length && in[sp+j] == desigs[i][j]) { j++; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    if (j == desigs[i].length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        private int findDesigBuf(ByteBuffer in, byte[][] desigs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (desigs == null) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            while (i < desigs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (desigs[i] != null && in.remaining() >= desigs[i].length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    in.mark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    while (j < desigs[i].length && in.get() == desigs[i][j]) { j++; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    if (j == desigs[i].length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    in.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        private CoderResult decodeArrayLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                            CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            int b1 = 0, b2 = 0, b3 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    b1 = sa[sp] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    int inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    switch (b1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        case ISO_SO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            shiftout = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                            inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        case ISO_SI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                            shiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                            inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        case ISO_ESC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                            if (sl - sp - 1 < minDesignatorLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            int desig = findDesig(sa, sp + 1, sl, SODesig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                            if (desig != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                curSODes = desig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                inputSize = SODesig[desig].length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                            desig = findDesig(sa, sp + 1, sl, SS2Desig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                            if (desig != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                curSS2Des = desig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                inputSize = SS2Desig[desig].length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                            desig = findDesig(sa, sp + 1, sl, SS3Desig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                            if (desig != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                curSS3Des = desig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                inputSize = SS3Desig[desig].length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                            if (sl - sp < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            b1 = sa[sp + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            switch(b1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            case ISO_SS2_7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                if (sl - sp < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                b2 = sa[sp +2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                b3 = sa[sp +3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                if (dl - dp <1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                da[dp] = decode((byte)b2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                                (byte)b3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                                SS2Flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                dp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                inputSize = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                            case ISO_SS3_7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                if (sl - sp < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                b2 = sa[sp + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                b3 = sa[sp + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                if (dl - dp <1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                da[dp] = decode((byte)b2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                                (byte)b3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                                SS3Flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                dp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                inputSize = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                return CoderResult.malformedForLength(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            if (dl - dp < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            if (!shiftout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                da[dp++]=(char)(sa[sp] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                if (dl - dp < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                if (sl - sp < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                b2 = sa[sp+1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                da[dp++] = decode((byte)b1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                                  (byte)b2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                   SOFlag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                inputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    sp += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        private CoderResult decodeBufferLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                             CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            int b1 = 0, b2 = 0, b3 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    b1 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    int inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    switch (b1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        case ISO_SO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            shiftout = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        case ISO_SI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            shiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        case ISO_ESC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            if (src.remaining() < minDesignatorLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            int desig = findDesigBuf(src, SODesig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            if (desig != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                curSODes = desig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                inputSize = SODesig[desig].length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                            desig = findDesigBuf(src, SS2Desig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            if (desig != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                curSS2Des = desig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                inputSize = SS2Desig[desig].length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                            desig = findDesigBuf(src, SS3Desig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                            if (desig != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                curSS3Des = desig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                inputSize = SS3Desig[desig].length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                            if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                            b1 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                            switch(b1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                case ISO_SS2_7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                    if (src.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                    b2 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                    b3 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                    if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                    dst.put(decode((byte)b2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                                   (byte)b3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                                   SS2Flag));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                    inputSize = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                case ISO_SS3_7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                    if (src.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                    b2 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                    b3 = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                    if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                    dst.put(decode((byte)b2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                                   (byte)b3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                                   SS3Flag));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                    inputSize = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                    return CoderResult.malformedForLength(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                            if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            if (!shiftout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                dst.put((char)(b1 & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                    return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                b2 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                dst.put(decode((byte)b1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                                      (byte)b2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                                        SOFlag));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                inputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            } catch (Exception e) { e.printStackTrace(); return CoderResult.OVERFLOW; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        protected CoderResult decodeLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                         CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                return decodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                return decodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    protected static class Encoder extends CharsetEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        private final Surrogate.Parser sgp = new Surrogate.Parser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        private final byte SS2 = (byte)0x8e;
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   392
        private final byte PLANE2 = (byte)0xA2;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   393
        private final byte PLANE3 = (byte)0xA3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        private final byte MSB = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        protected final byte maximumDesignatorLength = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        protected String SODesig,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                         SS2Desig = null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                         SS3Desig = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        protected CharsetEncoder ISOEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        private boolean shiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        private boolean SODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        private boolean SS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        private boolean SS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        private boolean newshiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        private boolean newSODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        private boolean newSS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        private boolean newSS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        protected Encoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            super(cs, 4.0f, 8.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        public boolean canEncode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return (ISOEncoder.canEncode(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        protected void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            shiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            SODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            SS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            SS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        private int unicodeToNative(char unicode, byte ebyte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            byte        tmpByte[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            char        convChar[] = {unicode};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            byte        convByte[] = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            int         converted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                CharBuffer cc = CharBuffer.wrap(convChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                ByteBuffer bb = ByteBuffer.allocate(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                ISOEncoder.encode(cc, bb, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                converted = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                bb.get(convByte,0,converted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (converted == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                if (!SODesDefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    newSODesDefined = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    ebyte[0] = ISO_ESC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    tmpByte = SODesig.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    System.arraycopy(tmpByte,0,ebyte,1,tmpByte.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    index = tmpByte.length+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                if (!shiftout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    newshiftout = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    ebyte[index++] = ISO_SO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                ebyte[index++] = (byte)(convByte[0] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                ebyte[index++] = (byte)(convByte[1] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            } else {
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   463
                if(convByte[0] == SS2) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   464
                    if (convByte[1] == PLANE2) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   465
                        if (!SS2DesDefined) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   466
                            newSS2DesDefined = true;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   467
                            ebyte[0] = ISO_ESC;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   468
                            tmpByte = SS2Desig.getBytes();
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   469
                            System.arraycopy(tmpByte, 0, ebyte, 1, tmpByte.length);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   470
                            index = tmpByte.length+1;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   471
                        }
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   472
                        ebyte[index++] = ISO_ESC;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   473
                        ebyte[index++] = ISO_SS2_7;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   474
                        ebyte[index++] = (byte)(convByte[2] & 0x7f);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   475
                        ebyte[index++] = (byte)(convByte[3] & 0x7f);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   476
                    } else if (convByte[1] == PLANE3) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   477
                        if(!SS3DesDefined){
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   478
                            newSS3DesDefined = true;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   479
                            ebyte[0] = ISO_ESC;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   480
                            tmpByte = SS3Desig.getBytes();
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   481
                            System.arraycopy(tmpByte, 0, ebyte, 1, tmpByte.length);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   482
                            index = tmpByte.length+1;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   483
                        }
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   484
                        ebyte[index++] = ISO_ESC;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   485
                        ebyte[index++] = ISO_SS3_7;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   486
                        ebyte[index++] = (byte)(convByte[2] & 0x7f);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   487
                        ebyte[index++] = (byte)(convByte[3] & 0x7f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        private CoderResult encodeArrayLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                            ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            char[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            byte[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            int outputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            byte[]  outputByte = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            newshiftout = shiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            newSODesDefined = SODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            newSS2DesDefined = SS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            newSS3DesDefined = SS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    char c = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    if (Surrogate.is(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        if (sgp.parse(c, sa, sp, sl) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                            return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    if (c < 0x80) {     // ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        if (shiftout){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                            newshiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                            outputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                            outputByte[0] = ISO_SI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                            outputByte[1] = (byte)(c & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                            outputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                            outputByte[0] = (byte)(c & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        if(sa[sp] == '\n'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                            newSODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                            newSS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                            newSS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        outputSize = unicodeToNative(c, outputByte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        if (outputSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                            return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    if (dl - dp < outputSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    for (int i = 0; i < outputSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        da[dp++] = outputByte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    sp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    shiftout = newshiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    SODesDefined = newSODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    SS2DesDefined = newSS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    SS3DesDefined = newSS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
             } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        private CoderResult encodeBufferLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                                             ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            int outputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            byte[]  outputByte = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            int     inputSize = 0;                 // Size of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            newshiftout = shiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            newSODesDefined = SODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            newSS2DesDefined = SS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            newSS3DesDefined = SS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    char inputChar = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    if (Surrogate.is(inputChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        if (sgp.parse(inputChar, src) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    if (inputChar < 0x80) {     // ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        if (shiftout){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                            newshiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                            outputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                            outputByte[0] = ISO_SI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                            outputByte[1] = (byte)(inputChar & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                            outputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                            outputByte[0] = (byte)(inputChar & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        if(inputChar == '\n'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                            newSODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                            newSS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                            newSS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        outputSize = unicodeToNative(inputChar, outputByte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        if (outputSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                            return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    if (dst.remaining() < outputSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    for (int i = 0; i < outputSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        dst.put(outputByte[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    shiftout = newshiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    SODesDefined = newSODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    SS2DesDefined = newSS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    SS3DesDefined = newSS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        protected CoderResult encodeLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                         ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                return encodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                return encodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
}