src/jdk.charsets/share/classes/sun/nio/cs/ext/ISO2022.java
author coleenp
Wed, 13 Nov 2019 08:23:23 -0500
changeset 59056 15936b142f86
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233913: Remove implicit conversion from Method* to methodHandle Summary: Fix call sites to use existing THREAD local or pass down THREAD local for shallower callsites. Make linkResolver methods return Method* for caller to handleize if needed. Reviewed-by: iklam, thartmann, hseigel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5785
diff changeset
     2
 * Copyright (c) 2002, 2010, 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: 3714
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: 3714
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: 3714
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
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 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;
5785
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   107
                tmpDecoder = SODecoder;
2
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;
5785
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   111
                tmpDecoder = SS2Decoder;
2
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;
5785
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   115
                tmpDecoder = SS3Decoder;
2
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();
3623
4e71b4e83158 6730652: CharsetEncoder.canEncode(char) returns incorrect values for some charsets
sherman
parents: 2913
diff changeset
   391
        public static final byte SS2 = (byte)0x8e;
4e71b4e83158 6730652: CharsetEncoder.canEncode(char) returns incorrect values for some charsets
sherman
parents: 2913
diff changeset
   392
        public static final byte PLANE2 = (byte)0xA2;
4e71b4e83158 6730652: CharsetEncoder.canEncode(char) returns incorrect values for some charsets
sherman
parents: 2913
diff changeset
   393
        public static 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
30787
758feb62a4b0 8080803: sun/nio/cs/FindEncoderBugs.java failing intermittently
sherman
parents: 25859
diff changeset
   398
        protected byte[] SODesig,
2
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
30787
758feb62a4b0 8080803: sun/nio/cs/FindEncoderBugs.java failing intermittently
sherman
parents: 25859
diff changeset
   429
        private int unicodeToNative(char unicode, byte ebyte[]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            char        convChar[] = {unicode};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            byte        convByte[] = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            int         converted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                CharBuffer cc = CharBuffer.wrap(convChar);
30787
758feb62a4b0 8080803: sun/nio/cs/FindEncoderBugs.java failing intermittently
sherman
parents: 25859
diff changeset
   437
                ByteBuffer bb = ByteBuffer.wrap(convByte);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                ISOEncoder.encode(cc, bb, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                converted = bb.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (converted == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if (!SODesDefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    newSODesDefined = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    ebyte[0] = ISO_ESC;
30787
758feb62a4b0 8080803: sun/nio/cs/FindEncoderBugs.java failing intermittently
sherman
parents: 25859
diff changeset
   449
                    System.arraycopy(SODesig, 0, ebyte, 1, SODesig.length);
758feb62a4b0 8080803: sun/nio/cs/FindEncoderBugs.java failing intermittently
sherman
parents: 25859
diff changeset
   450
                    index = SODesig.length + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (!shiftout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    newshiftout = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    ebyte[index++] = ISO_SO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                ebyte[index++] = (byte)(convByte[0] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                ebyte[index++] = (byte)(convByte[1] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            } else {
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   459
                if(convByte[0] == SS2) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   460
                    if (convByte[1] == PLANE2) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   461
                        if (!SS2DesDefined) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   462
                            newSS2DesDefined = true;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   463
                            ebyte[0] = ISO_ESC;
30787
758feb62a4b0 8080803: sun/nio/cs/FindEncoderBugs.java failing intermittently
sherman
parents: 25859
diff changeset
   464
                            System.arraycopy(SS2Desig, 0, ebyte, 1, SS2Desig.length);
758feb62a4b0 8080803: sun/nio/cs/FindEncoderBugs.java failing intermittently
sherman
parents: 25859
diff changeset
   465
                            index = SS2Desig.length + 1;
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   466
                        }
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   467
                        ebyte[index++] = ISO_ESC;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   468
                        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
   469
                        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
   470
                        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
   471
                    } 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
   472
                        if(!SS3DesDefined){
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   473
                            newSS3DesDefined = true;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   474
                            ebyte[0] = ISO_ESC;
30787
758feb62a4b0 8080803: sun/nio/cs/FindEncoderBugs.java failing intermittently
sherman
parents: 25859
diff changeset
   475
                            System.arraycopy(SS3Desig, 0, ebyte, 1, SS3Desig.length);
758feb62a4b0 8080803: sun/nio/cs/FindEncoderBugs.java failing intermittently
sherman
parents: 25859
diff changeset
   476
                            index = SS3Desig.length + 1;
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   477
                        }
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   478
                        ebyte[index++] = ISO_ESC;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   479
                        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
   480
                        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
   481
                        ebyte[index++] = (byte)(convByte[3] & 0x7f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        private CoderResult encodeArrayLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                            ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            char[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            byte[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            int outputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            byte[]  outputByte = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            newshiftout = shiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            newSODesDefined = SODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            newSS2DesDefined = SS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            newSS3DesDefined = SS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    char c = sa[sp];
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3623
diff changeset
   512
                    if (Character.isSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        if (sgp.parse(c, sa, sp, sl) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                            return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    if (c < 0x80) {     // ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        if (shiftout){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                            newshiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                            outputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                            outputByte[0] = ISO_SI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                            outputByte[1] = (byte)(c & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                            outputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                            outputByte[0] = (byte)(c & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        if(sa[sp] == '\n'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                            newSODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                            newSS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                            newSS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        outputSize = unicodeToNative(c, outputByte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        if (outputSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                            return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    if (dl - dp < outputSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    for (int i = 0; i < outputSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        da[dp++] = outputByte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    sp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    shiftout = newshiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    SODesDefined = newSODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    SS2DesDefined = newSS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    SS3DesDefined = newSS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
             } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        private CoderResult encodeBufferLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                             ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            int outputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            byte[]  outputByte = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            int     inputSize = 0;                 // Size of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            newshiftout = shiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            newSODesDefined = SODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            newSS2DesDefined = SS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            newSS3DesDefined = SS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    char inputChar = src.get();
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3623
diff changeset
   572
                    if (Character.isSurrogate(inputChar)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        if (sgp.parse(inputChar, src) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                            return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    if (inputChar < 0x80) {     // ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        if (shiftout){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                            newshiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                            outputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            outputByte[0] = ISO_SI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                            outputByte[1] = (byte)(inputChar & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                            outputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                            outputByte[0] = (byte)(inputChar & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        if(inputChar == '\n'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                            newSODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                            newSS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                            newSS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        outputSize = unicodeToNative(inputChar, outputByte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        if (outputSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                            return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    if (dst.remaining() < outputSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    for (int i = 0; i < outputSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                        dst.put(outputByte[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    shiftout = newshiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    SODesDefined = newSODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    SS2DesDefined = newSS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    SS3DesDefined = newSS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        protected CoderResult encodeLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                         ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                return encodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                return encodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
}