---
 apache-tomcat-6.0.18-src/java/org/apache/catalina/connector/Request.java   |    9 --
 apache-tomcat-6.0.18-src/java/org/apache/tomcat/util/buf/B2CConverter.java |   40 +++++++++-
 apache-tomcat-6.0.18-src/java/org/apache/tomcat/util/buf/ByteChunk.java    |   19 ++++
 apache-tomcat-6.0.18-src/webapps/docs/changelog.xml                        |    5 +
 4 files changed, 60 insertions(+), 13 deletions(-)

Index: tomcat6-6.0.18/apache-tomcat-6.0.18-src/java/org/apache/tomcat/util/buf/ByteChunk.java
===================================================================
--- tomcat6-6.0.18.orig/apache-tomcat-6.0.18-src/java/org/apache/tomcat/util/buf/ByteChunk.java	2012-01-06 15:35:38.237747398 +0100
+++ tomcat6-6.0.18/apache-tomcat-6.0.18-src/java/org/apache/tomcat/util/buf/ByteChunk.java	2012-01-06 15:35:44.552968481 +0100
@@ -19,6 +19,8 @@
 
 import java.io.IOException;
 import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
 
 /*
  * In a server it is very important to be able to operate on
@@ -95,7 +97,12 @@
 	8859_1, and this object is used mostly for servlets. 
     */
     public static final String DEFAULT_CHARACTER_ENCODING="ISO-8859-1";
-        
+
+    /** Default Charset to use for interpreting byte[] as as String
+    */
+    public static final Charset DEFAULT_CHARSET =
+        Charset.forName(DEFAULT_CHARACTER_ENCODING);
+    
     // byte[]
     private byte[] buff;
 
@@ -495,8 +502,14 @@
     public String toStringInternal() {
         String strValue=null;
         try {
-            if( enc==null ) enc=DEFAULT_CHARACTER_ENCODING;
-            strValue = new String( buff, start, end-start, enc );
+            Charset charset;
+            if (enc == null) {
+                charset = DEFAULT_CHARSET;
+            } else {
+                charset = B2CConverter.getCharset(enc);
+            }
+            strValue = charset.decode(
+                    ByteBuffer.wrap(buff, start, end-start)).toString();
             /*
              Does not improve the speed too much on most systems,
              it's safer to use the "clasical" new String().
Index: tomcat6-6.0.18/apache-tomcat-6.0.18-src/java/org/apache/tomcat/util/buf/B2CConverter.java
===================================================================
--- tomcat6-6.0.18.orig/apache-tomcat-6.0.18-src/java/org/apache/tomcat/util/buf/B2CConverter.java	2012-01-06 15:35:38.237747398 +0100
+++ tomcat6-6.0.18/apache-tomcat-6.0.18-src/java/org/apache/tomcat/util/buf/B2CConverter.java	2012-01-06 15:35:44.553968517 +0100
@@ -22,6 +22,10 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
 
 /** Efficient conversion of bytes  to character .
  *  
@@ -39,7 +43,36 @@
     
     private static org.apache.juli.logging.Log log=
         org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
+
+    private static final Map<String, Charset> encodingToCharsetCache =
+        new HashMap<String, Charset>();
     
+    static {
+        for (Charset charset: Charset.availableCharsets().values()) {
+            encodingToCharsetCache.put(
+                    charset.name().toLowerCase(Locale.US), charset);
+            for (String alias : charset.aliases()) {
+                encodingToCharsetCache.put(
+                        alias.toLowerCase(Locale.US), charset);
+            }
+        }
+    }
+
+    public static Charset getCharset(String enc)
+            throws UnsupportedEncodingException {
+
+        // Encoding names should all be ASCII
+        String lowerCaseEnc = enc.toLowerCase(Locale.US);
+        
+        Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
+
+        if (charset == null) {
+            // Pre-population of the cache means this must be invalid
+            throw new UnsupportedEncodingException(enc);
+        }
+        return charset;
+    }
+
     private IntermediateInputStream iis;
     private ReadConvertor conv;
     private String encoding;
@@ -114,7 +147,7 @@
     {
 	// destroy the reader/iis
 	iis=new IntermediateInputStream();
-	conv=new ReadConvertor( iis, encoding );
+        conv=new ReadConvertor( iis, getCharset(encoding) );
     }
 
     private final int debug=0;
@@ -194,10 +227,9 @@
     
     /** Create a converter.
      */
-    public ReadConvertor( IntermediateInputStream in, String enc )
-	throws UnsupportedEncodingException
+    public ReadConvertor( IntermediateInputStream in, Charset charset )
     {
-	super( in, enc );
+	super( in, charset );
 	iis=in;
     }
     
Index: tomcat6-6.0.18/apache-tomcat-6.0.18-src/java/org/apache/catalina/connector/Request.java
===================================================================
--- tomcat6-6.0.18.orig/apache-tomcat-6.0.18-src/java/org/apache/catalina/connector/Request.java	2012-01-06 15:35:38.238747433 +0100
+++ tomcat6-6.0.18/apache-tomcat-6.0.18-src/java/org/apache/catalina/connector/Request.java	2012-01-06 15:35:44.555968587 +0100
@@ -1491,15 +1491,12 @@
 
         if (usingReader)
             return;
-        
-        // Ensure that the specified encoding is valid
-        byte buffer[] = new byte[1];
-        buffer[0] = (byte) 'a';
-        String dummy = new String(buffer, enc);
+
+        // Confirm that the encoding name is valid
+        B2CConverter.getCharset(enc);
 
         // Save the validated encoding
         coyoteRequest.setCharacterEncoding(enc);
-
     }
 
 
Index: tomcat6-6.0.18/apache-tomcat-6.0.18-src/webapps/docs/changelog.xml
===================================================================
--- tomcat6-6.0.18.orig/apache-tomcat-6.0.18-src/webapps/docs/changelog.xml	2012-01-06 15:35:42.783906550 +0100
+++ tomcat6-6.0.18/apache-tomcat-6.0.18-src/webapps/docs/changelog.xml	2012-01-06 15:35:44.559968727 +0100
@@ -50,6 +50,11 @@
       <fix>
         Add additional checks for URI normalization. (remm)
       </fix>
+      <fix>
+        <bug>51400</bug>: Avoid jvm bottleneck on String/byte[] conversion
+        triggered by a JVM bug. Based on patches by Dave Engberg and Konstantin
+        Preißer. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">
