+2002-11-17 Jesse Rosenstock <jmr@ugcs.caltech.edu>
+
+ * java/nio/charset/Charset.java
+ (<clinit>): New method.
+ (encode): Synchronize use of cached encoder object.
+ (decode): Synchronize use of cached encoder object.
+
2002-11-18 Michael Koch <konqueror@gmx.de>
* gnu/java/nio/ByteBufferImpl.java,
*/
public abstract class Charset implements Comparable
{
+ private static CharsetEncoder cachedEncoder;
+ private static CharsetDecoder cachedDecoder;
+
+ static
+ {
+ synchronized (Charset.class)
+ {
+ cachedEncoder = null;
+ cachedDecoder = null;
+ }
+ }
+
private final String canonicalName;
private final String[] aliases;
{
try
{
- // TODO: cache encoders between sucessive invocations
- return newEncoder ().onMalformedInput (CodingErrorAction.REPLACE)
- .onUnmappableCharacter (CodingErrorAction.REPLACE)
- .encode (cb);
+ // NB: This implementation serializes different threads calling
+ // Charset.encode(), a potential performance problem. It might
+ // be better to remove the cache, or use ThreadLocal to cache on
+ // a per-thread basis.
+ synchronized (Charset.class)
+ {
+ if (cachedEncoder == null)
+ {
+ cachedEncoder = newEncoder ()
+ .onMalformedInput (CodingErrorAction.REPLACE)
+ .onUnmappableCharacter (CodingErrorAction.REPLACE);
+ }
+
+ return cachedEncoder.encode (cb);
+ }
}
catch (CharacterCodingException e)
{
public CharBuffer decode (ByteBuffer bb)
{
try
- {
- // TODO: cache encoders between sucessive invocations
- return newDecoder ().onMalformedInput (CodingErrorAction.REPLACE)
- .onUnmappableCharacter (CodingErrorAction.REPLACE)
- .decode (bb);
+ {
+ // NB: This implementation serializes different threads calling
+ // Charset.decode(), a potential performance problem. It might
+ // be better to remove the cache, or use ThreadLocal to cache on
+ // a per-thread basis.
+ synchronized (Charset.class)
+ {
+ if (cachedDecoder == null)
+ {
+ cachedDecoder = newDecoder ()
+ .onMalformedInput (CodingErrorAction.REPLACE)
+ .onUnmappableCharacter (CodingErrorAction.REPLACE);
+ }
+
+ return cachedDecoder.decode (bb);
+ }
}
catch (CharacterCodingException e)
{