-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFastCharset.java
More file actions
339 lines (299 loc) · 10.5 KB
/
FastCharset.java
File metadata and controls
339 lines (299 loc) · 10.5 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
package roj.text;
import org.jetbrains.annotations.Nullable;
import roj.compiler.runtime.SwitchMap;
import roj.util.*;
import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.function.IntConsumer;
import static java.lang.Character.*;
/**
* 高速字符编解码,至少是CharsetEncoder的两倍
* FixedIn: 固定输入,适用已知长度
* FixedOut: 固定输出,适用循环和缓冲的组合
*
* @author Roj234
* @since 2023/5/14 22:03
*/
public abstract class FastCharset {
public static FastCharset ISO_8859_1() {return LATIN1.INSTANCE;}
public static FastCharset GB18030() {return GB18030.INSTANCE;}
public static FastCharset UTF8() {return UTF8.INSTANCE;}
/**
* UTF_16本地字符集,基于处理器的Endian
*/
public static FastCharset UTF16() {return UTF16n.INSTANCE;}
public static FastCharset UTF16LE() {return JVM.BIG_ENDIAN ? UTF16i.INSTANCE : UTF16n.INSTANCE;}
public static FastCharset UTF16BE() {return JVM.BIG_ENDIAN ? UTF16n.INSTANCE : UTF16i.INSTANCE;}
@Nullable public static FastCharset getInstance(Charset charset) {
return switch (CHARSET_LOOKUP.get(charset.name())) {
case 1 -> UTF8.INSTANCE;
case 2 -> LATIN1.INSTANCE;
case 3 -> GB18030.INSTANCE;
case 4 -> UTF16n.INSTANCE;
case 5 -> UTF16i.INSTANCE;
default -> null;
};
}
private static final SwitchMap CHARSET_LOOKUP = SwitchMap.Builder.builder(9, true)
.add("UTF-8", 1)
.add("ISO-8859-1", 2)
.add("GB2312", 3)
.add("x-mswin-936", 3)
.add("GBK", 3)
.add("GB18030", 3)
.add("UTF-16", 4)
.add("UTF-16LE", !JVM.BIG_ENDIAN ? 4 : 5)
.add("UTF-16BE", JVM.BIG_ENDIAN ? 4 : 5)
.build();
/**
* @return 字符集规范名称
*/
public abstract String name();
/**
* 在编解码失败时抛出异常,而不是静默处理
*/
public FastCharset throwException(boolean doThrow) {return this;}
public static final char REPLACEMENT = '\uFFFD';
static boolean isSurrogate(int h) {return h >= MIN_HIGH_SURROGATE && h <= MAX_LOW_SURROGATE;}
static int codepoint(int h, int l) {
if (h >= MIN_LOW_SURROGATE) throw new IllegalArgumentException("unexpected low surrogate U+"+Integer.toHexString(h));
if (l < MIN_LOW_SURROGATE || l > MAX_LOW_SURROGATE) throw new IllegalStateException("invalid surrogate pair "+h+","+l);
return ((h << 10) + l) + (MIN_SUPPLEMENTARY_CODE_POINT - (MIN_HIGH_SURROGATE << 10) - MIN_LOW_SURROGATE);
}
static int codepointNoExc(int h, int l) {
if (h >= MIN_LOW_SURROGATE || l < MIN_LOW_SURROGATE || l > MAX_LOW_SURROGATE) return REPLACEMENT;
return ((h << 10) + l) + (MIN_SUPPLEMENTARY_CODE_POINT - (MIN_HIGH_SURROGATE << 10) - MIN_LOW_SURROGATE);
}
public final void encodeFixedIn(CharSequence s, DynByteBuf out) { encodeFixedIn(s, 0, s.length(), out); }
public final void encodeFixedIn(CharSequence s, int off, int end, DynByteBuf out) {
int space = byteCount(s, off, end);
if (!out.ensureWritable(space)) throw new IllegalArgumentException("没有足够的空间容纳编码后的"+space+"字节:"+out.writableBytes());
int ow = out.wIndex();
off = encodeLoop(s, off, end, out, space, 0);
assert off == end : "off="+off+",end="+end;
assert out.wIndex() == ow+space;
}
public final int encodePreAlloc(CharSequence s, DynByteBuf out, int byteCount) { return encodePreAlloc(s, 0, s.length(), out, byteCount); }
/**
* @return out的可用空间
*/
public final int encodePreAlloc(CharSequence s, int off, int end, DynByteBuf out, int byteCount) {
int ow = out.wIndex();
off = encodeLoop(s, off, end, out, byteCount, 0);
if (off != end) throw new IllegalArgumentException(!out.isWritable()?"缓冲区满":"预算的长度有误,并非"+byteCount);
return (out.wIndex()-ow)-byteCount;
}
public final void encodeFully(CharSequence s, DynByteBuf out) { encodeFully(s, 0, s.length(), out); }
public final void encodeFully(CharSequence s, int off, int end, DynByteBuf out) {
int i = encodeLoop(s, off, end, out, Integer.MAX_VALUE, 1024);
if (i < end) throw new IllegalStateException("Trailing surrogate?");
}
public final int encodeFixedOut(CharSequence s, int off, int end, DynByteBuf out) {
return encodeLoop(s, off, end, out, out.writableBytes(), 0);
}
public final int encodeFixedOut(CharSequence s, int off, int end, DynByteBuf out, int outMax) {
return encodeLoop(s, off, end, out, outMax, 0);
}
private static boolean alert;
/**
* @param s CharList|CharBuffer|CharSlice|char[]|String|CharSequence
* @return off
*/
public final int encodeLoop(Object s, int off, int end, DynByteBuf out, int outMax, int minSpace) {
if (!out.isReal()) {
ByteList bb = new ByteList(ArrayCache.getIOBuffer());
while (off < end) {
bb.clear();
off = encodeLoop(s,off,end,bb,bb.capacity(),0);
out.put(bb);
}
bb.release();
return off;
}
int off1 = 0;
char[] arr;
Class<?> k = s.getClass();
found: {
if (k == CharList.class) {
CharList sb = (CharList) s;
arr = sb.list;
break found;
} else if (s instanceof CharBuffer) {
CharBuffer sb = (CharBuffer) s;
if (sb.hasArray()) {
arr = sb.array();
off1 = sb.arrayOffset();
off += off1;
end += off1;
break found;
}
} else if (k == CharList.Slice.class) {
CharList.Slice sb = (CharList.Slice) s;
arr = sb.list;
off1 = sb.arrayOffset();
off += off1;
end += off1;
break found;
} else if (k == char[].class) {
arr = (char[]) s;
break found;
}
if (s.getClass() == String.class) {
arr = ArrayCache.getIOCharBuffer();
String ss = s.toString();
try {
while (true) {
int myLen = Math.min(end-off, arr.length);
ss.getChars(off, off+myLen, arr, 0);
int ow = out.wIndex();
int encoded = encodeLoop(arr,0,myLen,out,outMax,minSpace);
off += encoded;
outMax -= out.wIndex()-ow;
if (off == end || encoded < myLen) return off;
}
} finally {
ArrayCache.putArray(arr);
}
}
if (!alert) {
alert = true;
new Throwable("It is recommended to pre-copy your " + s.getClass().getName() + " to a CharBuffer or CharList").printStackTrace();
}
arr = ArrayCache.getIOCharBuffer();
CharSequence cs = (CharSequence) s;
try {
while (true) {
int myLen = Math.min(end-off, arr.length);
for (int i = 0; i < myLen; i++) arr[i] = cs.charAt(off+i);
int ow = out.wIndex();
int encoded = encodeLoop(arr,0,myLen,out,outMax,minSpace);
off += encoded;
outMax -= out.wIndex()-ow;
if (off == end || encoded < myLen) return off;
}
} finally {
ArrayCache.putArray(arr);
}
}
while (true) {
if (out.unsafeWritableBytes() < minSpace) {
out.ensureWritable(out.unsafeWritableBytes() + minSpace);
if (!out.isWritable()) throw new IllegalArgumentException("没有足够的空间:"+out.unsafeWritableBytes());
}
int uw = Math.min(outMax, out.unsafeWritableBytes());
long x = fastEncode(arr, off, end, out.array(), out._unsafeAddr()+out.wIndex(), uw);
off = (int) (x >>> 32);
int delta = (uw-(int) x);
outMax -= delta;
out.wIndex(out.wIndex() + delta);
if (off == end || outMax == 0 || delta == 0) break;
}
return off-off1;
}
public final void decodeFixedIn(DynByteBuf in, int len, Appendable out) {
int end = in.rIndex + len;
if (end > in.wIndex()) throw new IllegalArgumentException("没有"+len+"字节可供读取:"+in.readableBytes());
decodeLoop(in,len,out,Integer.MAX_VALUE,false);
}
/**
* @return outMax
*/
public final int decodeFixedOut(DynByteBuf in, int len, Appendable out, int outMax) {
int end = in.rIndex + len;
if (end > in.wIndex()) throw new IllegalArgumentException("没有"+len+"字节可供读取:"+in.readableBytes());
return decodeLoop(in,len,out,outMax,true);
}
/**
* @return outMax
*/
public final int decodeLoop(DynByteBuf in, int len, Appendable out, int outMax, boolean partial) {
char[] arr;
int off;
int unsafeWritable;
int kind;
found: {
if (out instanceof CharList) {
CharList sb = (CharList) out;
arr = sb.list;
off = sb.length();
unsafeWritable = arr.length - sb.length();
kind = 0;
break found;
} else if (out instanceof CharBuffer) {
CharBuffer sb = (CharBuffer) out;
if (sb.hasArray()) {
arr = sb.array();
off = sb.arrayOffset() + sb.position();
unsafeWritable = sb.remaining();
kind = 1;
if (unsafeWritable == 0 && outMax > 0) throw new IllegalArgumentException();
break found;
}
}
arr = ArrayCache.getIOCharBuffer();
off = 0;
unsafeWritable = arr.length;
kind = 2;
}
int rin = in.rIndex;
if (rin < 0) throw new IllegalArgumentException("in.rIndex < 0");
len += rin;
try {
while (true) {
int uw = Math.min(outMax, unsafeWritable);
long x = fastDecode(in.array(),in._unsafeAddr(),rin,len,arr,off,uw);
rin = (int) (x >>> 32);
int delta = (int) x - off;
outMax -= delta;
switch (kind) {
case 0 -> {
off += delta;
((CharList) out).len = off;
unsafeWritable -= delta;
}
case 1 -> {
off += delta;
((CharBuffer) out).position(off);
unsafeWritable -= delta;
}
case 2 -> out.append(new CharList.Slice(arr, 0, delta));
}
if (rin == len || outMax == 0) break;
if (kind == 0 && unsafeWritable < 1024) {
CharList sb = (CharList) out;
sb.ensureCapacity(arr.length+1024);
arr = sb.list;
unsafeWritable = arr.length - off;
} else if (delta == 0) { // truncate
if (unsafeWritable > 0 && !partial) throw new IllegalArgumentException("被截断");
break;
}
}
} catch (IOException e) {
Helpers.athrow(e);
} finally {
in.rIndex = rin;
if (kind == 2) ArrayCache.putArray(arr);
}
return outMax;
}
/**
* @return 高32位: 新off, 低32位: 新out_len
*/
public abstract long fastEncode(char[] in_ref, int in_off, int i_end, Object out_ref, long out_off, int out_len);
/**
* @return 高32位: 新pos, 低32位: 新off
*/
public abstract long fastDecode(Object ref, long base, int pos, int end, char[] out, int off, int outMax);
public static final int TRUNCATED = -1, MALFORMED = -2;
public final void validate(DynByteBuf in, IntConsumer codepointAcceptor) {
fastValidate(in.array(),in._unsafeAddr()+in.rIndex,in._unsafeAddr()+in.wIndex(),codepointAcceptor);
in.rIndex = in.wIndex();
}
public abstract void fastValidate(Object ref, long base, long end, IntConsumer verifier);
public final int byteCount(CharSequence s) { return byteCount(s, 0, s.length()); }
public abstract int byteCount(CharSequence s, int off, int len);
public int addBOM(Object ref, long addr) {return 0;}
public abstract int encodeSize(int codepoint);
}