Author: markt
Date: Mon Oct 13 19:27:37 2014
New Revision: 1631520

URL: http://svn.apache.org/r1631520
Log:
Cache the Encoder instances used to convert Strings to byte arrays in the 
Connectors (e.g. when writing HTTP headers) to improve throughput.

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties
    tomcat/trunk/java/org/apache/tomcat/util/buf/MessageBytes.java
    tomcat/trunk/webapps/docs/changelog.xml

Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties?rev=1631520&r1=1631519&r2=1631520&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties Mon 
Oct 13 19:27:37 2014
@@ -18,6 +18,9 @@ c2bConverter.recycleFailed=Failed to rec
 
 hexUtils.fromHex.oddDigits=The input must consist of an even number of hex 
digits
 hexUtils.fromHex.nonHex=The input must consist only of hex digits
+
+messageBytes.toBytesFailed=Failed to convert the String [{0}] to bytes using 
Charset [{0}]
+
 uDecoder.urlDecode.missingDigit=The % character must be followed by two 
hexademical digits
 uDecoder.convertHexDigit.notHex=[{0}] is not a hexadecimal digit
 uDecoder.urlDecode.uee=Unable to URL decode the specified input since the 
encoding [{0}] is not supported.

Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/MessageBytes.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/MessageBytes.java?rev=1631520&r1=1631519&r2=1631520&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/MessageBytes.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/MessageBytes.java Mon Oct 13 
19:27:37 2014
@@ -18,8 +18,16 @@ package org.apache.tomcat.util.buf;
 
 import java.io.IOException;
 import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
 import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+import java.util.HashMap;
 import java.util.Locale;
+import java.util.Map;
+
+import org.apache.tomcat.util.res.StringManager;
 
 /**
  * This class is used to represent a subarray of bytes in an HTTP message.
@@ -36,6 +44,9 @@ import java.util.Locale;
 public final class MessageBytes implements Cloneable, Serializable {
     private static final long serialVersionUID = 1L;
 
+    private static final StringManager sm = StringManager.getManager(
+            Constants.Package);
+
     // primary type ( whatever is set as original value )
     private int type = T_NULL;
 
@@ -64,6 +75,8 @@ public final class MessageBytes implemen
     // strValue!=null is the same
     private boolean hasStrValue=false;
 
+    private Map<Charset,CharsetEncoder> encoders = new HashMap<>();
+
     /**
      * Creates a new, uninitialized MessageBytes object.
      * Use static newInstance() in order to allow
@@ -215,17 +228,29 @@ public final class MessageBytes implemen
         byteC.setCharset(charset);
     }
 
-    /** Do a char->byte conversion.
+    /** Do a char-&gt;byte conversion.
      */
     public void toBytes() {
-        if( ! byteC.isNull() ) {
+        if (!byteC.isNull()) {
             type=T_BYTES;
             return;
         }
         toString();
         type=T_BYTES;
-        byte bb[] = strValue.getBytes(byteC.getCharset());
-        byteC.setBytes(bb, 0, bb.length);
+        Charset charset = byteC.getCharset();
+        CharsetEncoder encoder = encoders.get(charset);
+        if (encoder == null) {
+            encoder = charset.newEncoder();
+            encoders.put(charset, encoder);
+        }
+        ByteBuffer result;
+        try {
+             result = encoder.encode(CharBuffer.wrap(strValue));
+        } catch (CharacterCodingException e) {
+            throw new IllegalArgumentException(sm.getString(
+                    "messageBytes.toBytesFailed", strValue, charset), e);
+        }
+        byteC.setBytes(result.array(), result.arrayOffset(), result.limit());
     }
 
     /** Convert to char[] and fill the CharChunk.

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1631520&r1=1631519&r2=1631520&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Mon Oct 13 19:27:37 2014
@@ -143,6 +143,15 @@
       </fix>
     </changelog>
   </subsection>
+  <subsection name="Coyote">
+    <changelog>
+      <scode>
+        Cache the <code>Encoder</code> instances used to convert Strings to 
byte
+        arrays in the Connectors (e.g. when writing HTTP headers) to improve
+        throughput. (markt)
+      </scode>
+    </changelog>
+  </subsection>
   <subsection name="WebSocket">
     <changelog>
       <fix>



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to