Index: java/org/apache/catalina/connector/Request.java =================================================================== --- java/org/apache/catalina/connector/Request.java (revision 1137623) +++ java/org/apache/catalina/connector/Request.java (working copy) @@ -1516,12 +1516,22 @@ 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 + try { + B2CConverter.getCharset(enc); + } catch (java.nio.charset.UnsupportedCharsetException e) { + UnsupportedEncodingException uee = + new UnsupportedEncodingException(); + uee.initCause(e); + throw uee; + } catch (java.nio.charset.IllegalCharsetNameException e) { + UnsupportedEncodingException uee = + new UnsupportedEncodingException(); + uee.initCause(e); + throw uee; + } + // Save the validated encoding coyoteRequest.setCharacterEncoding(enc); Index: java/org/apache/tomcat/util/buf/B2CConverter.java =================================================================== --- java/org/apache/tomcat/util/buf/B2CConverter.java (revision 1137623) +++ java/org/apache/tomcat/util/buf/B2CConverter.java (working copy) @@ -21,7 +21,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.util.concurrent.ConcurrentHashMap; /** Efficient conversion of bytes to character . * @@ -39,6 +40,9 @@ private static org.apache.juli.logging.Log log= org.apache.juli.logging.LogFactory.getLog( B2CConverter.class ); + + private static final ConcurrentHashMap encodingToCharsetCache = + new ConcurrentHashMap(); private IntermediateInputStream iis; private ReadConvertor conv; @@ -114,7 +118,7 @@ { // destroy the reader/iis iis=new IntermediateInputStream(); - conv=new ReadConvertor( iis, encoding ); + conv=new ReadConvertor( iis, getCharset(encoding) ); } private final int debug=0; @@ -123,6 +127,15 @@ log.debug("B2CConverter: " + s ); } + public static Charset getCharset(String enc) { + Charset charset = encodingToCharsetCache.get(enc); + if (charset == null) { + charset = Charset.forName(enc); + encodingToCharsetCache.put(enc, charset); + } + return charset; + } + // -------------------- Not used - the speed improvemnt is quite small /* @@ -192,10 +205,9 @@ /** Create a converter. */ - public ReadConvertor( IntermediateInputStream in, String enc ) - throws UnsupportedEncodingException + public ReadConvertor( IntermediateInputStream in, Charset charset ) { - super( in, enc ); + super( in, charset ); } /** Overriden - will do nothing but reset internal state. @@ -224,6 +236,7 @@ } catch(IOException ioe){ } } + } Index: java/org/apache/tomcat/util/buf/ByteChunk.java =================================================================== --- java/org/apache/tomcat/util/buf/ByteChunk.java (revision 1137623) +++ java/org/apache/tomcat/util/buf/ByteChunk.java (working copy) @@ -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; @@ -493,8 +500,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(). @@ -507,7 +520,7 @@ // Method is commented out, in: return B2CConverter.decodeString( enc ); */ - } catch (java.io.UnsupportedEncodingException e) { + } catch (java.nio.charset.UnsupportedCharsetException e) { // Use the platform encoding in that case; the usage of a bad // encoding will have been logged elsewhere already strValue = new String(buff, start, end-start);