EncodingInfo.java
3.93 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.jcr.vault.util.xml.serialize;
import com.day.jcr.vault.util.xml.xerces.util.EncodingMap;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
public class EncodingInfo {
private static Method fgGetConverterMethod = null;
private static Method fgCanConvertMethod = null;
private static boolean fgConvertersAvailable = false;
private Object[] fArgsForMethod = null;
String ianaName;
String javaName;
int lastPrintable;
Object fCharToByteConverter = null;
boolean fHaveTriedCToB = false;
Charset nioCharset = null;
CharsetEncoder nioCharEncoder = null;
public EncodingInfo(String ianaName, String javaName, int lastPrintable) {
this.ianaName = ianaName;
this.javaName = EncodingMap.getIANA2JavaMapping(ianaName);
this.lastPrintable = lastPrintable;
try {
this.nioCharset = Charset.forName(this.javaName);
if (this.nioCharset.canEncode()) {
this.nioCharEncoder = this.nioCharset.newEncoder();
}
}
catch (IllegalCharsetNameException ie) {
this.nioCharset = null;
this.nioCharEncoder = null;
}
catch (UnsupportedCharsetException ue) {
this.nioCharset = null;
this.nioCharEncoder = null;
}
}
public String getIANAName() {
return this.ianaName;
}
public Writer getWriter(OutputStream output) throws UnsupportedEncodingException {
if (this.javaName != null) {
return new OutputStreamWriter(output, this.javaName);
}
this.javaName = EncodingMap.getIANA2JavaMapping(this.ianaName);
if (this.javaName == null) {
return new OutputStreamWriter(output, "UTF8");
}
return new OutputStreamWriter(output, this.javaName);
}
public boolean isPrintable(char ch) {
if (ch <= this.lastPrintable) {
return true;
}
if (this.nioCharEncoder != null) {
return this.nioCharEncoder.canEncode(ch);
}
if (this.fCharToByteConverter == null) {
if (this.fHaveTriedCToB || !fgConvertersAvailable) {
return false;
}
if (this.fArgsForMethod == null) {
this.fArgsForMethod = new Object[1];
}
try {
this.fArgsForMethod[0] = this.javaName;
this.fCharToByteConverter = fgGetConverterMethod.invoke(null, this.fArgsForMethod);
}
catch (Exception e) {
this.fHaveTriedCToB = true;
return false;
}
}
try {
this.fArgsForMethod[0] = new Character(ch);
return (Boolean)fgCanConvertMethod.invoke(this.fCharToByteConverter, this.fArgsForMethod);
}
catch (Exception e) {
this.fCharToByteConverter = null;
this.fHaveTriedCToB = false;
return false;
}
}
public static void testJavaEncodingName(String name) throws UnsupportedEncodingException {
byte[] bTest = new byte[]{118, 97, 108, 105, 100};
String s = new String(bTest, name);
}
static {
try {
Class clazz = Class.forName("sun.io.CharToByteConverter");
fgGetConverterMethod = clazz.getMethod("getConverter", String.class);
fgCanConvertMethod = clazz.getMethod("canConvert", Character.TYPE);
fgConvertersAvailable = true;
}
catch (Exception exc) {
fgGetConverterMethod = null;
fgCanConvertMethod = null;
fgConvertersAvailable = false;
}
}
}