01: /*
02: * %W% %E%
03: *
04: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: *
26: */
27:
28: package sun.io;
29:
30: /**
31: * @author Limin Shi
32: */
33:
34: public class CharToByteSJIS extends CharToByteJIS0208 {
35: CharToByteJIS0201 cbJIS0201 = new CharToByteJIS0201();
36:
37: public String getCharacterEncoding() {
38: return "SJIS";
39: }
40:
41: protected int convSingleByte(char inputChar, byte[] outputByte) {
42: byte b;
43: if (inputChar == 0) {
44: outputByte[0] = (byte) 0;
45: return 1;
46: }
47: if ((b = cbJIS0201.getNative(inputChar)) == 0)
48: return 0;
49: outputByte[0] = b;
50: return 1;
51: }
52:
53: protected int getNative(char ch) {
54: int offset = index1[ch >> 8] << 8;
55: int pos = index2[offset >> 12].charAt((offset & 0xfff)
56: + (ch & 0xff));
57: if (pos == 0) {
58: /* Zero value indicates this Unicode has no mapping to JIS0208.
59: * We bail here because the JIS -> SJIS algorithm produces
60: * bogus SJIS values for invalid JIS input. Zero should be the
61: * only invalid JIS value in our table.
62: */
63: return 0;
64: }
65: /*
66: * This algorithm for converting from JIS to SJIS comes from Ken Lunde's
67: * "Understanding Japanese Information Processing", pg 163.
68: */
69: int c1 = (pos >> 8) & 0xff;
70: int c2 = pos & 0xff;
71: int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;
72: int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F)
73: : 0x7E;
74: return ((((c1 + 1) >> 1) + rowOffset) << 8) | (c2 + cellOffset);
75: }
76: }
|