You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by be...@apache.org on 2013/06/17 13:53:21 UTC

[2/2] git commit: VYSPER-342: simplify charset codec initialization, prefer singleton approach over thread local

VYSPER-342: simplify charset codec initialization, prefer singleton approach over thread local


Project: http://git-wip-us.apache.org/repos/asf/mina-vysper/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-vysper/commit/013be8be
Tree: http://git-wip-us.apache.org/repos/asf/mina-vysper/tree/013be8be
Diff: http://git-wip-us.apache.org/repos/asf/mina-vysper/diff/013be8be

Branch: refs/heads/master
Commit: 013be8be8bc384678afc524c053c8584c24827a4
Parents: f477875
Author: Bernd Fondermann <be...@brainlounge.de>
Authored: Mon Jun 17 13:43:19 2013 +0200
Committer: Bernd Fondermann <be...@brainlounge.de>
Committed: Mon Jun 17 13:43:19 2013 +0200

----------------------------------------------------------------------
 .../org/apache/vysper/charset/CharsetUtil.java  | 34 +++++++-------------
 1 file changed, 12 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-vysper/blob/013be8be/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java
----------------------------------------------------------------------
diff --git a/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java b/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java
index 48e1da8..3ef49b3 100644
--- a/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java
+++ b/nbxml/src/main/java/org/apache/vysper/charset/CharsetUtil.java
@@ -33,34 +33,24 @@ public class CharsetUtil {
 
     private static final Charset UTF8 = Charset.forName("UTF-8");
 
-    private static ThreadLocal<CharsetDecoder> decoderCache = new ThreadLocal<CharsetDecoder>();
-    private static ThreadLocal<CharsetEncoder> encoderCache = new ThreadLocal<CharsetEncoder>();
+    private static CharsetDecoder decoderCache = null;
+    private static CharsetEncoder encoderCache = null;
 
-    private static Object getReference(ThreadLocal threadLocal) {
-        SoftReference reference = (SoftReference) threadLocal.get();
-        if (reference != null) return reference.get();
-        return null;
-    }
+    public static CharsetDecoder getDecoder() {
+        if (decoderCache != null) return decoderCache;
 
-    private static void setReference(ThreadLocal threadLocal, Object object) {
-        threadLocal.set(new SoftReference(object));
+        synchronized (CharsetUtil.class) {
+            decoderCache = UTF8.newDecoder();
+        }
+        return decoderCache;
     }
 
     public static CharsetEncoder getEncoder() {
-        CharsetEncoder encoder = (CharsetEncoder) getReference(encoderCache);
-        if (encoder == null) {
-            encoder = UTF8.newEncoder();
-            setReference(encoderCache, encoder);
-        }
-        return encoder;
-    }
+        if (encoderCache != null) return encoderCache;
 
-    public static CharsetDecoder getDecoder() {
-        CharsetDecoder decoder = (CharsetDecoder) getReference(decoderCache);
-        if (decoder == null) {
-            decoder = UTF8.newDecoder();
-            setReference(decoderCache, decoder);
+        synchronized (CharsetUtil.class) {
+            encoderCache = UTF8.newEncoder();
         }
-        return decoder;
+        return encoderCache;
     }
 }
\ No newline at end of file