~ubuntu-branches/ubuntu/precise/tomcat7/precise-proposed

« back to all changes in this revision

Viewing changes to java/org/apache/tomcat/util/buf/B2CConverter.java

  • Committer: Bazaar Package Importer
  • Author(s): tony mancill
  • Date: 2011-07-25 22:58:33 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110725225833-1t773ak3y3g9utm2
Tags: 7.0.19-1
* Team upload.
* New upstream release.
  - Includes fix for CVE-2011-2526 (Closes: #634992)
* Remove patch for CVE-2011-2204 (included upstream).

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import java.io.InputStream;
23
23
import java.io.InputStreamReader;
24
24
import java.io.UnsupportedEncodingException;
 
25
import java.nio.charset.Charset;
 
26
import java.util.HashMap;
 
27
import java.util.Locale;
 
28
import java.util.Map;
 
29
 
 
30
import org.apache.tomcat.util.res.StringManager;
25
31
 
26
32
/** Efficient conversion of bytes  to character .
27
33
 *  
40
46
    private static final org.apache.juli.logging.Log log=
41
47
        org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
42
48
    
 
49
    private static final StringManager sm =
 
50
        StringManager.getManager(Constants.Package);
 
51
 
 
52
    private static final Map<String, Charset> encodingToCharsetCache =
 
53
        new HashMap<String, Charset>();
 
54
 
 
55
    static {
 
56
        for (Charset charset: Charset.availableCharsets().values()) {
 
57
            encodingToCharsetCache.put(
 
58
                    charset.name().toLowerCase(Locale.US), charset);
 
59
            for (String alias : charset.aliases()) {
 
60
                encodingToCharsetCache.put(
 
61
                        alias.toLowerCase(Locale.US), charset);
 
62
            }
 
63
        }
 
64
    }
 
65
 
 
66
    public static Charset getCharset(String enc)
 
67
            throws UnsupportedEncodingException{
 
68
 
 
69
        // Encoding names should all be ASCII
 
70
        String lowerCaseEnc = enc.toLowerCase(Locale.US);
 
71
 
 
72
        Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
 
73
        
 
74
        if (charset == null) {
 
75
            // Pre-population of the cache means this must be invalid
 
76
            throw new UnsupportedEncodingException(
 
77
                    sm.getString("b2cConvertor.unknownEncoding", enc));
 
78
        }
 
79
        return charset;
 
80
    }
 
81
    
43
82
    private IntermediateInputStream iis;
44
83
    private ReadConvertor conv;
45
84
    private String encoding;
104
143
    {
105
144
        // destroy the reader/iis
106
145
        iis=new IntermediateInputStream();
107
 
        conv=new ReadConvertor( iis, encoding );
 
146
        conv = new ReadConvertor(iis, getCharset(encoding));
108
147
    }
109
148
 
110
149
}
120
159
    
121
160
    /** Create a converter.
122
161
     */
123
 
    public ReadConvertor( IntermediateInputStream in, String enc )
124
 
        throws UnsupportedEncodingException
125
 
    {
126
 
        super( in, enc );
 
162
    public ReadConvertor(IntermediateInputStream in, Charset charset) {
 
163
        super(in, charset);
127
164
    }
128
165
    
129
166
    /** Overridden - will do nothing but reset internal state.