View | Details | Raw Unified | Return to bug 51400
Collapse All | Expand All

(-)java/org/apache/tomcat/util/buf/ByteChunk.java (-4 / +15 lines)
Lines 19-24 Link Here
19
19
20
import java.io.IOException;
20
import java.io.IOException;
21
import java.io.Serializable;
21
import java.io.Serializable;
22
import java.nio.charset.Charset;
22
23
23
/*
24
/*
24
 * In a server it is very important to be able to operate on
25
 * In a server it is very important to be able to operate on
Lines 95-101 Link Here
95
        8859_1, and this object is used mostly for servlets. 
96
        8859_1, and this object is used mostly for servlets. 
96
    */
97
    */
97
    public static final String DEFAULT_CHARACTER_ENCODING="ISO-8859-1";
98
    public static final String DEFAULT_CHARACTER_ENCODING="ISO-8859-1";
98
        
99
100
    /** Default Charset to use for interpreting byte[] as as String
101
    */
102
    public static final Charset DEFAULT_CHARSET =
103
	Charset.forName(DEFAULT_CHARACTER_ENCODING);
104
    
99
    // byte[]
105
    // byte[]
100
    private byte[] buff;
106
    private byte[] buff;
101
107
Lines 493-500 Link Here
493
    public String toStringInternal() {
499
    public String toStringInternal() {
494
        String strValue=null;
500
        String strValue=null;
495
        try {
501
        try {
496
            if( enc==null ) enc=DEFAULT_CHARACTER_ENCODING;
502
	    Charset charset;
497
            strValue = new String( buff, start, end-start, enc );
503
            if (enc == null) {
504
		charset = DEFAULT_CHARSET;
505
	    } else {
506
		charset = B2CConverter.getCharset(enc);
507
	    }
508
            strValue = new String( buff, start, end-start, charset );
498
            /*
509
            /*
499
             Does not improve the speed too much on most systems,
510
             Does not improve the speed too much on most systems,
500
             it's safer to use the "clasical" new String().
511
             it's safer to use the "clasical" new String().
Lines 507-513 Link Here
507
             // Method is commented out, in:
518
             // Method is commented out, in:
508
              return B2CConverter.decodeString( enc );
519
              return B2CConverter.decodeString( enc );
509
              */
520
              */
510
        } catch (java.io.UnsupportedEncodingException e) {
521
        } catch (java.nio.charset.UnsupportedCharsetException e) {
511
            // Use the platform encoding in that case; the usage of a bad
522
            // Use the platform encoding in that case; the usage of a bad
512
            // encoding will have been logged elsewhere already
523
            // encoding will have been logged elsewhere already
513
            strValue = new String(buff, start, end-start);
524
            strValue = new String(buff, start, end-start);
(-)java/org/apache/tomcat/util/buf/B2CConverter.java (-4 / +18 lines)
Lines 22-27 Link Here
22
import java.io.InputStream;
22
import java.io.InputStream;
23
import java.io.InputStreamReader;
23
import java.io.InputStreamReader;
24
import java.io.UnsupportedEncodingException;
24
import java.io.UnsupportedEncodingException;
25
import java.nio.charset.Charset;
26
import java.util.concurrent.ConcurrentHashMap;
25
27
26
/** Efficient conversion of bytes  to character .
28
/** Efficient conversion of bytes  to character .
27
 *  
29
 *  
Lines 39-44 Link Here
39
    
41
    
40
    private static org.apache.juli.logging.Log log=
42
    private static org.apache.juli.logging.Log log=
41
        org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
43
        org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
44
45
    private static final ConcurrentHashMap<String, Charset> encodingToCharsetCache =
46
	new ConcurrentHashMap<String, Charset>();
42
    
47
    
43
    private IntermediateInputStream iis;
48
    private IntermediateInputStream iis;
44
    private ReadConvertor conv;
49
    private ReadConvertor conv;
Lines 114-120 Link Here
114
    {
119
    {
115
        // destroy the reader/iis
120
        // destroy the reader/iis
116
        iis=new IntermediateInputStream();
121
        iis=new IntermediateInputStream();
117
        conv=new ReadConvertor( iis, encoding );
122
        conv=new ReadConvertor( iis, getCharset(encoding) );
118
    }
123
    }
119
124
120
    private final int debug=0;
125
    private final int debug=0;
Lines 123-128 Link Here
123
            log.debug("B2CConverter: " + s );
128
            log.debug("B2CConverter: " + s );
124
    }
129
    }
125
130
131
    public static Charset getCharset(String enc) {
132
	Charset charset = encodingToCharsetCache.get(enc);
133
	if (charset == null) {
134
	    charset = Charset.forName(enc);
135
	    encodingToCharsetCache.put(enc, charset);
136
	}
137
	return charset;
138
    }
139
126
    // -------------------- Not used - the speed improvemnt is quite small
140
    // -------------------- Not used - the speed improvemnt is quite small
127
141
128
    /*
142
    /*
Lines 192-201 Link Here
192
    
206
    
193
    /** Create a converter.
207
    /** Create a converter.
194
     */
208
     */
195
    public ReadConvertor( IntermediateInputStream in, String enc )
209
    public ReadConvertor( IntermediateInputStream in, Charset charset )
196
        throws UnsupportedEncodingException
197
    {
210
    {
198
        super( in, enc );
211
        super( in, charset );
199
    }
212
    }
200
    
213
    
201
    /** Overriden - will do nothing but reset internal state.
214
    /** Overriden - will do nothing but reset internal state.
Lines 224-229 Link Here
224
        } catch(IOException ioe){
237
        } catch(IOException ioe){
225
        }
238
        }
226
    }
239
    }
240
227
}
241
}
228
242
229
243
(-)java/org/apache/catalina/connector/Request.java (-5 / +9 lines)
Lines 1516-1527 Link Here
1516
1516
1517
        if (usingReader)
1517
        if (usingReader)
1518
            return;
1518
            return;
1519
        
1520
        // Ensure that the specified encoding is valid
1521
        byte buffer[] = new byte[1];
1522
        buffer[0] = (byte) 'a';
1523
        String dummy = new String(buffer, enc);
1524
1519
1520
	// Confirm that the encoding name is valid
1521
	try {
1522
	    B2CConverter.getCharset(enc);
1523
	} catch (java.nio.charset.UnsupportedCharsetException e) {
1524
	    throw new UnsupportedEncodingException(e.getMessage());
1525
	} catch (java.nio.charset.IllegalCharsetNameException e) {
1526
	    throw new UnsupportedEncodingException(e.getMessage());
1527
	}
1528
1525
        // Save the validated encoding
1529
        // Save the validated encoding
1526
        coyoteRequest.setCharacterEncoding(enc);
1530
        coyoteRequest.setCharacterEncoding(enc);
1527
1531

Return to bug 51400