01: /*
02: * Copyright 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package sun.io;
27:
28: /**
29: * @author Limin Shi
30: * @author Mark Son-Bell
31: */
32:
33: public class CharToByteSJIS extends CharToByteJIS0208 {
34: CharToByteJIS0201 cbJIS0201 = new CharToByteJIS0201();
35:
36: public String getCharacterEncoding() {
37: return "SJIS";
38: }
39:
40: protected int convSingleByte(char inputChar, byte[] outputByte) {
41: byte b;
42:
43: // \u0000 - \u007F map straight through
44: if ((inputChar & 0xFF80) == 0) {
45: outputByte[0] = (byte) inputChar;
46: return 1;
47: }
48:
49: if ((b = cbJIS0201.getNative(inputChar)) == 0)
50: return 0;
51:
52: outputByte[0] = b;
53: return 1;
54: }
55:
56: protected int getNative(char ch) {
57: int offset = index1[ch >> 8] << 8;
58: int pos = index2[offset >> 12].charAt((offset & 0xfff)
59: + (ch & 0xff));
60: if (pos == 0) {
61: /* Zero value indicates this Unicode has no mapping to JIS0208.
62: * We bail here because the JIS -> SJIS algorithm produces
63: * bogus SJIS values for invalid JIS input. Zero should be the
64: * only invalid JIS value in our table.
65: */
66: return 0;
67: }
68: /*
69: * This algorithm for converting from JIS to SJIS comes from Ken Lunde's
70: * "Understanding Japanese Information Processing", pg 163.
71: */
72: int c1 = (pos >> 8) & 0xff;
73: int c2 = pos & 0xff;
74: int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;
75: int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F)
76: : 0x7E;
77: return ((((c1 + 1) >> 1) + rowOffset) << 8) | (c2 + cellOffset);
78: }
79: }
|