Path: blob/master/test/jdk/sun/nio/cs/OLD/SJIS_OLD.java
41154 views
/*1* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*/2728import java.nio.*;29import java.nio.charset.Charset;30import java.nio.charset.CharsetDecoder;31import java.nio.charset.CharsetEncoder;32import java.nio.charset.CoderResult;33import sun.nio.cs.HistoricallyNamedCharset;3435public class SJIS_OLD36extends Charset37implements HistoricallyNamedCharset38{3940public SJIS_OLD() {41super("Shift_JIS_OLD", null);42}4344public String historicalName() {45return "SJIS";46}4748public boolean contains(Charset cs) {49return ((cs.name().equals("US-ASCII"))50|| (cs instanceof JIS_X_0201_OLD)51|| (cs instanceof SJIS_OLD)52|| (cs instanceof JIS_X_0208_OLD));53}5455public CharsetDecoder newDecoder() {56return new Decoder(this);57}5859public CharsetEncoder newEncoder() {6061// Need to force the replacement byte to 0x3f62// because JIS_X_0208_Encoder defines its own63// alternative 2 byte substitution to permit it64// to exist as a self-standing Encoder6566byte[] replacementBytes = { (byte)0x3f };67return new Encoder(this).replaceWith(replacementBytes);68}6970static class Decoder extends JIS_X_0208_Decoder {7172JIS_X_0201_OLD.Decoder jis0201;7374protected Decoder(Charset cs) {75super(cs);76jis0201 = new JIS_X_0201_OLD.Decoder(cs);77}7879protected char decodeSingle(int b) {80// If the high bits are all off, it's ASCII == Unicode81if ((b & 0xFF80) == 0) {82return (char)b;83}84return jis0201.decode(b);85}8687protected char decodeDouble(int c1, int c2) {88int adjust = c2 < 0x9F ? 1 : 0;89int rowOffset = c1 < 0xA0 ? 0x70 : 0xB0;90int cellOffset = (adjust == 1) ? (c2 > 0x7F ? 0x20 : 0x1F) : 0x7E;91int b1 = ((c1 - rowOffset) << 1) - adjust;92int b2 = c2 - cellOffset;93return super.decodeDouble(b1, b2);94}9596// Make some protected methods public for use by JISAutoDetect97public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {98return super.decodeLoop(src, dst);99}100public void implReset() {101super.implReset();102}103public CoderResult implFlush(CharBuffer out) {104return super.implFlush(out);105}106}107108static class Encoder extends JIS_X_0208_Encoder {109110private JIS_X_0201_OLD.Encoder jis0201;111112private static final short[] j0208Index1 =113JIS_X_0208_Encoder.getIndex1();114private static final String[] j0208Index2 =115JIS_X_0208_Encoder.getIndex2();116117protected Encoder(Charset cs) {118super(cs);119jis0201 = new JIS_X_0201_OLD.Encoder(cs);120}121122protected int encodeSingle(char inputChar) {123byte b;124125// \u0000 - \u007F map straight through126if ((inputChar & 0xFF80) == 0)127return (byte)inputChar;128129if ((b = jis0201.encode(inputChar)) == 0)130return -1;131else132return b;133}134135protected int encodeDouble(char ch) {136int offset = j0208Index1[ch >> 8] << 8;137int pos = j0208Index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));138if (pos == 0) {139/* Zero value indicates this Unicode has no mapping to140* JIS0208.141* We bail here because the JIS -> SJIS algorithm produces142* bogus SJIS values for invalid JIS input. Zero should be143* the only invalid JIS value in our table.144*/145return 0;146}147/*148* This algorithm for converting from JIS to SJIS comes from149* Ken Lunde's "Understanding Japanese Information Processing",150* pg 163.151*/152int c1 = (pos >> 8) & 0xff;153int c2 = pos & 0xff;154int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;155int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E;156return ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset);157}158}159}160161162