2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1996, 1998, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
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) + (ch & 0xff));
|
|
59 |
if (pos == 0) {
|
|
60 |
/* Zero value indicates this Unicode has no mapping to JIS0208.
|
|
61 |
* We bail here because the JIS -> SJIS algorithm produces
|
|
62 |
* bogus SJIS values for invalid JIS input. Zero should be the
|
|
63 |
* only invalid JIS value in our table.
|
|
64 |
*/
|
|
65 |
return 0;
|
|
66 |
}
|
|
67 |
/*
|
|
68 |
* This algorithm for converting from JIS to SJIS comes from Ken Lunde's
|
|
69 |
* "Understanding Japanese Information Processing", pg 163.
|
|
70 |
*/
|
|
71 |
int c1 = (pos >> 8) & 0xff;
|
|
72 |
int c2 = pos & 0xff;
|
|
73 |
int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;
|
|
74 |
int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E;
|
|
75 |
return ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset);
|
|
76 |
}
|
|
77 |
}
|