CharsetASCII.java
9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.agl.charset;
import com.adobe.agl.charset.CharsetDecoderICU;
import com.adobe.agl.charset.CharsetEncoderICU;
import com.adobe.agl.charset.CharsetICU;
import com.adobe.agl.text.UTF16;
import com.adobe.agl.text.UnicodeSet;
import java.nio.*;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
class CharsetASCII
extends CharsetICU {
protected byte[] fromUSubstitution = new byte[]{26};
public CharsetASCII(String icuCanonicalName, String javaCanonicalName, String[] aliases) {
super(icuCanonicalName, javaCanonicalName, aliases);
this.maxBytesPerChar = 1;
this.minBytesPerChar = 1;
this.maxCharsPerByte = 1.0f;
}
public CharsetDecoder newDecoder() {
return new CharsetDecoderASCII(this);
}
public CharsetEncoder newEncoder() {
return new CharsetEncoderASCII(this);
}
void getUnicodeSetImpl(UnicodeSet setFillIn, int which) {
setFillIn.add(0, 127);
}
class CharsetEncoderASCII
extends CharsetEncoderICU {
private static final int NEED_TO_WRITE_BOM = 1;
public CharsetEncoderASCII(CharsetICU cs) {
super(cs, CharsetASCII.this.fromUSubstitution);
this.implReset();
}
protected void implReset() {
super.implReset();
this.fromUnicodeStatus = 1;
}
protected CoderResult encodeLoop(CharBuffer source, ByteBuffer target, IntBuffer offsets, boolean flush) {
CoderResult cr;
if (!source.hasRemaining()) {
return CoderResult.UNDERFLOW;
}
if (!target.hasRemaining()) {
return CoderResult.OVERFLOW;
}
int oldSource = source.position();
int oldTarget = target.position();
if (this.fromUChar32 != 0) {
cr = this.encodeTrail(source, (char)this.fromUChar32, flush);
} else if (source.hasArray() && target.hasArray()) {
int targetIndex;
int offset;
int targetLength;
int limit;
int targetOffset;
char[] sourceArray = source.array();
int sourceOffset = source.arrayOffset();
int sourceIndex = oldSource + sourceOffset;
int sourceLength = source.limit() - oldSource;
byte[] targetArray = target.array();
cr = this.encodeLoopCoreOptimized(source, target, sourceArray, targetArray, sourceIndex, offset = (targetIndex = oldTarget + (targetOffset = target.arrayOffset())) - sourceIndex, limit = (sourceLength < (targetLength = target.limit() - oldTarget) ? sourceLength : targetLength) + sourceIndex, flush);
if (cr == null) {
if (sourceLength <= targetLength) {
source.position(oldSource + sourceLength);
target.position(oldTarget + sourceLength);
cr = CoderResult.UNDERFLOW;
} else {
source.position(oldSource + targetLength);
target.position(oldTarget + targetLength);
cr = CoderResult.OVERFLOW;
}
}
} else {
try {
cr = this.encodeLoopCoreUnoptimized(source, target, flush);
}
catch (BufferUnderflowException ex) {
cr = CoderResult.UNDERFLOW;
}
catch (BufferOverflowException ex) {
source.position(source.position() - 1);
cr = CoderResult.OVERFLOW;
}
}
if (offsets != null) {
int count = target.position() - oldTarget;
int sourceIndex = -1;
while (--count >= 0) {
offsets.put(++sourceIndex);
}
}
return cr;
}
protected CoderResult encodeLoopCoreOptimized(CharBuffer source, ByteBuffer target, char[] sourceArray, byte[] targetArray, int oldSource, int offset, int limit, boolean flush) {
int i;
int ch = 0;
for (i = oldSource; i < limit && ((ch = sourceArray[i]) & 65408) == 0; ++i) {
targetArray[i + offset] = (byte)ch;
}
if ((ch & 65408) != 0) {
source.position(i + 1);
target.position(i + offset);
return this.encodeMalformedOrUnmappable(source, ch, flush);
}
return null;
}
protected CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, boolean flush) throws BufferUnderflowException, BufferOverflowException {
char ch;
while (((ch = source.get()) & 65408) == 0) {
target.put((byte)ch);
}
return this.encodeMalformedOrUnmappable(source, ch, flush);
}
protected final CoderResult encodeMalformedOrUnmappable(CharBuffer source, int ch, boolean flush) {
return UTF16.isSurrogate((char)ch) ? this.encodeTrail(source, (char)ch, flush) : CoderResult.unmappableForLength(1);
}
private final CoderResult encodeTrail(CharBuffer source, char lead, boolean flush) {
CoderResult cr = this.handleSurrogates(source, lead);
if (cr != null) {
return cr;
}
return CoderResult.unmappableForLength(2);
}
}
class CharsetDecoderASCII
extends CharsetDecoderICU {
public CharsetDecoderASCII(CharsetICU cs) {
super(cs);
}
protected CoderResult decodeLoop(ByteBuffer source, CharBuffer target, IntBuffer offsets, boolean flush) {
CoderResult cr;
if (!source.hasRemaining()) {
return CoderResult.UNDERFLOW;
}
if (!target.hasRemaining()) {
return CoderResult.OVERFLOW;
}
int oldSource = source.position();
int oldTarget = target.position();
if (source.hasArray() && target.hasArray()) {
int targetIndex;
int offset;
int targetLength;
int limit;
int targetOffset;
byte[] sourceArray = source.array();
int sourceOffset = source.arrayOffset();
int sourceIndex = oldSource + sourceOffset;
int sourceLength = source.limit() - oldSource;
char[] targetArray = target.array();
cr = this.decodeLoopCoreOptimized(source, target, sourceArray, targetArray, sourceIndex, offset = (targetIndex = oldTarget + (targetOffset = target.arrayOffset())) - sourceIndex, limit = (sourceLength < (targetLength = target.limit() - oldTarget) ? sourceLength : targetLength) + sourceIndex);
if (cr == null) {
if (sourceLength <= targetLength) {
source.position(oldSource + sourceLength);
target.position(oldTarget + sourceLength);
cr = CoderResult.UNDERFLOW;
} else {
source.position(oldSource + targetLength);
target.position(oldTarget + targetLength);
cr = CoderResult.OVERFLOW;
}
}
} else {
try {
cr = this.decodeLoopCoreUnoptimized(source, target);
}
catch (BufferUnderflowException ex) {
cr = CoderResult.UNDERFLOW;
}
catch (BufferOverflowException ex) {
source.position(source.position() - 1);
cr = CoderResult.OVERFLOW;
}
}
if (offsets != null) {
int count = target.position() - oldTarget;
int sourceIndex = -1;
while (--count >= 0) {
offsets.put(++sourceIndex);
}
}
return cr;
}
protected CoderResult decodeLoopCoreOptimized(ByteBuffer source, CharBuffer target, byte[] sourceArray, char[] targetArray, int oldSource, int offset, int limit) {
int i;
int ch = 0;
for (i = oldSource; i < limit && ((ch = sourceArray[i] & 255) & 128) == 0; ++i) {
targetArray[i + offset] = (char)ch;
}
if ((ch & 128) != 0) {
source.position(i + 1);
target.position(i + offset);
return this.decodeMalformedOrUnmappable(ch);
}
return null;
}
protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) throws BufferUnderflowException, BufferOverflowException {
int ch = 0;
while (((ch = source.get() & 255) & 128) == 0) {
target.put((char)ch);
}
return this.decodeMalformedOrUnmappable(ch);
}
protected CoderResult decodeMalformedOrUnmappable(int ch) {
this.toUBytesArray[0] = (byte)ch;
this.toULength = 1;
return CoderResult.malformedForLength(1);
}
}
}