CharsetEncoderICU.java
14.2 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.agl.charset;
import com.adobe.agl.charset.CharsetCallback;
import com.adobe.agl.charset.CharsetICU;
import com.adobe.agl.impl.Assert;
import com.adobe.agl.lang.UCharacter;
import com.adobe.agl.text.UTF16;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.IntBuffer;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
public abstract class CharsetEncoderICU
extends CharsetEncoder {
static final char MISSING_CHAR_MARKER = '\uffff';
byte[] errorBuffer = new byte[30];
int errorBufferLength = 0;
int fromUnicodeStatus;
int fromUChar32;
boolean useSubChar1;
boolean useFallback;
static final int EXT_MAX_UCHARS = 19;
int preFromUFirstCP;
char[] preFromUArray = new char[19];
int preFromUBegin;
int preFromULength;
char[] invalidUCharBuffer = new char[2];
int invalidUCharLength;
Object fromUContext;
private CharsetCallback.Encoder onUnmappableInput = CharsetCallback.FROM_U_CALLBACK_STOP;
private CharsetCallback.Encoder onMalformedInput = CharsetCallback.FROM_U_CALLBACK_STOP;
CharsetCallback.Encoder fromCharErrorBehaviour;
private static final CharBuffer EMPTY = CharBuffer.allocate(0);
CharsetEncoderICU(CharsetICU cs, byte[] replacement) {
super(cs, (cs.minBytesPerChar + cs.maxBytesPerChar) / 2, cs.maxBytesPerChar, replacement);
this.fromCharErrorBehaviour = new CharsetCallback.Encoder(){
public CoderResult call(CharsetEncoderICU encoder, Object context, CharBuffer source, ByteBuffer target, IntBuffer offsets, char[] buffer, int length, int cp, CoderResult cr) {
if (cr.isUnmappable()) {
return CharsetEncoderICU.this.onUnmappableInput.call(encoder, context, source, target, offsets, buffer, length, cp, cr);
}
return CharsetEncoderICU.this.onMalformedInput.call(encoder, context, source, target, offsets, buffer, length, cp, cr);
}
};
}
public boolean isFallbackUsed() {
return this.useFallback;
}
public void setFallbackUsed(boolean usesFallback) {
this.useFallback = usesFallback;
}
final boolean isFromUUseFallback(int c) {
return this.useFallback || UCharacter.getType(c) == 17;
}
static final boolean isFromUUseFallback(boolean iUseFallback, int c) {
return iUseFallback || UCharacter.getType(c) == 17;
}
protected void implOnMalformedInput(CodingErrorAction newAction) {
this.onMalformedInput = CharsetEncoderICU.getCallback(newAction);
}
protected void implOnUnmappableCharacter(CodingErrorAction newAction) {
this.onUnmappableInput = CharsetEncoderICU.getCallback(newAction);
}
public final void setFromUCallback(CoderResult err, CharsetCallback.Encoder newCallback, Object newContext) {
if (err.isMalformed()) {
this.onMalformedInput = newCallback;
} else if (err.isUnmappable()) {
this.onUnmappableInput = newCallback;
}
if (this.fromUContext == null || !this.fromUContext.equals(newContext)) {
this.setFromUContext(newContext);
}
}
public final void setFromUContext(Object newContext) {
this.fromUContext = newContext;
}
private static CharsetCallback.Encoder getCallback(CodingErrorAction action) {
if (action == CodingErrorAction.REPLACE) {
return CharsetCallback.FROM_U_CALLBACK_SUBSTITUTE;
}
if (action == CodingErrorAction.IGNORE) {
return CharsetCallback.FROM_U_CALLBACK_SKIP;
}
return CharsetCallback.FROM_U_CALLBACK_STOP;
}
protected CoderResult implFlush(ByteBuffer out) {
return this.encode(EMPTY, out, null, true);
}
protected void implReset() {
this.errorBufferLength = 0;
this.fromUnicodeStatus = 0;
this.fromUChar32 = 0;
this.fromUnicodeReset();
}
private void fromUnicodeReset() {
this.preFromUBegin = 0;
this.preFromUFirstCP = -1;
this.preFromULength = 0;
}
protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
if (!in.hasRemaining() && this.errorBufferLength == 0) {
this.fromUChar32 = 0;
return CoderResult.UNDERFLOW;
}
in.position(in.position() + this.fromUCountPending());
CoderResult ret = this.encode(in, out, null, false);
this.setSourcePosition(in);
return ret;
}
abstract CoderResult encodeLoop(CharBuffer var1, ByteBuffer var2, IntBuffer var3, boolean var4);
final CoderResult encode(CharBuffer source, ByteBuffer target, IntBuffer offsets, boolean flush) {
if (target == null || source == null) {
throw new IllegalArgumentException();
}
if (this.errorBufferLength > 0) {
byte[] overflowArray = this.errorBuffer;
int length = this.errorBufferLength;
int i = 0;
do {
if (target.remaining() == 0) {
int j = 0;
do {
overflowArray[j++] = overflowArray[i++];
} while (i < length);
this.errorBufferLength = (byte)j;
return CoderResult.OVERFLOW;
}
target.put(overflowArray[i++]);
if (offsets == null) continue;
offsets.put(-1);
} while (i < length);
this.errorBufferLength = 0;
}
if (!flush && source.remaining() == 0 && this.preFromULength >= 0) {
return CoderResult.UNDERFLOW;
}
return this.fromUnicodeWithCallback(source, target, offsets, flush);
}
/*
* Unable to fully structure code
* Enabled aggressive block sorting
* Lifted jumps to return sites
*/
final CoderResult fromUnicodeWithCallback(CharBuffer source, ByteBuffer target, IntBuffer offsets, boolean flush) {
replayArray = CharBuffer.allocate(19);
replayArrayIndex = 0;
cr = CoderResult.UNDERFLOW;
sourceIndex = 0;
if (this.preFromULength >= 0) {
realSource = null;
realFlush = false;
} else {
realSource = source;
realFlush = flush;
replayArray.put(this.preFromUArray, 0, - this.preFromULength);
source = replayArray;
source.position(replayArrayIndex);
source.limit(replayArrayIndex - this.preFromULength);
flush = false;
this.preFromULength = 0;
}
block0 : do {
converterSawEndOfInput = (cr = this.encodeLoop(source, target, offsets, flush)).isUnderflow() != false && flush != false && source.remaining() == 0 && this.fromUChar32 == 0;
calledCallback = false;
errorInputLength = 0;
do {
if (this.preFromULength < 0) {
if (realSource == null) {
realSource = source;
realFlush = flush;
replayArray.put(this.preFromUArray, 0, - this.preFromULength);
source = replayArray;
source.position(replayArrayIndex);
source.limit(replayArrayIndex - this.preFromULength);
flush = false;
if ((sourceIndex += this.preFromULength) < 0) {
sourceIndex = -1;
}
this.preFromULength = 0;
} else {
Assert.assrt(realSource == null);
}
}
sBufferIndex = source.position();
if (cr.isUnderflow()) {
if (sBufferIndex < source.limit()) continue block0;
if (realSource != null) {
source = realSource;
flush = realFlush;
sourceIndex = source.position();
realSource = null;
continue block0;
}
if (flush && this.fromUChar32 != 0) {
cr = CoderResult.malformedForLength(1);
calledCallback = false;
} else {
if (flush == false) return cr;
if (converterSawEndOfInput) ** break;
continue block0;
this.implReset();
return cr;
}
}
if (calledCallback || cr.isOverflow() || !cr.isMalformed() && !cr.isUnmappable()) {
if (realSource == null) return cr;
length = source.remaining();
if (length > 0) {
source.get(this.preFromUArray, 0, length);
this.preFromULength = (byte)(- length);
}
source = realSource;
flush = realFlush;
return cr;
}
codePoint = this.fromUChar32;
this.invalidUCharLength = errorInputLength = UTF16.append(this.invalidUCharBuffer, 0, this.fromUChar32);
this.fromUChar32 = 0;
cr = this.fromCharErrorBehaviour.call(this, this.fromUContext, source, target, offsets, this.invalidUCharBuffer, this.invalidUCharLength, codePoint, cr);
calledCallback = true;
} while (true);
break;
} while (true);
}
public boolean canEncode(int[] codepoint) {
return true;
}
public boolean isLegalReplacement(byte[] repl) {
return true;
}
static final CoderResult fromUWriteBytes(CharsetEncoderICU cnv, byte[] bytesArray, int bytesBegin, int bytesLength, ByteBuffer out, IntBuffer offsets, int sourceIndex) {
int obl = bytesLength;
CoderResult cr = CoderResult.UNDERFLOW;
int bytesLimit = bytesBegin + bytesLength;
try {
while (bytesBegin < bytesLimit) {
out.put(bytesArray[bytesBegin]);
++bytesBegin;
}
bytesLength = 0;
}
catch (BufferOverflowException ex) {
cr = CoderResult.OVERFLOW;
}
if (offsets != null) {
while (obl > bytesLength) {
offsets.put(sourceIndex);
--obl;
}
}
cnv.errorBufferLength = bytesLimit - bytesBegin;
if (cnv.errorBufferLength > 0) {
int index = 0;
while (bytesBegin < bytesLimit) {
cnv.errorBuffer[index++] = bytesArray[bytesBegin++];
}
cr = CoderResult.OVERFLOW;
}
return cr;
}
int fromUCountPending() {
if (this.preFromULength > 0) {
return UTF16.getCharCount(this.preFromUFirstCP) + this.preFromULength;
}
if (this.preFromULength < 0) {
return - this.preFromULength;
}
if (this.fromUChar32 > 0) {
return 1;
}
if (this.preFromUFirstCP > 0) {
return UTF16.getCharCount(this.preFromUFirstCP);
}
return 0;
}
private final void setSourcePosition(CharBuffer source) {
source.position(source.position() - this.fromUCountPending());
}
CoderResult cbFromUWriteSub(CharsetEncoderICU encoder, CharBuffer source, ByteBuffer target, IntBuffer offsets) {
CharsetICU cs = (CharsetICU)encoder.charset();
byte[] sub = encoder.replacement();
if (cs.subChar1 != 0 && encoder.invalidUCharBuffer[0] <= '\u00ff') {
return CharsetEncoderICU.fromUWriteBytes(encoder, new byte[]{cs.subChar1}, 0, 1, target, offsets, source.position());
}
return CharsetEncoderICU.fromUWriteBytes(encoder, sub, 0, sub.length, target, offsets, source.position());
}
CoderResult cbFromUWriteUChars(CharsetEncoderICU encoder, CharBuffer source, ByteBuffer target, IntBuffer offsets) {
CoderResult cr = CoderResult.UNDERFLOW;
int offsetIndex = source.position();
cr = encoder.encode(source, target, null, false);
if (offsets != null) {
for (int oldTargetPosition = target.position(); target.position() != oldTargetPosition; ++oldTargetPosition) {
offsets.put(offsetIndex);
}
}
if (cr.isOverflow()) {
int errBuffLen = encoder.errorBufferLength;
ByteBuffer newTarget = ByteBuffer.wrap(encoder.errorBuffer);
newTarget.position(errBuffLen);
encoder.errorBufferLength = 0;
encoder.encode(source, newTarget, null, false);
encoder.errorBuffer = newTarget.array();
encoder.errorBufferLength = newTarget.position();
}
return cr;
}
final CoderResult handleSurrogates(CharBuffer source, char lead) {
if (!UTF16.isLeadSurrogate(lead)) {
this.fromUChar32 = lead;
return CoderResult.malformedForLength(1);
}
if (!source.hasRemaining()) {
this.fromUChar32 = lead;
return CoderResult.UNDERFLOW;
}
char trail = source.get();
if (!UTF16.isTrailSurrogate(trail)) {
this.fromUChar32 = lead;
source.position(source.position() - 1);
return CoderResult.malformedForLength(1);
}
this.fromUChar32 = UCharacter.getCodePoint(lead, trail);
return null;
}
final CoderResult handleSurrogates(char[] sourceArray, int sourceIndex, int sourceLimit, char lead) {
if (!UTF16.isLeadSurrogate(lead)) {
this.fromUChar32 = lead;
return CoderResult.malformedForLength(1);
}
if (sourceIndex >= sourceLimit) {
this.fromUChar32 = lead;
return CoderResult.UNDERFLOW;
}
char trail = sourceArray[sourceIndex];
if (!UTF16.isTrailSurrogate(trail)) {
this.fromUChar32 = lead;
return CoderResult.malformedForLength(1);
}
this.fromUChar32 = UCharacter.getCodePoint(lead, trail);
return null;
}
}