Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.charsets/share/classes/sun/nio/cs/ext/ISO2022.java
41161 views
1
/*
2
* Copyright (c) 2002, 2021, 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
package sun.nio.cs.ext;
30
31
import java.nio.ByteBuffer;
32
import java.nio.CharBuffer;
33
import java.nio.charset.Charset;
34
import java.nio.charset.CharsetDecoder;
35
import java.nio.charset.CharsetEncoder;
36
import java.nio.charset.CoderResult;
37
import sun.nio.cs.Surrogate;
38
39
abstract class ISO2022
40
extends Charset
41
{
42
public ISO2022(String csname, String[] aliases) {
43
super(csname, aliases);
44
}
45
46
public abstract CharsetDecoder newDecoder();
47
48
public abstract CharsetEncoder newEncoder();
49
50
// No default Decoder implementation is provided here; the concrete
51
// encodings differ enough that most had been specialized for
52
// performance reasons, leaving the generic implementation that existed
53
// here before JDK-8261418 unused except by ISO2022_KR. As both a
54
// simplification and an optimization the implementation was moved
55
// there and specialized.
56
57
protected static class Encoder extends CharsetEncoder {
58
59
private static final byte ISO_ESC = 0x1b;
60
private static final byte ISO_SI = 0x0f;
61
private static final byte ISO_SO = 0x0e;
62
private static final byte ISO_SS2_7 = 0x4e;
63
private static final byte ISO_SS3_7 = 0x4f;
64
65
private final Surrogate.Parser sgp = new Surrogate.Parser();
66
public static final byte SS2 = (byte)0x8e;
67
public static final byte PLANE2 = (byte)0xA2;
68
public static final byte PLANE3 = (byte)0xA3;
69
70
protected final byte maximumDesignatorLength = 4;
71
72
protected byte[] SODesig,
73
SS2Desig = null,
74
SS3Desig = null;
75
76
protected CharsetEncoder ISOEncoder;
77
78
private boolean shiftout = false;
79
private boolean SODesDefined = false;
80
private boolean SS2DesDefined = false;
81
private boolean SS3DesDefined = false;
82
83
private boolean newshiftout = false;
84
private boolean newSODesDefined = false;
85
private boolean newSS2DesDefined = false;
86
private boolean newSS3DesDefined = false;
87
88
protected Encoder(Charset cs) {
89
super(cs, 4.0f, 8.0f);
90
}
91
92
public boolean canEncode(char c) {
93
return (ISOEncoder.canEncode(c));
94
}
95
96
protected void implReset() {
97
shiftout = false;
98
SODesDefined = false;
99
SS2DesDefined = false;
100
SS3DesDefined = false;
101
}
102
103
private int unicodeToNative(char unicode, byte ebyte[]) {
104
int index = 0;
105
char[] convChar = {unicode};
106
byte[] convByte = new byte[4];
107
int converted;
108
109
try{
110
CharBuffer cc = CharBuffer.wrap(convChar);
111
ByteBuffer bb = ByteBuffer.wrap(convByte);
112
ISOEncoder.encode(cc, bb, true);
113
bb.flip();
114
converted = bb.remaining();
115
} catch(Exception e) {
116
return -1;
117
}
118
119
if (converted == 2) {
120
if (!SODesDefined) {
121
newSODesDefined = true;
122
ebyte[0] = ISO_ESC;
123
System.arraycopy(SODesig, 0, ebyte, 1, SODesig.length);
124
index = SODesig.length + 1;
125
}
126
if (!shiftout) {
127
newshiftout = true;
128
ebyte[index++] = ISO_SO;
129
}
130
ebyte[index++] = (byte)(convByte[0] & 0x7f);
131
ebyte[index++] = (byte)(convByte[1] & 0x7f);
132
} else {
133
if(convByte[0] == SS2) {
134
if (convByte[1] == PLANE2) {
135
if (!SS2DesDefined) {
136
newSS2DesDefined = true;
137
ebyte[0] = ISO_ESC;
138
System.arraycopy(SS2Desig, 0, ebyte, 1, SS2Desig.length);
139
index = SS2Desig.length + 1;
140
}
141
ebyte[index++] = ISO_ESC;
142
ebyte[index++] = ISO_SS2_7;
143
ebyte[index++] = (byte)(convByte[2] & 0x7f);
144
ebyte[index++] = (byte)(convByte[3] & 0x7f);
145
} else if (convByte[1] == PLANE3) {
146
if(!SS3DesDefined){
147
newSS3DesDefined = true;
148
ebyte[0] = ISO_ESC;
149
System.arraycopy(SS3Desig, 0, ebyte, 1, SS3Desig.length);
150
index = SS3Desig.length + 1;
151
}
152
ebyte[index++] = ISO_ESC;
153
ebyte[index++] = ISO_SS3_7;
154
ebyte[index++] = (byte)(convByte[2] & 0x7f);
155
ebyte[index++] = (byte)(convByte[3] & 0x7f);
156
}
157
}
158
}
159
return index;
160
}
161
162
private CoderResult encodeArrayLoop(CharBuffer src,
163
ByteBuffer dst)
164
{
165
char[] sa = src.array();
166
int sp = src.arrayOffset() + src.position();
167
int sl = src.arrayOffset() + src.limit();
168
169
byte[] da = dst.array();
170
int dp = dst.arrayOffset() + dst.position();
171
int dl = dst.arrayOffset() + dst.limit();
172
173
int outputSize;
174
byte[] outputByte = new byte[8];
175
newshiftout = shiftout;
176
newSODesDefined = SODesDefined;
177
newSS2DesDefined = SS2DesDefined;
178
newSS3DesDefined = SS3DesDefined;
179
180
try {
181
while (sp < sl) {
182
char c = sa[sp];
183
if (Character.isSurrogate(c)) {
184
if (sgp.parse(c, sa, sp, sl) < 0)
185
return sgp.error();
186
return sgp.unmappableResult();
187
}
188
189
if (c < 0x80) { // ASCII
190
if (shiftout){
191
newshiftout = false;
192
outputSize = 2;
193
outputByte[0] = ISO_SI;
194
outputByte[1] = (byte)(c & 0x7f);
195
} else {
196
outputSize = 1;
197
outputByte[0] = (byte)(c & 0x7f);
198
}
199
if(sa[sp] == '\n'){
200
newSODesDefined = false;
201
newSS2DesDefined = false;
202
newSS3DesDefined = false;
203
}
204
} else {
205
outputSize = unicodeToNative(c, outputByte);
206
if (outputSize == 0) {
207
return CoderResult.unmappableForLength(1);
208
}
209
}
210
if (dl - dp < outputSize)
211
return CoderResult.OVERFLOW;
212
213
for (int i = 0; i < outputSize; i++)
214
da[dp++] = outputByte[i];
215
sp++;
216
shiftout = newshiftout;
217
SODesDefined = newSODesDefined;
218
SS2DesDefined = newSS2DesDefined;
219
SS3DesDefined = newSS3DesDefined;
220
}
221
return CoderResult.UNDERFLOW;
222
} finally {
223
src.position(sp - src.arrayOffset());
224
dst.position(dp - dst.arrayOffset());
225
}
226
}
227
228
private CoderResult encodeBufferLoop(CharBuffer src,
229
ByteBuffer dst)
230
{
231
int outputSize;
232
byte[] outputByte = new byte[8];
233
newshiftout = shiftout;
234
newSODesDefined = SODesDefined;
235
newSS2DesDefined = SS2DesDefined;
236
newSS3DesDefined = SS3DesDefined;
237
int mark = src.position();
238
239
try {
240
while (src.hasRemaining()) {
241
char inputChar = src.get();
242
if (Character.isSurrogate(inputChar)) {
243
if (sgp.parse(inputChar, src) < 0)
244
return sgp.error();
245
return sgp.unmappableResult();
246
}
247
if (inputChar < 0x80) { // ASCII
248
if (shiftout){
249
newshiftout = false;
250
outputSize = 2;
251
outputByte[0] = ISO_SI;
252
outputByte[1] = (byte)(inputChar & 0x7f);
253
} else {
254
outputSize = 1;
255
outputByte[0] = (byte)(inputChar & 0x7f);
256
}
257
if (inputChar == '\n') {
258
newSODesDefined = false;
259
newSS2DesDefined = false;
260
newSS3DesDefined = false;
261
}
262
} else {
263
outputSize = unicodeToNative(inputChar, outputByte);
264
if (outputSize == 0) {
265
return CoderResult.unmappableForLength(1);
266
}
267
}
268
269
if (dst.remaining() < outputSize)
270
return CoderResult.OVERFLOW;
271
for (int i = 0; i < outputSize; i++)
272
dst.put(outputByte[i]);
273
mark++;
274
shiftout = newshiftout;
275
SODesDefined = newSODesDefined;
276
SS2DesDefined = newSS2DesDefined;
277
SS3DesDefined = newSS3DesDefined;
278
}
279
return CoderResult.UNDERFLOW;
280
} finally {
281
src.position(mark);
282
}
283
}
284
285
protected CoderResult encodeLoop(CharBuffer src,
286
ByteBuffer dst)
287
{
288
if (src.hasArray() && dst.hasArray())
289
return encodeArrayLoop(src, dst);
290
else
291
return encodeBufferLoop(src, dst);
292
}
293
}
294
}
295
296