PCK_OLD.java revision 6073:cea72c2bf071
1/*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 */
28
29import java.nio.charset.Charset;
30import java.nio.charset.CharsetDecoder;
31import java.nio.charset.CharsetEncoder;
32import java.nio.charset.CoderResult;
33import java.nio.ByteBuffer;
34import java.nio.CharBuffer;
35import sun.nio.cs.HistoricallyNamedCharset;
36
37public class PCK_OLD
38    extends Charset
39    implements HistoricallyNamedCharset
40{
41
42    public PCK_OLD() {
43        super("x-PCK_OLD", null);
44    }
45
46    public String historicalName() {
47        return "PCK";
48    }
49
50    public boolean contains(Charset cs) {
51        return ((cs.name().equals("US-ASCII"))
52                || (cs instanceof JIS_X_0201_OLD)
53                || (cs instanceof PCK_OLD));
54    }
55
56    public CharsetDecoder newDecoder() {
57        return new Decoder(this);
58    }
59
60    public CharsetEncoder newEncoder() {
61
62        // Need to force the replacement byte to 0x3f
63        // because JIS_X_0208_Encoder defines its own
64        // alternative 2 byte substitution to permit it
65        // to exist as a self-standing Encoder
66
67        byte[] replacementBytes = { (byte)0x3f };
68        return new Encoder(this).replaceWith(replacementBytes);
69    }
70
71    private static class Decoder extends SJIS_OLD.Decoder {
72
73        JIS_X_0208_Solaris_Decoder jis0208;
74        private static final char REPLACE_CHAR='\uFFFD';
75
76        private Decoder(Charset cs) {
77            super(cs);
78            jis0208 = new JIS_X_0208_Solaris_Decoder(cs);
79        }
80
81        protected char decodeDouble(int c1, int c2) {
82            char outChar;
83
84            if ((outChar = super.decodeDouble(c1, c2)) != '\uFFFD')  {
85                // Map JIS X 0208:1983 0x213D <--> U+2015
86                return ((outChar != '\u2014')? outChar: '\u2015');
87            } else {
88                int adjust = c2 < 0x9F ? 1 : 0;
89                int rowOffset = c1 < 0xA0 ? 0x70 : 0xB0;
90                int cellOffset = (adjust == 1) ? (c2 > 0x7F ? 0x20 : 0x1F) : 0x7E;
91                int b1 = ((c1 - rowOffset) << 1) - adjust;
92                int b2 = c2 - cellOffset;
93                char outChar2 = jis0208.decodeDouble(b1, b2);
94                return outChar2;
95            }
96        }
97    }
98
99    private static class Encoder extends SJIS_OLD.Encoder {
100
101        private JIS_X_0201_OLD.Encoder jis0201;
102
103        private static final short[] j0208Index1 =
104            JIS_X_0208_Solaris_Encoder.getIndex1();
105        private static final String[] j0208Index2 =
106            JIS_X_0208_Solaris_Encoder.getIndex2();
107
108        private Encoder(Charset cs) {
109            super(cs);
110            jis0201 = new JIS_X_0201_OLD.Encoder(cs);
111        }
112
113        protected int encodeDouble(char ch) {
114            int result = 0;
115
116            // PCK uses JIS_X_0208:1983 rather than JIS_X_0208:1997
117
118            switch (ch) {
119                case '\u2015':
120                    return 0x815C;
121                case '\u2014':
122                    return 0;
123                default:
124                    break;
125            }
126
127            if ((result = super.encodeDouble(ch)) != 0) {
128                return result;
129            }
130            else {
131                int offset = j0208Index1[ch >> 8] << 8;
132                int pos = j0208Index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
133                if (pos != 0) {
134                int c1 = (pos >> 8) & 0xff;
135                int c2 = pos & 0xff;
136                int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;
137                int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E;
138                result = ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset);
139                }
140            }
141            return result;
142        }
143    }
144}
145