ASF Bugzilla – Attachment 27214 Details for
Bug 51400
Use of "new String(byte[] b, String enc)" hits Sun JVM bottleneck
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch for tc6.0.x
bug51400-tc6-v4.patch (text/plain), 5.73 KB, created by
Mark Thomas
on 2011-06-27 16:10:54 UTC
(
hide
)
Description:
Patch for tc6.0.x
Filename:
MIME Type:
Creator:
Mark Thomas
Created:
2011-06-27 16:10:54 UTC
Size:
5.73 KB
patch
obsolete
>Index: java/org/apache/catalina/connector/Request.java >=================================================================== >--- java/org/apache/catalina/connector/Request.java (revision 1139393) >+++ 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 1139393) >+++ java/org/apache/tomcat/util/buf/B2CConverter.java (working copy) >@@ -21,7 +21,10 @@ > import java.io.IOException; > import java.io.InputStream; > import java.io.InputStreamReader; >-import java.io.UnsupportedEncodingException; >+import java.nio.charset.Charset; >+import java.nio.charset.UnsupportedCharsetException; >+import java.util.Locale; >+import java.util.concurrent.ConcurrentHashMap; > > /** Efficient conversion of bytes to character . > * >@@ -39,7 +42,35 @@ > > private static org.apache.juli.logging.Log log= > org.apache.juli.logging.LogFactory.getLog( B2CConverter.class ); >+ >+ private static final ConcurrentHashMap<String, Charset> encodingToCharsetCache = >+ new ConcurrentHashMap<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) { >+ >+ // 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 UnsupportedCharsetException(enc); >+ } >+ return charset; >+ } >+ > private IntermediateInputStream iis; > private ReadConvertor conv; > private String encoding; >@@ -114,7 +145,7 @@ > { > // destroy the reader/iis > iis=new IntermediateInputStream(); >- conv=new ReadConvertor( iis, encoding ); >+ conv=new ReadConvertor( iis, getCharset(encoding) ); > } > > private final int debug=0; >@@ -192,10 +223,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 +254,7 @@ > } catch(IOException ioe){ > } > } >+ > } > > >Index: java/org/apache/tomcat/util/buf/ByteChunk.java >=================================================================== >--- java/org/apache/tomcat/util/buf/ByteChunk.java (revision 1139393) >+++ 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);
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 51400
:
27186
|
27189
|
27202
|
27211
|
27212
|
27213
|
27214
|
27219