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