jdk/src/share/classes/sun/nio/cs/ext/SJIS.java
author martin
Wed, 30 Jun 2010 16:11:32 -0700
changeset 5986 04eb44085c00
parent 5785 5dfabe612d10
child 7668 d4a77089c587
permissions -rw-r--r--
6934265: Add public method Character.isBmpCodePoint Summary: Move isBmpCodePoint from sun.nio.cs.Surrogate to Character Reviewed-by: sherman Contributed-by: Ulf Zibis <ulf.zibis@gmx.de>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2006, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package sun.nio.cs.ext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.nio.cs.HistoricallyNamedCharset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class SJIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    implements HistoricallyNamedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public SJIS() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        super("Shift_JIS", ExtendedCharsets.aliasesFor("Shift_JIS"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public String historicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return "SJIS";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return ((cs.name().equals("US-ASCII"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                || (cs instanceof JIS_X_0201)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                || (cs instanceof SJIS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                || (cs instanceof JIS_X_0208));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return new Decoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // Need to force the replacement byte to 0x3f
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // because JIS_X_0208_Encoder defines its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // alternative 2 byte substitution to permit it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // to exist as a self-standing Encoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        byte[] replacementBytes = { (byte)0x3f };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return new Encoder(this).replaceWith(replacementBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static class Decoder extends JIS_X_0208_Decoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        implements DelegatableDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        JIS_X_0201.Decoder jis0201;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        protected Decoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            super(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            jis0201 = new JIS_X_0201.Decoder(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        protected char decodeSingle(int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // If the high bits are all off, it's ASCII == Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if ((b & 0xFF80) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                return (char)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return jis0201.decode(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        protected char decodeDouble(int c1, int c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int adjust = c2 < 0x9F ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int rowOffset = c1 < 0xA0 ? 0x70 : 0xB0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            int cellOffset = (adjust == 1) ? (c2 > 0x7F ? 0x20 : 0x1F) : 0x7E;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            int b1 = ((c1 - rowOffset) << 1) - adjust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            int b2 = c2 - cellOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return super.decodeDouble(b1, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // Make some protected methods public for use by JISAutoDetect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return super.decodeLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        public void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            super.implReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        public CoderResult implFlush(CharBuffer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return super.implFlush(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static class Encoder extends JIS_X_0208_Encoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        private JIS_X_0201.Encoder jis0201;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
5785
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   117
        private static final short[] j0208Index1 =
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   118
            JIS_X_0208_Encoder.getIndex1();
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   119
        private static final String[] j0208Index2 =
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   120
            JIS_X_0208_Encoder.getIndex2();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        protected Encoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            super(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            jis0201 = new JIS_X_0201.Encoder(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        protected int encodeSingle(char inputChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            byte b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // \u0000 - \u007F map straight through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if ((inputChar & 0xFF80) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                return (byte)inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if ((b = jis0201.encode(inputChar)) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        protected int encodeDouble(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            int offset = j0208Index1[ch >> 8] << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int pos = j0208Index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (pos == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                /* Zero value indicates this Unicode has no mapping to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                 * JIS0208.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                 * We bail here because the JIS -> SJIS algorithm produces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                 * bogus SJIS values for invalid JIS input.  Zero should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                 * the only invalid JIS value in our table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
             * This algorithm for converting from JIS to SJIS comes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
             * Ken Lunde's "Understanding Japanese Information Processing",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
             * pg 163.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            int c1 = (pos >> 8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            int c2 = pos & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}