diff options
author | Jesse Rosenstock <jmr@ugcs.caltech.edu> | 2002-11-18 14:15:16 +0000 |
---|---|---|
committer | Michael Koch <mkoch@gcc.gnu.org> | 2002-11-18 14:15:16 +0000 |
commit | 3386451d202d3de5d223dec8de08a90f8fe607e0 (patch) | |
tree | 93ceced0a04b7328d3bda45902eab4d028e20753 /libjava/java/nio/charset/Charset.java | |
parent | ea4210ef82eac78a2a512b48c888b88c9912c6eb (diff) | |
download | gcc-3386451d202d3de5d223dec8de08a90f8fe607e0.zip gcc-3386451d202d3de5d223dec8de08a90f8fe607e0.tar.gz gcc-3386451d202d3de5d223dec8de08a90f8fe607e0.tar.bz2 |
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.
From-SVN: r59218
Diffstat (limited to 'libjava/java/nio/charset/Charset.java')
-rw-r--r-- | libjava/java/nio/charset/Charset.java | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/libjava/java/nio/charset/Charset.java b/libjava/java/nio/charset/Charset.java index 5d96daf..cc60c99b 100644 --- a/libjava/java/nio/charset/Charset.java +++ b/libjava/java/nio/charset/Charset.java @@ -55,6 +55,18 @@ import gnu.java.nio.charset.Provider; */ 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; @@ -195,10 +207,21 @@ public abstract class Charset implements Comparable { 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) { @@ -214,11 +237,22 @@ public abstract class Charset implements Comparable 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) { |