CharsetUtil.java
5.34 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.agl.charset.CharsetICU
*/
package com.adobe.fontengine;
import com.adobe.agl.charset.CharsetICU;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.*;
import java.util.concurrent.ConcurrentHashMap;
public class CharsetUtil {
private static ConcurrentHashMap<String, Charset> charSetMap = new ConcurrentHashMap();
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static CoderResult encodeLoop(CharBuffer input, ByteBuffer output, CharsetEncoder encoder, boolean throwExceptions) throws MalformedInputException, UnmappableCharacterException {
CoderResult result;
result = null;
try {
result = encoder.encode(input, output, true);
if ((result.isMalformed() || !result.isError()) && (result = encoder.flush(output)).isError() && !result.isUnderflow()) {
CharsetUtil.conditionallyThrowCodingError(result);
}
CharsetUtil.conditionallyThrowCodingError(result);
}
catch (MalformedInputException e) {
if (throwExceptions) {
throw e;
}
}
catch (UnmappableCharacterException e) {
if (throwExceptions) {
throw e;
}
}
return result;
}
public static CoderResult encodeLoopNoExceptions(CharBuffer input, ByteBuffer output, CharsetEncoder encoder) {
CoderResult result = null;
try {
result = CharsetUtil.encodeLoop(input, output, encoder, false);
}
catch (MalformedInputException e) {
}
catch (UnmappableCharacterException e) {
// empty catch block
}
return result;
}
public static CoderResult encodeLoop(CharBuffer input, ByteBuffer output, Charset charset, boolean throwExceptions) throws MalformedInputException, UnmappableCharacterException {
CharsetEncoder encoder = charset.newEncoder();
return CharsetUtil.encodeLoop(input, output, encoder, throwExceptions);
}
public static CoderResult encodeLoopNoExceptions(CharBuffer input, ByteBuffer output, Charset charset) throws MalformedInputException, UnmappableCharacterException {
CharsetEncoder encoder = charset.newEncoder();
return CharsetUtil.encodeLoopNoExceptions(input, output, encoder);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static CoderResult decodeLoop(ByteBuffer input, CharBuffer output, CharsetDecoder decoder, boolean throwExceptions) throws MalformedInputException, UnmappableCharacterException {
CoderResult result;
result = null;
try {
result = decoder.decode(input, output, true);
CharsetUtil.conditionallyThrowCodingError(result);
result = decoder.flush(output);
CharsetUtil.conditionallyThrowCodingError(result);
decoder.reset();
}
catch (MalformedInputException e) {
if (throwExceptions) {
throw e;
}
}
catch (UnmappableCharacterException e) {
if (throwExceptions) {
throw e;
}
}
return result;
}
public static CoderResult decodeLoopNoExceptions(ByteBuffer input, CharBuffer output, CharsetDecoder decoder) {
CoderResult result = null;
try {
result = CharsetUtil.decodeLoop(input, output, decoder, false);
}
catch (MalformedInputException e) {
}
catch (UnmappableCharacterException e) {
// empty catch block
}
return result;
}
public static CoderResult decodeLoop(ByteBuffer input, CharBuffer output, Charset charset, boolean throwExceptions) throws MalformedInputException, UnmappableCharacterException {
CharsetDecoder decoder = charset.newDecoder();
return CharsetUtil.decodeLoop(input, output, decoder, throwExceptions);
}
public static CoderResult decodeLoopNoExceptions(ByteBuffer input, CharBuffer output, Charset charset) throws MalformedInputException, UnmappableCharacterException {
CharsetDecoder decoder = charset.newDecoder();
return CharsetUtil.decodeLoopNoExceptions(input, output, decoder);
}
public static void conditionallyThrowCodingError(CoderResult result) throws MalformedInputException, UnmappableCharacterException {
if (result.isError()) {
if (result.isMalformed()) {
throw new MalformedInputException(result.length());
}
if (result.isOverflow()) {
// empty if block
}
if (result.isUnmappable()) {
throw new UnmappableCharacterException(result.length());
}
if (result.isUnderflow()) {
// empty if block
}
}
}
public static Charset forNameICU(String charsetName) {
Charset prev;
if (charsetName == null) {
return null;
}
Charset charSet = charSetMap.get(charsetName);
if (charSet == null && (prev = charSetMap.putIfAbsent(charsetName, charSet = CharsetICU.forNameICU((String)charsetName))) != null) {
charSet = prev;
}
return charSet;
}
}