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

(-)java/org/apache/tomcat/util/buf/B2CConverter.java (-8 / +18 lines)
Lines 24-33 Link Here
24
import java.nio.charset.CharsetDecoder;
24
import java.nio.charset.CharsetDecoder;
25
import java.nio.charset.CoderResult;
25
import java.nio.charset.CoderResult;
26
import java.nio.charset.CodingErrorAction;
26
import java.nio.charset.CodingErrorAction;
27
import java.nio.charset.IllegalCharsetNameException;
27
import java.nio.charset.StandardCharsets;
28
import java.nio.charset.StandardCharsets;
28
import java.util.HashMap;
29
import java.nio.charset.UnsupportedCharsetException;
30
import java.util.Arrays;
31
import java.util.List;
29
import java.util.Locale;
32
import java.util.Locale;
30
import java.util.Map;
33
import java.util.concurrent.ConcurrentHashMap;
34
import java.util.concurrent.ConcurrentMap;
31
35
32
import org.apache.tomcat.util.res.StringManager;
36
import org.apache.tomcat.util.res.StringManager;
33
37
Lines 39-52 Link Here
39
    private static final StringManager sm =
43
    private static final StringManager sm =
40
        StringManager.getManager(Constants.Package);
44
        StringManager.getManager(Constants.Package);
41
45
42
    private static final Map<String, Charset> encodingToCharsetCache =
46
    private static final ConcurrentMap<String, Charset> encodingToCharsetCache =
43
            new HashMap<>();
47
            new ConcurrentHashMap<>();
44
48
45
    // Protected so unit tests can use it
49
    // Protected so unit tests can use it
46
    protected static final int LEFTOVER_SIZE = 9;
50
    protected static final int LEFTOVER_SIZE = 9;
47
51
48
    static {
52
    static {
49
        for (Charset charset: Charset.availableCharsets().values()) {
53
        List<Charset> preloadCharsets = Arrays.asList(StandardCharsets.ISO_8859_1, StandardCharsets.UTF_8);
54
        for (Charset charset: preloadCharsets) {
50
            encodingToCharsetCache.put(
55
            encodingToCharsetCache.put(
51
                    charset.name().toLowerCase(Locale.ENGLISH), charset);
56
                    charset.name().toLowerCase(Locale.ENGLISH), charset);
52
            for (String alias : charset.aliases()) {
57
            for (String alias : charset.aliases()) {
Lines 74-82 Link Here
74
        Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
79
        Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
75
80
76
        if (charset == null) {
81
        if (charset == null) {
77
            // Pre-population of the cache means this must be invalid
82
            try {
78
            throw new UnsupportedEncodingException(
83
                charset = Charset.forName(lowerCaseEnc);
79
                    sm.getString("b2cConverter.unknownEncoding", lowerCaseEnc));
84
            } catch (UnsupportedCharsetException | IllegalCharsetNameException e) {
85
                throw new UnsupportedEncodingException(
86
                        sm.getString("b2cConverter.unknownEncoding", lowerCaseEnc));
87
            }
88
            encodingToCharsetCache.put(lowerCaseEnc, charset);
80
        }
89
        }
81
        return charset;
90
        return charset;
82
    }
91
    }
Lines 191-194 Link Here
191
            }
200
            }
192
        }
201
        }
193
    }
202
    }
203
194
}
204
}

Return to bug 57808