You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by sj...@apache.org on 2008/08/03 08:25:16 UTC

svn commit: r682107 - /mina/asyncweb/branches/1.0/client/src/main/java/org/apache/asyncweb/client/codec/HttpDecoder.java

Author: sjlee
Date: Sat Aug  2 23:25:16 2008
New Revision: 682107

URL: http://svn.apache.org/viewvc?rev=682107&view=rev
Log:
ASYNCWEB-20

Instead of a shared member variable, introduced it as a thread local.  This retains the original intention for optimization, but makes it thread safe by not sharing instances in multiple threads.

Modified:
    mina/asyncweb/branches/1.0/client/src/main/java/org/apache/asyncweb/client/codec/HttpDecoder.java

Modified: mina/asyncweb/branches/1.0/client/src/main/java/org/apache/asyncweb/client/codec/HttpDecoder.java
URL: http://svn.apache.org/viewvc/mina/asyncweb/branches/1.0/client/src/main/java/org/apache/asyncweb/client/codec/HttpDecoder.java?rev=682107&r1=682106&r2=682107&view=diff
==============================================================================
--- mina/asyncweb/branches/1.0/client/src/main/java/org/apache/asyncweb/client/codec/HttpDecoder.java (original)
+++ mina/asyncweb/branches/1.0/client/src/main/java/org/apache/asyncweb/client/codec/HttpDecoder.java Sat Aug  2 23:25:16 2008
@@ -23,6 +23,7 @@
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
 import java.util.Date;
+import java.util.regex.Pattern;
 
 import org.apache.asyncweb.client.util.DateUtil;
 import org.apache.asyncweb.client.util.NameValuePair;
@@ -91,8 +92,16 @@
     private static final char HT = 9;
 
 
-    /** The decoder. */
-    private CharsetDecoder decoder = Charset.forName(HttpMessage.HTTP_ELEMENT_CHARSET).newDecoder();
+    /** The decoder instances as threadlocals. */
+    private static final ThreadLocal<CharsetDecoder> decoder = new ThreadLocal<CharsetDecoder>() {
+        @Override
+        protected CharsetDecoder initialValue() {
+            return Charset.forName(HttpMessage.HTTP_ELEMENT_CHARSET).newDecoder();
+        }
+    };
+    
+    /** Cached folding pattern. */
+    private static final Pattern folding = Pattern.compile("\\r\\n([ \\t])");
 
     /**
      * Finds a line from a ByteBuffer that ends with a CR/LF and returns the line as a String.
@@ -131,7 +140,7 @@
         if (terminatorPos > 1) {
             ByteBuffer line = in.slice();
             line.limit(terminatorPos - beginPos - 1);
-            result = line.getString(decoder);
+            result = line.getString(decoder.get());
         }
 
         in.position(terminatorPos + 1);
@@ -200,7 +209,7 @@
         if (terminatorPos > 1) {
             ByteBuffer line = in.slice();
             line.limit(terminatorPos - beginPos - 1);
-            result = line.getString(decoder);
+            result = line.getString(decoder.get());
         }
 
         in.position(terminatorPos + 1);
@@ -241,7 +250,7 @@
     public void decodeHeader(String line, HttpResponseMessage msg) throws Exception {
         LOG.debug("Processing Header Line: " + line);
         // first, get rid of the CRLF from linear whitespace
-        line = line.replaceAll("\\r\\n([ \\t])", "$1");
+        line = folding.matcher(line).replaceAll("$1");
         int pos = line.indexOf(":");
         String name = line.substring(0, pos);
         String value = trimHeaderValue(line.substring(pos + 1));