Path: blob/master/test/jdk/sun/nio/cs/OLD/PCK_OLD.java
41154 views
/*1* Copyright (c) 2003, 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.charset.Charset;29import java.nio.charset.CharsetDecoder;30import java.nio.charset.CharsetEncoder;31import java.nio.charset.CoderResult;32import java.nio.ByteBuffer;33import java.nio.CharBuffer;34import sun.nio.cs.HistoricallyNamedCharset;3536public class PCK_OLD37extends Charset38implements HistoricallyNamedCharset39{4041public PCK_OLD() {42super("x-PCK_OLD", null);43}4445public String historicalName() {46return "PCK";47}4849public boolean contains(Charset cs) {50return ((cs.name().equals("US-ASCII"))51|| (cs instanceof JIS_X_0201_OLD)52|| (cs instanceof PCK_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}6970private static class Decoder extends SJIS_OLD.Decoder {7172JIS_X_0208_Solaris_Decoder jis0208;73private static final char REPLACE_CHAR='\uFFFD';7475private Decoder(Charset cs) {76super(cs);77jis0208 = new JIS_X_0208_Solaris_Decoder(cs);78}7980protected char decodeDouble(int c1, int c2) {81char outChar;8283if ((outChar = super.decodeDouble(c1, c2)) != '\uFFFD') {84// Map JIS X 0208:1983 0x213D <--> U+201585return ((outChar != '\u2014')? outChar: '\u2015');86} else {87int adjust = c2 < 0x9F ? 1 : 0;88int rowOffset = c1 < 0xA0 ? 0x70 : 0xB0;89int cellOffset = (adjust == 1) ? (c2 > 0x7F ? 0x20 : 0x1F) : 0x7E;90int b1 = ((c1 - rowOffset) << 1) - adjust;91int b2 = c2 - cellOffset;92char outChar2 = jis0208.decodeDouble(b1, b2);93return outChar2;94}95}96}9798private static class Encoder extends SJIS_OLD.Encoder {99100private JIS_X_0201_OLD.Encoder jis0201;101102private static final short[] j0208Index1 =103JIS_X_0208_Solaris_Encoder.getIndex1();104private static final String[] j0208Index2 =105JIS_X_0208_Solaris_Encoder.getIndex2();106107private Encoder(Charset cs) {108super(cs);109jis0201 = new JIS_X_0201_OLD.Encoder(cs);110}111112protected int encodeDouble(char ch) {113int result = 0;114115// PCK uses JIS_X_0208:1983 rather than JIS_X_0208:1997116117switch (ch) {118case '\u2015':119return 0x815C;120case '\u2014':121return 0;122default:123break;124}125126if ((result = super.encodeDouble(ch)) != 0) {127return result;128}129else {130int offset = j0208Index1[ch >> 8] << 8;131int pos = j0208Index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));132if (pos != 0) {133int c1 = (pos >> 8) & 0xff;134int c2 = pos & 0xff;135int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;136int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E;137result = ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset);138}139}140return result;141}142}143}144145146