Lines 24-33
Link Here
|
24 |
import java.nio.charset.CharsetDecoder; |
24 |
import java.nio.charset.CharsetDecoder; |
25 |
import java.nio.charset.CoderResult; |
25 |
import java.nio.charset.CoderResult; |
26 |
import java.nio.charset.CodingErrorAction; |
26 |
import java.nio.charset.CodingErrorAction; |
|
|
27 |
import java.nio.charset.IllegalCharsetNameException; |
27 |
import java.nio.charset.StandardCharsets; |
28 |
import java.nio.charset.StandardCharsets; |
|
|
29 |
import java.nio.charset.UnsupportedCharsetException; |
30 |
import java.util.Arrays; |
28 |
import java.util.HashMap; |
31 |
import java.util.HashMap; |
|
|
32 |
import java.util.List; |
29 |
import java.util.Locale; |
33 |
import java.util.Locale; |
30 |
import java.util.Map; |
34 |
import java.util.Map; |
|
|
35 |
import java.util.concurrent.ConcurrentHashMap; |
31 |
|
36 |
|
32 |
import org.apache.tomcat.util.res.StringManager; |
37 |
import org.apache.tomcat.util.res.StringManager; |
33 |
|
38 |
|
Lines 39-58
Link Here
|
39 |
private static final StringManager sm = |
44 |
private static final StringManager sm = |
40 |
StringManager.getManager(Constants.Package); |
45 |
StringManager.getManager(Constants.Package); |
41 |
|
46 |
|
42 |
private static final Map<String, Charset> encodingToCharsetCache = |
47 |
private static final boolean preloadAllCharsets; |
43 |
new HashMap<>(); |
|
|
44 |
|
48 |
|
|
|
49 |
private static final Map<String, Charset> encodingToCharsetCache; |
50 |
|
45 |
// Protected so unit tests can use it |
51 |
// Protected so unit tests can use it |
46 |
protected static final int LEFTOVER_SIZE = 9; |
52 |
protected static final int LEFTOVER_SIZE = 9; |
47 |
|
53 |
|
48 |
static { |
54 |
static { |
49 |
for (Charset charset: Charset.availableCharsets().values()) { |
55 |
String preloadCharsets = System.getProperty("tomcat.util.buf.B2CConverter.preloadCharsets"); |
50 |
encodingToCharsetCache.put( |
56 |
preloadAllCharsets = preloadCharsets == null; |
51 |
charset.name().toLowerCase(Locale.ENGLISH), charset); |
57 |
if (preloadAllCharsets) { |
52 |
for (String alias : charset.aliases()) { |
58 |
encodingToCharsetCache = new HashMap<>(); |
|
|
59 |
for (Charset charset: Charset.availableCharsets().values()) { |
53 |
encodingToCharsetCache.put( |
60 |
encodingToCharsetCache.put( |
54 |
alias.toLowerCase(Locale.ENGLISH), charset); |
61 |
charset.name().toLowerCase(Locale.ENGLISH), charset); |
|
|
62 |
for (String alias : charset.aliases()) { |
63 |
encodingToCharsetCache.put( |
64 |
alias.toLowerCase(Locale.ENGLISH), charset); |
65 |
} |
55 |
} |
66 |
} |
|
|
67 |
} else { |
68 |
encodingToCharsetCache = new ConcurrentHashMap<>(); |
69 |
String[] charsetNames = preloadCharsets.split(","); |
70 |
for (String charsetName : charsetNames) { |
71 |
Charset charset = Charset.forName(charsetName); |
72 |
encodingToCharsetCache.put( |
73 |
charset.name().toLowerCase(Locale.ENGLISH), charset); |
74 |
for (String alias : charset.aliases()) { |
75 |
encodingToCharsetCache.put( |
76 |
alias.toLowerCase(Locale.ENGLISH), charset); |
77 |
} |
78 |
} |
56 |
} |
79 |
} |
57 |
} |
80 |
} |
58 |
|
81 |
|
Lines 74-82
Link Here
|
74 |
Charset charset = encodingToCharsetCache.get(lowerCaseEnc); |
97 |
Charset charset = encodingToCharsetCache.get(lowerCaseEnc); |
75 |
|
98 |
|
76 |
if (charset == null) { |
99 |
if (charset == null) { |
77 |
// Pre-population of the cache means this must be invalid |
100 |
if (preloadAllCharsets) { |
78 |
throw new UnsupportedEncodingException( |
101 |
// Pre-population of the cache means this must be invalid |
79 |
sm.getString("b2cConverter.unknownEncoding", lowerCaseEnc)); |
102 |
throw new UnsupportedEncodingException( |
|
|
103 |
sm.getString("b2cConverter.unknownEncoding", lowerCaseEnc)); |
104 |
} |
105 |
|
106 |
try { |
107 |
charset = Charset.forName(lowerCaseEnc); |
108 |
} catch (UnsupportedCharsetException | IllegalCharsetNameException e) { |
109 |
throw new UnsupportedEncodingException( |
110 |
sm.getString("b2cConverter.unknownEncoding", lowerCaseEnc)); |
111 |
} |
112 |
encodingToCharsetCache.put(lowerCaseEnc, charset); |
80 |
} |
113 |
} |
81 |
return charset; |
114 |
return charset; |
82 |
} |
115 |
} |
Lines 191-194
Link Here
|
191 |
} |
224 |
} |
192 |
} |
225 |
} |
193 |
} |
226 |
} |
|
|
227 |
|
194 |
} |
228 |
} |