Repository navigation
Expand file tree
/
Copy pathEncodingService.java
More file actions
476 lines (408 loc) · 17.8 KB
/
Copy pathEncodingService.java
File metadata and controls
476 lines (408 loc) · 17.8 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package org.jruby.runtime.encoding;
import org.jcodings.Encoding;
import org.jcodings.EncodingDB;
import org.jcodings.EncodingDB.Entry;
import org.jcodings.ascii.AsciiTables;
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.util.CaseInsensitiveBytesHash;
import org.jcodings.util.Hash.HashEntryIterator;
import org.jruby.Ruby;
import org.jruby.RubyConverter;
import org.jruby.RubyEncoding;
import org.jruby.exceptions.MainExitException;
import org.jruby.platform.Platform;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import java.io.Console;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Iterator;
import org.jcodings.specific.USASCIIEncoding;
import org.jruby.RubyFixnum;
import org.jruby.ext.nkf.RubyNKF;
public final class EncodingService {
private final CaseInsensitiveBytesHash<Entry> encodings;
private final CaseInsensitiveBytesHash<Entry> aliases;
// for fast lookup: encoding entry => org.jruby.RubyEncoding
private final IRubyObject[] encodingList;
// for fast lookup: org.joni.encoding.Encoding => org.jruby.RubyEncoding
private RubyEncoding[] encodingIndex = new RubyEncoding[4];
// the runtime
private final Ruby runtime;
private final Encoding ascii8bit;
private final Encoding javaDefault;
private static final ByteList LOCALE_BL = ByteList.create("locale");
private static final ByteList EXTERNAL_BL = ByteList.create("external");
private static final ByteList INTERNAL_BL = ByteList.create("internal");
private static final ByteList FILESYSTEM_BL = ByteList.create("filesystem");
public EncodingService (Ruby runtime) {
this.runtime = runtime;
encodings = EncodingDB.getEncodings();
aliases = EncodingDB.getAliases();
ascii8bit = encodings.get("ASCII-8BIT".getBytes()).getEncoding();
Charset javaDefaultCharset = Charset.defaultCharset();
ByteList javaDefaultBL = new ByteList(javaDefaultCharset.name().getBytes());
Entry javaDefaultEntry = findEncodingOrAliasEntry(javaDefaultBL);
javaDefault = javaDefaultEntry == null ? ascii8bit : javaDefaultEntry.getEncoding();
encodingList = new IRubyObject[encodings.size()];
if (runtime.is1_9()) {
RubyEncoding.createEncodingClass(runtime);
RubyConverter.createConverterClass(runtime);
defineEncodings();
defineAliases();
// External should always have a value, but Encoding.external_encoding{,=} will lazily setup
String encoding = runtime.getInstanceConfig().getExternalEncoding();
if (encoding != null && !encoding.equals("")) {
Encoding loadedEncoding = loadEncoding(ByteList.create(encoding));
if (loadedEncoding == null) throw new MainExitException(1, "unknown encoding name - " + encoding);
runtime.setDefaultExternalEncoding(loadedEncoding);
} else {
Encoding consoleEncoding = getConsoleEncoding();
Encoding availableEncoding = consoleEncoding == null ? getLocaleEncoding() : consoleEncoding;
runtime.setDefaultExternalEncoding(availableEncoding);
}
encoding = runtime.getInstanceConfig().getInternalEncoding();
if (encoding != null && !encoding.equals("")) {
Encoding loadedEncoding = loadEncoding(ByteList.create(encoding));
if (loadedEncoding == null) throw new MainExitException(1, "unknown encoding name - " + encoding);
runtime.setDefaultInternalEncoding(loadedEncoding);
}
}
}
/**
* Since Java 1.6, class {@link java.io.Console} is available.
* But the encoding or codepage of the underlying connected
* console is currently private. Had to use Reflection to get it.
*
* @return console codepage
*/
private Encoding getConsoleEncoding() {
if (!Platform.IS_WINDOWS) return null;
Encoding consoleEncoding = null;
try {
Console console = System.console();
if (console != null) {
final String CONSOLE_CHARSET = "cs";
Field fcs = Console.class.getDeclaredField(CONSOLE_CHARSET);
fcs.setAccessible(true);
Charset cs = (Charset) fcs.get(console);
consoleEncoding = loadEncoding(ByteList.create(cs.name()));
}
} catch (Throwable e) { // to cover both Exceptions and Errors
// just fall back on local encoding above
}
return consoleEncoding;
}
// mri: rb_usascii_encoding
public Encoding getUSAsciiEncoding() {
return USASCIIEncoding.INSTANCE;
}
// mri: rb_ascii8bit_encoding
public Encoding getAscii8bitEncoding() {
return ascii8bit;
}
// mri: rb_filesystem_encoding
public Encoding getFileSystemEncoding(Ruby runtime) {
return SpecialEncoding.FILESYSTEM.toEncoding(runtime);
}
public CaseInsensitiveBytesHash<Entry> getEncodings() {
return encodings;
}
public CaseInsensitiveBytesHash<Entry> getAliases() {
return aliases;
}
public Entry findEncodingEntry(ByteList bytes) {
return encodings.get(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getBegin() + bytes.getRealSize());
}
public Entry findAliasEntry(ByteList bytes) {
return aliases.get(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getBegin() + bytes.getRealSize());
}
public Entry findEncodingOrAliasEntry(ByteList bytes) {
Entry e = findEncodingEntry(bytes);
return e != null ? e : findAliasEntry(bytes);
}
// rb_locale_charmap...mostly
public Encoding getLocaleEncoding() {
Entry entry = findEncodingOrAliasEntry(new ByteList(Charset.defaultCharset().name().getBytes()));
return entry == null ? ASCIIEncoding.INSTANCE : entry.getEncoding();
}
public IRubyObject[] getEncodingList() {
return encodingList;
}
public Encoding loadEncoding(ByteList name) {
Entry entry = findEncodingOrAliasEntry(name);
if (entry == null) return null;
Encoding enc = entry.getEncoding(); // load the encoding
int index = enc.getIndex();
if (index >= encodingIndex.length) {
RubyEncoding tmp[] = new RubyEncoding[index + 4];
System.arraycopy(encodingIndex, 0, tmp, 0, encodingIndex.length);
encodingIndex = tmp;
}
encodingIndex[index] = (RubyEncoding)encodingList[entry.getIndex()];
return enc;
}
public RubyEncoding getEncoding(Encoding enc) {
int index = enc.getIndex();
RubyEncoding rubyEncoding;
if (index < encodingIndex.length && (rubyEncoding = encodingIndex[index]) != null) {
return rubyEncoding;
}
enc = loadEncoding(new ByteList(enc.getName(), false));
return encodingIndex[enc.getIndex()];
}
private void defineEncodings() {
HashEntryIterator hei = encodings.entryIterator();
while (hei.hasNext()) {
CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)hei.next());
Entry ee = e.value;
RubyEncoding encoding = RubyEncoding.newEncoding(runtime, e.bytes, e.p, e.end, ee.isDummy());
encodingList[ee.getIndex()] = encoding;
defineEncodingConstants(runtime, encoding, e.bytes, e.p, e.end);
}
}
private void defineAliases() {
HashEntryIterator hei = aliases.entryIterator();
while (hei.hasNext()) {
CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)hei.next());
Entry ee = e.value;
RubyEncoding encoding = (RubyEncoding)encodingList[ee.getIndex()];
defineEncodingConstants(runtime, encoding, e.bytes, e.p, e.end);
}
}
private void defineEncodingConstants(Ruby runtime, RubyEncoding encoding, byte[]name, int p,
int end) {
Encoding enc = ASCIIEncoding.INSTANCE;
int s = p;
int code = name[s] & 0xff;
if (enc.isDigit(code)) return;
boolean hasUpper = false;
boolean hasLower = false;
if (enc.isUpper(code)) {
hasUpper = true;
while (++s < end && (enc.isAlnum(name[s] & 0xff) || name[s] == (byte)'_')) {
if (enc.isLower(name[s] & 0xff)) hasLower = true;
}
}
boolean isValid = false;
if (s >= end) {
isValid = true;
defineEncodingConstant(runtime, encoding, name, p, end);
}
if (!isValid || hasLower) {
if (!hasLower || !hasUpper) {
do {
code = name[s] & 0xff;
if (enc.isLower(code)) hasLower = true;
if (enc.isUpper(code)) hasUpper = true;
} while (++s < end && (!hasLower || !hasUpper));
}
byte[]constName = new byte[end - p];
System.arraycopy(name, p, constName, 0, end - p);
s = 0;
code = constName[s] & 0xff;
if (!isValid) {
if (enc.isLower(code)) constName[s] = AsciiTables.ToUpperCaseTable[code];
for (; s < constName.length; ++s) {
if (!enc.isAlnum(constName[s] & 0xff)) constName[s] = (byte)'_';
}
if (hasUpper) {
defineEncodingConstant(runtime, encoding, constName, 0, constName.length);
}
}
if (hasLower) {
for (s = 0; s < constName.length; ++s) {
code = constName[s] & 0xff;
if (enc.isLower(code)) constName[s] = AsciiTables.ToUpperCaseTable[code];
}
defineEncodingConstant(runtime, encoding, constName, 0, constName.length);
}
}
}
private void defineEncodingConstant(Ruby runtime, RubyEncoding encoding, byte[]constName,
int constP, int constEnd) {
runtime.getEncoding().defineConstant(new String(constName, constP , constEnd), encoding);
}
public IRubyObject getDefaultExternal() {
IRubyObject defaultExternal = convertEncodingToRubyEncoding(runtime.getDefaultExternalEncoding());
if (defaultExternal.isNil()) {
// TODO: MRI seems to default blindly to US-ASCII and we were using Charset default from Java...which is right?
ByteList encodingName = ByteList.create("US-ASCII");
Encoding encoding = runtime.getEncodingService().loadEncoding(encodingName);
runtime.setDefaultExternalEncoding(encoding);
defaultExternal = convertEncodingToRubyEncoding(encoding);
}
return defaultExternal;
}
public IRubyObject getDefaultInternal() {
return convertEncodingToRubyEncoding(runtime.getDefaultInternalEncoding());
}
public IRubyObject convertEncodingToRubyEncoding(Encoding defaultEncoding) {
return defaultEncoding != null ? getEncoding(defaultEncoding) : runtime.getNil();
}
public Encoding getJavaDefault() {
return javaDefault;
}
public Encoding getEncodingFromObject(IRubyObject arg) {
if (arg == null) return null;
Encoding encoding = null;
if (arg instanceof RubyEncoding) {
encoding = ((RubyEncoding) arg).getEncoding();
} else if (arg instanceof RubyFixnum && RubyNKF.NKFCharsetMap.containsKey((int)arg.convertToInteger().getLongValue())) {
return getEncodingFromNKFId(arg);
} else if (!arg.isNil()) {
encoding = arg.convertToString().toEncoding(runtime);
}
return encoding;
}
private Encoding getEncodingFromNKFId(IRubyObject id) {
String name = RubyNKF.NKFCharsetMap.get((int)id.convertToInteger().getLongValue());
HashEntryIterator hei = encodings.entryIterator();
while (hei.hasNext()) {
CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)hei.next());
EncodingDB.Entry ee = e.value;
String className = ee.getEncodingClass();
if (className.equals(name)) {
Encoding enc = ee.getEncoding();
return enc;
}
}
return null;
}
public Encoding getEncodingFromString(String string) {
if (string == null) return null;
ByteList name = new ByteList(ByteList.plain(string));
checkAsciiEncodingName(name);
SpecialEncoding special = SpecialEncoding.valueOf(name);
if (special != null) {
return special.toEncoding(runtime);
}
return findEncodingWithError(name);
}
/**
* Find an encoding given a Ruby object, coercing it to a String in the process.
*
* @param str the object to coerce and use to look up encoding. The coerced String
* must be ASCII-compatible.
* @return the Encoding object found, nil (for internal), or raises ArgumentError
*/
public Encoding findEncoding(IRubyObject str) {
ByteList name = str.convertToString().getByteList();
checkAsciiEncodingName(name);
SpecialEncoding special = SpecialEncoding.valueOf(name);
if (special != null) {
return special.toEncoding(runtime);
}
return findEncodingWithError(name);
}
/**
* Find an encoding given a Ruby object, coercing it to a String in the process.
*
* @param str the object to coerce and use to look up encoding. The coerced String
* must be ASCII-compatible.
* @return the Encoding object found, nil (for internal), or raises ArgumentError
*/
public Entry findEntry(IRubyObject str) {
ByteList name = str.convertToString().getByteList();
checkAsciiEncodingName(name);
SpecialEncoding special = SpecialEncoding.valueOf(name);
if (special != null) {
return findEntryFromEncoding(special.toEncoding(runtime));
}
return findEntryWithError(name);
}
/**
* Look up the pre-existing RubyEncoding object for an EncodingDB.Entry.
*
* @param str
* @return
*/
public IRubyObject rubyEncodingFromObject(IRubyObject str) {
Entry entry = findEntry(str);
if (entry == null) return runtime.getNil();
return getEncodingList()[entry.getIndex()];
}
/**
* Get a java.nio Charset for the given encoding, or null if impossible
*
* @param encoding the encoding
* @return the charset
*/
public Charset charsetForEncoding(Encoding encoding) {
Charset charset = encoding.getCharset();
if (encoding.toString().equals("ASCII-8BIT")) {
return Charset.forName("ISO-8859-1");
}
try {
return Charset.forName(encoding.toString());
} catch (UnsupportedCharsetException uce) {
throw runtime.newEncodingCompatibilityError("no java.nio.charset.Charset found for encoding `" + encoding.toString() + "'");
}
}
private void checkAsciiEncodingName(ByteList name) {
if (!name.getEncoding().isAsciiCompatible()) {
throw runtime.newArgumentError("invalid name encoding (non ASCII)");
}
}
/**
* Represents one of the four "special" internal encoding names: internal,
* external, locale, or filesystem.
*/
private enum SpecialEncoding {
LOCALE, EXTERNAL, INTERNAL, FILESYSTEM;
public static SpecialEncoding valueOf(ByteList name) {
if (name.caseInsensitiveCmp(LOCALE_BL) == 0) {
return LOCALE;
} else if (name.caseInsensitiveCmp(EXTERNAL_BL) == 0) {
return EXTERNAL;
} else if (name.caseInsensitiveCmp(INTERNAL_BL) == 0) {
return INTERNAL;
} else if (name.caseInsensitiveCmp(FILESYSTEM_BL) == 0) {
return FILESYSTEM;
}
return null;
}
public Encoding toEncoding(Ruby runtime) {
EncodingService service = runtime.getEncodingService();
switch (this) {
case LOCALE: return service.getLocaleEncoding();
case EXTERNAL: return runtime.getDefaultExternalEncoding();
case INTERNAL: return runtime.getDefaultInternalEncoding();
case FILESYSTEM:
// This needs to do something different on Windows. See encoding.c,
// in the enc_set_filesystem_encoding function.
return runtime.getDefaultExternalEncoding();
default:
throw new RuntimeException("invalid SpecialEncoding: " + this);
}
}
}
/**
* Find a non-special encoding, raising argument error if it does not exist.
*
* @param name the name of the encoding to look up
* @return the Encoding object found, or raises ArgumentError
*/
public Encoding findEncodingWithError(ByteList name) {
return findEntryWithError(name).getEncoding();
}
/**
* Find a non-special encoding Entry, raising argument error if it does not exist.
*
* @param name the name of the encoding to look up
* @return the EncodingDB.Entry object found, or raises ArgumentError
*/
private Entry findEntryWithError(ByteList name) {
Entry e = findEncodingOrAliasEntry(name);
if (e == null) throw runtime.newArgumentError("unknown encoding name - " + name);
return e;
}
private Entry findEntryFromEncoding(Encoding e) {
if (e == null) return null;
return findEncodingEntry(new ByteList(e.getName()));
}
}