Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/nio/cs/OLD/SJIS_OLD.java
41154 views
1
/*
2
* Copyright (c) 2002, 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
29
import java.nio.*;
30
import java.nio.charset.Charset;
31
import java.nio.charset.CharsetDecoder;
32
import java.nio.charset.CharsetEncoder;
33
import java.nio.charset.CoderResult;
34
import sun.nio.cs.HistoricallyNamedCharset;
35
36
public class SJIS_OLD
37
extends Charset
38
implements HistoricallyNamedCharset
39
{
40
41
public SJIS_OLD() {
42
super("Shift_JIS_OLD", null);
43
}
44
45
public String historicalName() {
46
return "SJIS";
47
}
48
49
public boolean contains(Charset cs) {
50
return ((cs.name().equals("US-ASCII"))
51
|| (cs instanceof JIS_X_0201_OLD)
52
|| (cs instanceof SJIS_OLD)
53
|| (cs instanceof JIS_X_0208_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
static class Decoder extends JIS_X_0208_Decoder {
72
73
JIS_X_0201_OLD.Decoder jis0201;
74
75
protected Decoder(Charset cs) {
76
super(cs);
77
jis0201 = new JIS_X_0201_OLD.Decoder(cs);
78
}
79
80
protected char decodeSingle(int b) {
81
// If the high bits are all off, it's ASCII == Unicode
82
if ((b & 0xFF80) == 0) {
83
return (char)b;
84
}
85
return jis0201.decode(b);
86
}
87
88
protected char decodeDouble(int c1, int c2) {
89
int adjust = c2 < 0x9F ? 1 : 0;
90
int rowOffset = c1 < 0xA0 ? 0x70 : 0xB0;
91
int cellOffset = (adjust == 1) ? (c2 > 0x7F ? 0x20 : 0x1F) : 0x7E;
92
int b1 = ((c1 - rowOffset) << 1) - adjust;
93
int b2 = c2 - cellOffset;
94
return super.decodeDouble(b1, b2);
95
}
96
97
// Make some protected methods public for use by JISAutoDetect
98
public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
99
return super.decodeLoop(src, dst);
100
}
101
public void implReset() {
102
super.implReset();
103
}
104
public CoderResult implFlush(CharBuffer out) {
105
return super.implFlush(out);
106
}
107
}
108
109
static class Encoder extends JIS_X_0208_Encoder {
110
111
private JIS_X_0201_OLD.Encoder jis0201;
112
113
private static final short[] j0208Index1 =
114
JIS_X_0208_Encoder.getIndex1();
115
private static final String[] j0208Index2 =
116
JIS_X_0208_Encoder.getIndex2();
117
118
protected Encoder(Charset cs) {
119
super(cs);
120
jis0201 = new JIS_X_0201_OLD.Encoder(cs);
121
}
122
123
protected int encodeSingle(char inputChar) {
124
byte b;
125
126
// \u0000 - \u007F map straight through
127
if ((inputChar & 0xFF80) == 0)
128
return (byte)inputChar;
129
130
if ((b = jis0201.encode(inputChar)) == 0)
131
return -1;
132
else
133
return b;
134
}
135
136
protected int encodeDouble(char ch) {
137
int offset = j0208Index1[ch >> 8] << 8;
138
int pos = j0208Index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
139
if (pos == 0) {
140
/* Zero value indicates this Unicode has no mapping to
141
* JIS0208.
142
* We bail here because the JIS -> SJIS algorithm produces
143
* bogus SJIS values for invalid JIS input. Zero should be
144
* the only invalid JIS value in our table.
145
*/
146
return 0;
147
}
148
/*
149
* This algorithm for converting from JIS to SJIS comes from
150
* Ken Lunde's "Understanding Japanese Information Processing",
151
* pg 163.
152
*/
153
int c1 = (pos >> 8) & 0xff;
154
int c2 = pos & 0xff;
155
int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;
156
int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E;
157
return ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset);
158
}
159
}
160
}
161
162