/* * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.nio.cs.ext; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; import java.nio.charset.CoderResult; import sun.nio.cs.HistoricallyNamedCharset; import sun.nio.cs.SingleByte; import static sun.nio.cs.CharsetMapping.*; public class SJIS extends Charset implements HistoricallyNamedCharset { public SJIS() { super("Shift_JIS", ExtendedCharsets.aliasesFor("Shift_JIS")); } public String historicalName() { return "SJIS"; } public boolean contains(Charset cs) { return ((cs.name().equals("US-ASCII")) || (cs instanceof JIS_X_0201) || (cs instanceof SJIS) || (cs instanceof JIS_X_0208)); } public CharsetDecoder newDecoder() { return new Decoder(this); } public CharsetEncoder newEncoder() { // Need to force the replacement byte to 0x3f // because JIS_X_0208_Encoder defines its own // alternative 2 byte substitution to permit it // to exist as a self-standing Encoder byte[] replacementBytes = { (byte)0x3f }; return new Encoder(this).replaceWith(replacementBytes); } // TBD: DB.Decoder impl currently does not use decodeSingle/ // Double() (performance). If it does switch back to use the // decodeSingle/Double, SJIS.Decoder can simply extend it. static class Decoder extends CharsetDecoder implements DelegatableDecoder { private static SingleByte.Decoder jis0201 = (SingleByte.Decoder)new JIS_X_0201().newDecoder(); private static DoubleByte.Decoder jis0208 = (DoubleByte.Decoder)new JIS_X_0208().newDecoder(); protected Decoder(Charset cs) { super(cs, 0.5f, 1.0f); } public char decodeSingle(int b) { return jis0201.decode(b); } public char decodeDouble(int c1, int c2) { int adjust = c2 < 0x9F ? 1 : 0; int rowOffset = c1 < 0xA0 ? 0x70 : 0xB0; int cellOffset = (adjust == 1) ? (c2 > 0x7F ? 0x20 : 0x1F) : 0x7E; int b1 = ((c1 - rowOffset) << 1) - adjust; int b2 = c2 - cellOffset; return jis0208.decodeDouble(b1, b2); } protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); try { while (sp < sl && dp < dl) { int inSize = 1; int b1 = sa[sp]; // & 0xff; char c = decodeSingle(b1); if (c == UNMAPPABLE_DECODING) { if (sl - sp < 2) return CoderResult.UNDERFLOW; int b2 = sa[sp + 1] & 0xff; if ((c = decodeDouble(b1 & 0xff, b2)) == UNMAPPABLE_DECODING) { return CoderResult.unmappableForLength(2); } inSize = 2; } da[dp++] = c; sp += inSize; } return (sp >= sl) ? CoderResult.UNDERFLOW : CoderResult.OVERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } } protected CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) { int mark = src.position(); try { while (src.hasRemaining() && dst.hasRemaining()) { int b1 = src.get(); // & 0xff; char c = decodeSingle(b1); int inSize = 1; if (c == UNMAPPABLE_DECODING) { if (src.remaining() < 1) return CoderResult.UNDERFLOW; int b2 = src.get() & 0xff; if ((c = decodeDouble(b1 & 0xff, b2)) == UNMAPPABLE_DECODING) { return CoderResult.unmappableForLength(2); } inSize = 2; } dst.put(c); mark += inSize; } return src.hasRemaining()? CoderResult.OVERFLOW : CoderResult.UNDERFLOW; } finally { src.position(mark); } } public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) { if (src.hasArray() && dst.hasArray()) return decodeArrayLoop(src, dst); else return decodeBufferLoop(src, dst); } public void implReset() { super.implReset(); } public CoderResult implFlush(CharBuffer out) { return super.implFlush(out); } } static class Encoder extends DoubleByte.Encoder_JISX0208 { private static SingleByte.Encoder jis0201 = (SingleByte.Encoder)new JIS_X_0201().newEncoder(); private static DoubleByte.Encoder jis0208 = (DoubleByte.Encoder)new JIS_X_0208().newEncoder(); //init protected Encoder(Charset cs) { super(cs, JIS_X_0208.c2b, JIS_X_0208.c2bIndex); } public int encodeChar(char ch) { int bb; if ((bb = jis0201.encode(ch)) != UNMAPPABLE_ENCODING) { return bb; } bb = super.encodeChar(ch); if (bb == UNMAPPABLE_ENCODING || bb <= MAX_SINGLEBYTE) return UNMAPPABLE_ENCODING; /* * This algorithm for converting from JIS to SJIS comes from * Ken Lunde's "Understanding Japanese Information Processing", * pg 163. */ int c1 = (bb >> 8) & 0xff; int c2 = bb & 0xff; int rowOffset = c1 < 0x5F ? 0x70 : 0xB0; int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E; return ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset); } } }