src/jdk.charsets/share/classes/sun/nio/cs/ext/ISO2022_JP.java
author weijun
Thu, 24 Oct 2019 15:53:20 +0800
changeset 58776 ea153023d832
parent 52378 fb71a4bc010d
permissions -rw-r--r--
8231598: keytool does not export sun.security.mscapi Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13366
diff changeset
     2
 * Copyright (c) 2002, 2012, 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
package sun.nio.cs.ext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.CodingErrorAction;
28969
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
    35
import sun.nio.cs.DelegatableDecoder;
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
    36
import sun.nio.cs.DoubleByte;
2
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
import sun.nio.cs.US_ASCII;
28969
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
    40
import sun.nio.cs.*;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
    41
import static sun.nio.cs.CharsetMapping.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Implementation notes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * (1)"Standard based" (ASCII, JIS_X_0201 and JIS_X_0208) ISO2022-JP charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * is provided by the base implementation of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Three Microsoft ISO2022-JP variants, MS50220, MS50221 and MSISO2022JP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * are provided via subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * (2)MS50220 and MS50221 are assumed to work the same way as Microsoft
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * CP50220 and CP50221's 7-bit implementation works by using CP5022X
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * specific JIS0208 and JIS0212 mapping tables (generated via Microsoft's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * MultiByteToWideChar/WideCharToMultiByte APIs). The only difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * between these 2 classes is that MS50220 does not support singlebyte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * halfwidth kana (Uff61-Uff9f) shiftin mechanism when "encoding", instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * these halfwidth kana characters are converted to their fullwidth JIS0208
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * counterparts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * The difference between the standard JIS_X_0208 and JIS_X_0212 mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * and the CP50220/50221 specific are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * 0208 mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *              1)0x213d <-> U2015 (compared to U2014)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *              2)One way mappings for 5 characters below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *                u2225 (ms) -> 0x2142 <-> u2016 (jis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *                uff0d (ms) -> 0x215d <-> u2212 (jis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *                uffe0 (ms) -> 0x2171 <-> u00a2 (jis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *                uffe1 (ms) -> 0x2172 <-> u00a3 (jis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *                uffe2 (ms) -> 0x224c <-> u00ac (jis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *                //should consider 0xff5e -> 0x2141 <-> U301c?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *              3)NEC Row13 0x2d21-0x2d79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *              4)85-94 ku <-> UE000,UE3AB (includes NEC selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *                IBM kanji in 89-92ku)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *              5)UFF61-UFF9f -> Fullwidth 0208 KANA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * 0212 mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *              1)0x2237 <-> UFF5E (Fullwidth Tilde)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *              2)0x2271 <-> U2116 (Numero Sign)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *              3)85-94 ku <-> UE3AC - UE757
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * (3)MSISO2022JP uses a JIS0208 mapping generated from MS932DB.b2c
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * and MS932DB.c2b by converting the SJIS codepoints back to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * JIS0208 counterparts. With the exception of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * (a)Codepoints with a resulting JIS0208 codepoints beyond 0x7e00 are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *    dropped (this includs the IBM Extended Kanji/Non-kanji from 0x9321
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *    to 0x972c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * (b)The Unicode codepoints that the IBM Extended Kanji/Non-kanji are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *    mapped to (in MS932) are mapped back to NEC selected IBM Kanji/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *    Non-kanji area at 0x7921-0x7c7e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Compared to JIS_X_0208 mapping, this MS932 based mapping has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * (a)different mappings for 7 JIS codepoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *        0x213d <-> U2015
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *        0x2141 <-> UFF5E
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *        0x2142 <-> U2225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *        0x215d <-> Uff0d
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *        0x2171 <-> Uffe0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *        0x2172 <-> Uffe1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *        0x224c <-> Uffe2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * (b)added one-way c2b mappings for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *        U00b8 -> 0x2124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *        U00b7 -> 0x2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *        U00af -> 0x2131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *        U00ab -> 0x2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *        U00bb -> 0x2264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *        U3094 -> 0x2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *        U00b5 -> 0x264c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * (c)NEC Row 13
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * (d)NEC selected IBM extended Kanji/Non-kanji
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *    These codepoints are mapped to the same Unicode codepoints as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *    the MS932 does, while MS50220/50221 maps them to the Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *    private area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * # There is also an interesting difference when compared to MS5022X
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *   0208 mapping for JIS codepoint "0x2D60", MS932 maps it to U301d
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *   but MS5022X maps it to U301e, obvious MS5022X is wrong, but...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
public class ISO2022_JP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    implements HistoricallyNamedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static final int ASCII = 0;                 // ESC ( B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static final int JISX0201_1976 = 1;         // ESC ( J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static final int JISX0208_1978 = 2;         // ESC $ @
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final int JISX0208_1983 = 3;         // ESC $ B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static final int JISX0212_1990 = 4;         // ESC $ ( D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static final int JISX0201_1976_KANA = 5;    // ESC ( I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static final int SHIFTOUT = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static final int ESC = 0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static final int SO = 0x0e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static final int SI = 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public ISO2022_JP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        super("ISO-2022-JP",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
              ExtendedCharsets.aliasesFor("ISO-2022-JP"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected ISO2022_JP(String canonicalName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                         String[] aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        super(canonicalName, aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public String historicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return "ISO2022JP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return ((cs instanceof JIS_X_0201)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                || (cs instanceof US_ASCII)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                || (cs instanceof JIS_X_0208)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                || (cs instanceof ISO2022_JP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public CharsetDecoder newDecoder() {
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   161
        return new Decoder(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public CharsetEncoder newEncoder() {
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   165
        return new Encoder(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected boolean doSBKANA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   172
    static class Decoder extends CharsetDecoder
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        implements DelegatableDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   175
        final static DoubleByte.Decoder DEC0208 =
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   176
            (DoubleByte.Decoder)new JIS_X_0208().newDecoder();
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   177
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        private int currentState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        private int previousState;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   180
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   181
        private DoubleByte.Decoder dec0208;
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   182
        private DoubleByte.Decoder dec0212;
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   183
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   184
        private Decoder(Charset cs) {
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   185
            this(cs, DEC0208, null);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   186
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        protected Decoder(Charset cs,
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   189
                          DoubleByte.Decoder dec0208,
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   190
                          DoubleByte.Decoder dec0212) {
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   191
            super(cs, 0.5f, 1.0f);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   192
            this.dec0208 = dec0208;
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   193
            this.dec0212 = dec0212;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            currentState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            previousState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        public void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            currentState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            previousState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        private CoderResult decodeArrayLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                            CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            int b1 = 0, b2 = 0, b3 = 0, b4 = 0;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   208
            char c = UNMAPPABLE_DECODING;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    b1 = sa[sp] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    if ((b1 & 0x80) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    if (b1 == ESC || b1 == SO || b1 == SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        if (b1 == ESC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                            if (sp + inputSize + 2 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            b2 = sa[sp + inputSize++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            if (b2 == '(') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                b3 = sa[sp + inputSize++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                if (b3 == 'B'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                    currentState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                } else if (b3 == 'J'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                    currentState = JISX0201_1976;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                } else if (b3 == 'I'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                    currentState = JISX0201_1976_KANA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                    return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            } else if (b2 == '$'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                b3 = sa[sp + inputSize++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                if (b3 == '@'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                    currentState = JISX0208_1978;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                } else if (b3 == 'B'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                    currentState = JISX0208_1983;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   250
                                } else if (b3 == '(' && dec0212 != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                    if (sp + inputSize + 1 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                    b4 = sa[sp + inputSize++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                    if (b4 == 'D') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                        currentState = JISX0212_1990;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                    return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        } else if (b1 == SO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                            previousState = currentState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                            currentState = SHIFTOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        } else if (b1 == SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                            currentState = previousState;
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
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    if (dp + 1 > dl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    switch (currentState){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        case ASCII:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                            da[dp++] = (char)(b1 & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        case JISX0201_1976:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                          switch (b1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                              case 0x5c:  // Yen/tilde substitution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                da[dp++] = '\u00a5';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                              case 0x7e:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                da[dp++] = '\u203e';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                              default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                da[dp++] = (char)b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        case JISX0208_1978:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        case JISX0208_1983:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            if (sp + inputSize + 1 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            b2 = sa[sp + inputSize++] & 0xff;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   299
                            c = dec0208.decodeDouble(b1,b2);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   300
                            if (c == UNMAPPABLE_DECODING)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            da[dp++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        case JISX0212_1990:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                            if (sp + inputSize + 1 > sl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                            b2 = sa[sp + inputSize++] & 0xff;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   308
                            c = dec0212.decodeDouble(b1,b2);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   309
                            if (c == UNMAPPABLE_DECODING)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                            da[dp++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        case JISX0201_1976_KANA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        case SHIFTOUT:
52378
fb71a4bc010d 8211382: ISO2022JP and GB18030 NIO converter issues
itakiguchi
parents: 47216
diff changeset
   315
                            if (b1 > 0x5f) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                            da[dp++] = (char)(b1 + 0xff40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    sp += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        private CoderResult decodeBufferLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                             CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            int b1 = 0, b2 = 0, b3 = 0, b4=0;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   335
            char c = UNMAPPABLE_DECODING;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            int inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    b1 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    if ((b1 & 0x80) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    if (b1 == ESC || b1 == SO || b1 == SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        if (b1 == ESC) {  // ESC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                            if (src.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                            b2 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                            inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            if (b2 == '(') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                b3 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                if (b3 == 'B'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                    currentState = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                } else if (b3 == 'J'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                    currentState = JISX0201_1976;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                } else if (b3 == 'I'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                    currentState = JISX0201_1976_KANA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                   return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                            } else if (b2 == '$'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                b3 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                if (b3 == '@'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                    currentState = JISX0208_1978;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                } else if (b3 == 'B'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                    currentState = JISX0208_1983;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   368
                                } else if (b3 == '(' && dec0212 != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                    if (!src.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                    b4 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                    if (b4 == 'D') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                        currentState = JISX0212_1990;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                        return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                    return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        } else if (b1 == SO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            previousState = currentState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                            currentState = SHIFTOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        } else if (b1 == SI) { // shift back in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                            currentState = previousState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    if (!dst.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    switch (currentState){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        case ASCII:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                            dst.put((char)(b1 & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        case JISX0201_1976:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                            switch (b1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                              case 0x5c:  // Yen/tilde substitution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                dst.put('\u00a5');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                              case 0x7e:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                dst.put('\u203e');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                              default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                dst.put((char)b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        case JISX0208_1978:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        case JISX0208_1983:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                            if (!src.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                            b2 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                            inputSize++;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   419
                            c = dec0208.decodeDouble(b1,b2);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   420
                            if (c == UNMAPPABLE_DECODING)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                            dst.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        case JISX0212_1990:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            if (!src.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            b2 = src.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                            inputSize++;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   429
                            c = dec0212.decodeDouble(b1,b2);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   430
                            if (c == UNMAPPABLE_DECODING)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                return CoderResult.unmappableForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            dst.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        case JISX0201_1976_KANA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        case SHIFTOUT:
52378
fb71a4bc010d 8211382: ISO2022JP and GB18030 NIO converter issues
itakiguchi
parents: 47216
diff changeset
   436
                            if (b1 > 0x5f) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                return CoderResult.malformedForLength(inputSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            dst.put((char)(b1 + 0xff40));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    mark += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // Make some protected methods public for use by JISAutoDetect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                return decodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                return decodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        public CoderResult implFlush(CharBuffer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return super.implFlush(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   463
    static class Encoder extends CharsetEncoder {
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   464
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   465
        final static DoubleByte.Encoder ENC0208 =
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   466
            (DoubleByte.Encoder)new JIS_X_0208().newEncoder();
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   467
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        private static byte[] repl = { (byte)0x21, (byte)0x29 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        private int currentMode = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        private int replaceMode = JISX0208_1983;
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   471
        private DoubleByte.Encoder enc0208;
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   472
        private DoubleByte.Encoder enc0212;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        private boolean doSBKANA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   475
        private Encoder(Charset cs) {
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   476
            this(cs, ENC0208, null, true);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   477
        }
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   478
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   479
        Encoder(Charset cs,
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   480
                DoubleByte.Encoder enc0208,
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   481
                DoubleByte.Encoder enc0212,
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   482
                boolean doSBKANA) {
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   483
            super(cs, 4.0f, (enc0212 != null)? 9.0f : 8.0f, repl);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   484
            this.enc0208 = enc0208;
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   485
            this.enc0212 = enc0212;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            this.doSBKANA = doSBKANA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        protected int encodeSingle(char inputChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        protected void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            currentMode = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        protected void implReplaceWith(byte[] newReplacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            /* It's almost impossible to decide which charset they belong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
               to. The best thing we can do here is to "guess" based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
               the length of newReplacement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            if (newReplacement.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                replaceMode = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            } else if (newReplacement.length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                replaceMode = JISX0208_1983;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        protected CoderResult implFlush(ByteBuffer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (currentMode != ASCII) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                if (out.remaining() < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                out.put((byte)0x1b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                out.put((byte)0x28);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                out.put((byte)0x42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                currentMode = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        public boolean canEncode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return ((c <= '\u007F') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    (c >= 0xFF61 && c <= 0xFF9F) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    (c == '\u00A5') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    (c == '\u203E') ||
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   526
                    enc0208.canEncode(c) ||
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   527
                    (enc0212!=null && enc0212.canEncode(c)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        private final Surrogate.Parser sgp = new Surrogate.Parser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        private CoderResult encodeArrayLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                                            ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            char[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            byte[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    char c = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    if (c <= '\u007F') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        if (currentMode != ASCII) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                            if (dl - dp < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                            da[dp++] = (byte)0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                            da[dp++] = (byte)0x28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                            da[dp++] = (byte)0x42;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                            currentMode = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                        if (dl - dp < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        da[dp++] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    } else if (c >= 0xff61 && c <= 0xff9f && doSBKANA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        //a single byte kana
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        if (currentMode != JISX0201_1976_KANA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                            if (dl - dp < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                            da[dp++] = (byte)0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                            da[dp++] = (byte)0x28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                            da[dp++] = (byte)0x49;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                            currentMode = JISX0201_1976_KANA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        if (dl - dp < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        da[dp++] = (byte)(c - 0xff40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    } else if (c == '\u00A5' || c == '\u203E') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        //backslash or tilde
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        if (currentMode != JISX0201_1976) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                            if (dl - dp < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                            da[dp++] = (byte)0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                            da[dp++] = (byte)0x28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            da[dp++] = (byte)0x4a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                            currentMode = JISX0201_1976;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        if (dl - dp < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        da[dp++] = (c == '\u00A5')?(byte)0x5C:(byte)0x7e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    } else {
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   588
                        int index = enc0208.encodeChar(c);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   589
                        if (index != UNMAPPABLE_ENCODING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                            if (currentMode != JISX0208_1983) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                if (dl - dp < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                                da[dp++] = (byte)0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                da[dp++] = (byte)0x24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                da[dp++] = (byte)0x42;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                                currentMode = JISX0208_1983;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            if (dl - dp < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                            da[dp++] = (byte)(index >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                            da[dp++] = (byte)(index & 0xff);
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   602
                        } else if (enc0212 != null &&
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   603
                                   (index = enc0212.encodeChar(c)) != UNMAPPABLE_ENCODING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                            if (currentMode != JISX0212_1990) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                if (dl - dp < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                da[dp++] = (byte)0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                                da[dp++] = (byte)0x24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                                da[dp++] = (byte)0x28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                da[dp++] = (byte)0x44;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                                currentMode = JISX0212_1990;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                            if (dl - dp < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                            da[dp++] = (byte)(index >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                            da[dp++] = (byte)(index & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                        } else {
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
   618
                            if (Character.isSurrogate(c) && sgp.parse(c, sa, sp, sl) < 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                            if (unmappableCharacterAction()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                == CodingErrorAction.REPLACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                && currentMode != replaceMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                if (dl - dp < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                if (replaceMode == ASCII) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                    da[dp++] = (byte)0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                                    da[dp++] = (byte)0x28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                                    da[dp++] = (byte)0x42;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                    da[dp++] = (byte)0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                    da[dp++] = (byte)0x24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                    da[dp++] = (byte)0x42;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                currentMode = replaceMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                            }
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
   636
                            if (Character.isSurrogate(c))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                                return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                            return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    sp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        private CoderResult encodeBufferLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                             ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    char c = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    if (c <= '\u007F') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        if (currentMode != ASCII) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                            if (dst.remaining() < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                            dst.put((byte)0x1b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                            dst.put((byte)0x28);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                            dst.put((byte)0x42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                            currentMode = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        dst.put((byte)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    } else if (c >= 0xff61 && c <= 0xff9f && doSBKANA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        //Is it a single byte kana?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        if (currentMode != JISX0201_1976_KANA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                            if (dst.remaining() < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                            dst.put((byte)0x1b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                            dst.put((byte)0x28);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                            dst.put((byte)0x49);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                            currentMode = JISX0201_1976_KANA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                        dst.put((byte)(c - 0xff40));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    } else if (c == '\u00a5' || c == '\u203E') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        if (currentMode != JISX0201_1976) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                            if (dst.remaining() < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                            dst.put((byte)0x1b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                            dst.put((byte)0x28);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                            dst.put((byte)0x4a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                            currentMode = JISX0201_1976;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        if (dst.remaining() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                        dst.put((c == '\u00A5')?(byte)0x5C:(byte)0x7e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    } else {
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   696
                        int index = enc0208.encodeChar(c);
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   697
                        if (index != UNMAPPABLE_ENCODING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                            if (currentMode != JISX0208_1983) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                                if (dst.remaining() < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                dst.put((byte)0x1b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                                dst.put((byte)0x24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                                dst.put((byte)0x42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                                currentMode = JISX0208_1983;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                            if (dst.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                            dst.put((byte)(index >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                            dst.put((byte)(index & 0xff));
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   710
                        } else if (enc0212 != null &&
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   711
                                   (index = enc0212.encodeChar(c)) != UNMAPPABLE_ENCODING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                            if (currentMode != JISX0212_1990) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                                if (dst.remaining() < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                                dst.put((byte)0x1b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                                dst.put((byte)0x24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                                dst.put((byte)0x28);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                                dst.put((byte)0x44);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                                currentMode = JISX0212_1990;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                            if (dst.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                            dst.put((byte)(index >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                            dst.put((byte)(index & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                        } else {
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
   726
                            if (Character.isSurrogate(c) && sgp.parse(c, src) < 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                                return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                            if (unmappableCharacterAction() == CodingErrorAction.REPLACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                                && currentMode != replaceMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                                if (dst.remaining() < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                                if (replaceMode == ASCII) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                                    dst.put((byte)0x1b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                                    dst.put((byte)0x28);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                                    dst.put((byte)0x42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                    dst.put((byte)0x1b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                    dst.put((byte)0x24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                    dst.put((byte)0x42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                currentMode = replaceMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                            }
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
   743
                            if (Character.isSurrogate(c))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                                return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                            return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
              } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        protected CoderResult encodeLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                                         ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                return encodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                return encodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
}