57 |
{ |
{ |
58 |
private static CharsetEncoder cachedEncoder; |
private static CharsetEncoder cachedEncoder; |
59 |
private static CharsetDecoder cachedDecoder; |
private static CharsetDecoder cachedDecoder; |
60 |
|
|
61 |
|
static |
62 |
|
{ |
63 |
|
synchronized (Charset.class) |
64 |
|
{ |
65 |
|
cachedEncoder = null; |
66 |
|
cachedDecoder = null; |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
private final String canonicalName; |
private final String canonicalName; |
71 |
private final String[] aliases; |
private final String[] aliases; |
72 |
|
|
207 |
{ |
{ |
208 |
try |
try |
209 |
{ |
{ |
210 |
if (cachedEncoder == null) |
// NB: This implementation serializes different threads calling |
211 |
|
// Charset.encode(), a potential performance problem. It might |
212 |
|
// be better to remove the cache, or use ThreadLocal to cache on |
213 |
|
// a per-thread basis. |
214 |
|
synchronized (Charset.class) |
215 |
{ |
{ |
216 |
cachedEncoder = newEncoder () |
if (cachedEncoder == null) |
217 |
.onMalformedInput (CodingErrorAction.REPLACE) |
{ |
218 |
.onUnmappableCharacter (CodingErrorAction.REPLACE); |
cachedEncoder = newEncoder () |
219 |
} |
.onMalformedInput (CodingErrorAction.REPLACE) |
220 |
|
.onUnmappableCharacter (CodingErrorAction.REPLACE); |
221 |
return cachedEncoder.encode (cb); |
} |
222 |
|
|
223 |
|
return cachedEncoder.encode (cb); |
224 |
|
} |
225 |
} |
} |
226 |
catch (CharacterCodingException e) |
catch (CharacterCodingException e) |
227 |
{ |
{ |
237 |
public CharBuffer decode (ByteBuffer bb) |
public CharBuffer decode (ByteBuffer bb) |
238 |
{ |
{ |
239 |
try |
try |
240 |
{ |
{ |
241 |
if (cachedDecoder == null) |
// NB: This implementation serializes different threads calling |
242 |
|
// Charset.decode(), a potential performance problem. It might |
243 |
|
// be better to remove the cache, or use ThreadLocal to cache on |
244 |
|
// a per-thread basis. |
245 |
|
synchronized (Charset.class) |
246 |
{ |
{ |
247 |
cachedDecoder = newDecoder () |
if (cachedDecoder == null) |
248 |
.onMalformedInput (CodingErrorAction.REPLACE) |
{ |
249 |
.onUnmappableCharacter (CodingErrorAction.REPLACE); |
cachedDecoder = newDecoder () |
250 |
} |
.onMalformedInput (CodingErrorAction.REPLACE) |
251 |
|
.onUnmappableCharacter (CodingErrorAction.REPLACE); |
252 |
return cachedDecoder.decode (bb); |
} |
253 |
|
|
254 |
|
return cachedDecoder.decode (bb); |
255 |
|
} |
256 |
} |
} |
257 |
catch (CharacterCodingException e) |
catch (CharacterCodingException e) |
258 |
{ |
{ |