2002-11-17 Jesse Rosenstock <jmr@ugcs.caltech.edu>
authorJesse Rosenstock <jmr@ugcs.caltech.edu>
Mon, 18 Nov 2002 14:15:16 +0000 (14:15 +0000)
committerMichael Koch <mkoch@gcc.gnu.org>
Mon, 18 Nov 2002 14:15:16 +0000 (14:15 +0000)
* 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

libjava/ChangeLog
libjava/java/nio/charset/Charset.java

index 85215d574658dda7271c4afe6668161f0ebc061e..7942b98059db1ce3196a4bafbb30e566b38d5357 100644 (file)
@@ -1,3 +1,10 @@
+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,
index 5d96daf7f762db842e7396bca2d175c15494dea0..cc60c99b9b6a5ec6a56d6daa2b3190c2e90baf31 100644 (file)
@@ -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)
       {