0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A */
0N/A
0N/Apackage sun.nio.cs.ext;
0N/A
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.CharBuffer;
0N/Aimport java.nio.charset.Charset;
0N/Aimport java.nio.charset.CharsetDecoder;
0N/Aimport java.nio.charset.CharsetEncoder;
0N/Aimport java.nio.charset.CoderResult;
0N/Aimport sun.nio.cs.HistoricallyNamedCharset;
0N/A
0N/Apublic class SJIS
0N/A extends Charset
0N/A implements HistoricallyNamedCharset
0N/A{
0N/A
0N/A public SJIS() {
0N/A super("Shift_JIS", ExtendedCharsets.aliasesFor("Shift_JIS"));
0N/A }
0N/A
0N/A public String historicalName() {
0N/A return "SJIS";
0N/A }
0N/A
0N/A public boolean contains(Charset cs) {
0N/A return ((cs.name().equals("US-ASCII"))
0N/A || (cs instanceof JIS_X_0201)
0N/A || (cs instanceof SJIS)
0N/A || (cs instanceof JIS_X_0208));
0N/A }
0N/A
0N/A public CharsetDecoder newDecoder() {
0N/A return new Decoder(this);
0N/A }
0N/A
0N/A public CharsetEncoder newEncoder() {
0N/A
0N/A // Need to force the replacement byte to 0x3f
0N/A // because JIS_X_0208_Encoder defines its own
0N/A // alternative 2 byte substitution to permit it
0N/A // to exist as a self-standing Encoder
0N/A
0N/A byte[] replacementBytes = { (byte)0x3f };
0N/A return new Encoder(this).replaceWith(replacementBytes);
0N/A }
0N/A
0N/A static class Decoder extends JIS_X_0208_Decoder
0N/A implements DelegatableDecoder {
0N/A
0N/A JIS_X_0201.Decoder jis0201;
0N/A
0N/A protected Decoder(Charset cs) {
0N/A super(cs);
0N/A jis0201 = new JIS_X_0201.Decoder(cs);
0N/A }
0N/A
0N/A protected char decodeSingle(int b) {
0N/A // If the high bits are all off, it's ASCII == Unicode
0N/A if ((b & 0xFF80) == 0) {
0N/A return (char)b;
0N/A }
0N/A return jis0201.decode(b);
0N/A }
0N/A
0N/A protected char decodeDouble(int c1, int c2) {
0N/A int adjust = c2 < 0x9F ? 1 : 0;
0N/A int rowOffset = c1 < 0xA0 ? 0x70 : 0xB0;
0N/A int cellOffset = (adjust == 1) ? (c2 > 0x7F ? 0x20 : 0x1F) : 0x7E;
0N/A int b1 = ((c1 - rowOffset) << 1) - adjust;
0N/A int b2 = c2 - cellOffset;
0N/A return super.decodeDouble(b1, b2);
0N/A }
0N/A
0N/A // Make some protected methods public for use by JISAutoDetect
0N/A public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
0N/A return super.decodeLoop(src, dst);
0N/A }
0N/A public void implReset() {
0N/A super.implReset();
0N/A }
0N/A public CoderResult implFlush(CharBuffer out) {
0N/A return super.implFlush(out);
0N/A }
0N/A }
0N/A
0N/A static class Encoder extends JIS_X_0208_Encoder {
0N/A
0N/A private JIS_X_0201.Encoder jis0201;
0N/A
2472N/A private static final short[] j0208Index1 =
2472N/A JIS_X_0208_Encoder.getIndex1();
2472N/A private static final String[] j0208Index2 =
2472N/A JIS_X_0208_Encoder.getIndex2();
0N/A
0N/A protected Encoder(Charset cs) {
0N/A super(cs);
0N/A jis0201 = new JIS_X_0201.Encoder(cs);
0N/A }
0N/A
0N/A protected int encodeSingle(char inputChar) {
0N/A byte b;
0N/A
0N/A // \u0000 - \u007F map straight through
0N/A if ((inputChar & 0xFF80) == 0)
0N/A return (byte)inputChar;
0N/A
0N/A if ((b = jis0201.encode(inputChar)) == 0)
0N/A return -1;
0N/A else
0N/A return b;
0N/A }
0N/A
0N/A protected int encodeDouble(char ch) {
0N/A int offset = j0208Index1[ch >> 8] << 8;
0N/A int pos = j0208Index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
0N/A if (pos == 0) {
0N/A /* Zero value indicates this Unicode has no mapping to
0N/A * JIS0208.
0N/A * We bail here because the JIS -> SJIS algorithm produces
0N/A * bogus SJIS values for invalid JIS input. Zero should be
0N/A * the only invalid JIS value in our table.
0N/A */
0N/A return 0;
0N/A }
0N/A /*
0N/A * This algorithm for converting from JIS to SJIS comes from
0N/A * Ken Lunde's "Understanding Japanese Information Processing",
0N/A * pg 163.
0N/A */
0N/A int c1 = (pos >> 8) & 0xff;
0N/A int c2 = pos & 0xff;
0N/A int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;
0N/A int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E;
0N/A return ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset);
0N/A }
0N/A }
0N/A}