jdk/src/share/classes/sun/nio/cs/ext/EUC_JP.java
author martin
Mon, 31 Aug 2009 15:00:04 -0700
changeset 3714 6a4eb8f53f91
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6860431: Character.isSurrogate(char ch) Summary: Add new method Character.isSurrogate(char ch) Reviewed-by: sherman, darcy, okutsu
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-2004 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.HistoricallyNamedCharset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.nio.cs.Surrogate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class EUC_JP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    implements HistoricallyNamedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public EUC_JP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        super("EUC-JP", ExtendedCharsets.aliasesFor("EUC-JP"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public String historicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return "EUC_JP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return ((cs.name().equals("US-ASCII"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                || (cs instanceof JIS_X_0201)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                || (cs instanceof JIS_X_0208)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                || (cs instanceof JIS_X_0212)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                || (cs instanceof EUC_JP));
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 CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return new Decoder(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
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Need to force the replacement byte to 0x3f
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // because JIS_X_0208_Encoder defines its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // alternative 2 byte substitution to permit it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // to exist as a self-standing Encoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        byte[] replacementBytes = { (byte)0x3f };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return new Encoder(this).replaceWith(replacementBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static class Decoder extends JIS_X_0208_Decoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        implements DelegatableDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        JIS_X_0201.Decoder decoderJ0201;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        JIS_X_0212_Decoder decoderJ0212;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        short[] j0208Index1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        String[] j0208Index2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        protected Decoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            super(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            decoderJ0201 = new JIS_X_0201.Decoder(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            decoderJ0212 = new JIS_X_0212_Decoder(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            start = 0xa1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            end = 0xfe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            j0208Index1 = super.getIndex1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            j0208Index2 = super.getIndex2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        protected char decode0212(int byte1, int byte2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
             return decoderJ0212.decodeDouble(byte1, byte2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        protected char decodeDouble(int byte1, int byte2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (byte1 == 0x8e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return decoderJ0201.decode(byte2 - 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // Fix for bug 4121358 - similar fix for bug 4117820 put
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // into ByteToCharDoubleByte.getUnicode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (((byte1 < 0) || (byte1 > getIndex1().length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                || ((byte2 < start) || (byte2 > end)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                return REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            int n = (j0208Index1[byte1 - 0x80] & 0xf) * (end - start + 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    + (byte2 - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return j0208Index2[j0208Index1[byte1 - 0x80] >> 4].charAt(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        private CoderResult decodeArrayLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                            CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            int b1 = 0, b2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            char outputChar = REPLACE_CHAR; // U+FFFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    b1 = sa[sp] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    if ((b1 & 0x80) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        outputChar = (char)b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    else {      // Multibyte char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        if ((b1 & 0xff) == 0x8f) {   // JIS0212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            if (sp + 3 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                               return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                            b1 = sa[sp + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                            b2 = sa[sp + 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            inputSize += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                            outputChar = decode0212(b1-0x80, b2-0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                          // JIS0208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                            if (sp + 2 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                               return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                            b2 = sa[sp + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                            outputChar = decodeDouble(b1, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    if (outputChar == REPLACE_CHAR) { // can't be decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    if (dp + 1 > dl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    da[dp++] = outputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    sp += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        private CoderResult decodeBufferLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                             CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            int b1 = 0, b2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            char outputChar = REPLACE_CHAR; // U+FFFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    b1 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    if ((b1 & 0x80) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        outputChar = (char)b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    } else {    // Multibyte char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        if ((b1 & 0xff) == 0x8f) {   // JIS0212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            if (src.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                               return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                            b1 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                            b2 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                            inputSize += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                            outputChar = decode0212(b1-0x80, b2-0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                          // JIS0208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                            if (src.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                               return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            b2 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                            outputChar = decodeDouble(b1, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    if (outputChar == REPLACE_CHAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                dst.put(outputChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // Make some protected methods public for use by JISAutoDetect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return decodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                return decodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        public void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            super.implReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        public CoderResult implFlush(CharBuffer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return super.implFlush(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    static class Encoder extends JIS_X_0208_Encoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        JIS_X_0201.Encoder encoderJ0201;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        JIS_X_0212_Encoder encoderJ0212;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        short[] j0208Index1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        String[] j0208Index2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        private final Surrogate.Parser sgp = new Surrogate.Parser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        protected Encoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            super(cs, 3.0f, 3.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            encoderJ0201 = new JIS_X_0201.Encoder(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            encoderJ0212 = new JIS_X_0212_Encoder(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            j0208Index1 = super.getIndex1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            j0208Index2 = super.getIndex2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        public boolean canEncode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            byte[]  encodedBytes = new byte[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (encodeSingle(c, encodedBytes) == 0) { //doublebyte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                if (encodeDouble(c) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        protected int encodeSingle(char inputChar, byte[] outputByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            byte b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (inputChar == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                outputByte[0] = (byte)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if ((b = encoderJ0201.encode(inputChar)) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (b > 0 && b < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                outputByte[0] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            outputByte[0] = (byte)0x8e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            outputByte[1] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        protected int encodeDouble(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            int offset = j0208Index1[((ch & 0xff00) >> 8 )] << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            int r = j0208Index2[offset >> 12].charAt((offset & 0xfff) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    (ch & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (r != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                return r + 0x8080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            r = encoderJ0212.encodeDouble(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (r == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return r + 0x8F8080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        private CoderResult encodeArrayLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                            ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            char[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            byte[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            int outputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            byte[]  outputByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            int     inputSize = 0;                 // Size of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            byte[]  tmpBuf = new byte[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    outputByte = tmpBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    char c = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
   321
                    if (Character.isSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        if (sgp.parse(c, sa, sp, sl) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                            return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    outputSize = encodeSingle(c, outputByte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    if (outputSize == 0) { // DoubleByte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        int ncode = encodeDouble(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        if (ncode != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                            if ((ncode & 0xFF0000) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                outputByte[0] = (byte) ((ncode & 0xff00) >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                outputByte[1] = (byte) (ncode & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                outputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                outputByte[0] = (byte) 0x8f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                outputByte[1] = (byte) ((ncode & 0xff00) >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                outputByte[2] = (byte) (ncode & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                outputSize = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    if (dl - dp < outputSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    // Put the byte in the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    for (int i = 0; i < outputSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        da[dp++] = outputByte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    sp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        private CoderResult encodeBufferLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                             ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            int outputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            byte[]  outputByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            int     inputSize = 0;                 // Size of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            byte[]  tmpBuf = new byte[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    outputByte = tmpBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    char c = src.get();
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
   375
                    if (Character.isSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        if (sgp.parse(c, src) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                            return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    outputSize = encodeSingle(c, outputByte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if (outputSize == 0) { // DoubleByte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        int ncode = encodeDouble(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        if (ncode != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            if ((ncode & 0xFF0000) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                outputByte[0] = (byte) ((ncode & 0xff00) >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                outputByte[1] = (byte) (ncode & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                outputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                outputByte[0] = (byte) 0x8f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                outputByte[1] = (byte) ((ncode & 0xff00) >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                outputByte[2] = (byte) (ncode & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                outputSize = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    if (dst.remaining() < outputSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    // Put the byte in the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    for (int i = 0; i < outputSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        dst.put(outputByte[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        protected CoderResult encodeLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                         ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                return encodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                return encodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
}