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

(-)webapps/docs/config/systemprops.xml (+11 lines)
Lines 673-678 Link Here
673
      <p>The URL for the catalina.properties configuration file.</p>
673
      <p>The URL for the catalina.properties configuration file.</p>
674
    </property>
674
    </property>
675
675
676
    <property name="tomcat.util.buf.B2CConverter.preloadCharsets">
677
      <p>Comma speparted list of character set names to preload. If set the
678
      character sets with the specified names and all their aliases will be
679
      preloaded at start up time. All others will be loaded lazily at runtime
680
      when needed. If not set all available character sets will be loaded at
681
      start up time.</p>
682
      <p>Using something like "US-ASCII,ISO-8859-1,UTF-8" can reduce Tomcat
683
      memory usage by about 5MB and reduce start up times by 50ms to 100ms.</p>
684
      <p>The character set names are treated case insensitive.</p>
685
    </property>
686
676
    <property name="tomcat.util.buf.StringCache.byte.enabled">
687
    <property name="tomcat.util.buf.StringCache.byte.enabled">
677
      <p>If <code>true</code>, the String cache is enabled for
688
      <p>If <code>true</code>, the String cache is enabled for
678
      <code>ByteChunk</code>.</p>
689
      <code>ByteChunk</code>.</p>
(-)java/org/apache/tomcat/util/buf/B2CConverter.java (-10 / +44 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;
29
import java.nio.charset.UnsupportedCharsetException;
30
import java.util.Arrays;
28
import java.util.HashMap;
31
import java.util.HashMap;
32
import java.util.List;
29
import java.util.Locale;
33
import java.util.Locale;
30
import java.util.Map;
34
import java.util.Map;
35
import java.util.concurrent.ConcurrentHashMap;
31
36
32
import org.apache.tomcat.util.res.StringManager;
37
import org.apache.tomcat.util.res.StringManager;
33
38
Lines 39-58 Link Here
39
    private static final StringManager sm =
44
    private static final StringManager sm =
40
        StringManager.getManager(Constants.Package);
45
        StringManager.getManager(Constants.Package);
41
46
42
    private static final Map<String, Charset> encodingToCharsetCache =
47
    private static final boolean preloadAllCharsets;
43
            new HashMap<>();
44
48
49
    private static final Map<String, Charset> encodingToCharsetCache;
50
45
    // Protected so unit tests can use it
51
    // Protected so unit tests can use it
46
    protected static final int LEFTOVER_SIZE = 9;
52
    protected static final int LEFTOVER_SIZE = 9;
47
53
48
    static {
54
    static {
49
        for (Charset charset: Charset.availableCharsets().values()) {
55
        String preloadCharsets = System.getProperty("tomcat.util.buf.B2CConverter.preloadCharsets");
50
            encodingToCharsetCache.put(
56
        preloadAllCharsets = preloadCharsets == null;
51
                    charset.name().toLowerCase(Locale.ENGLISH), charset);
57
        if (preloadAllCharsets) {
52
            for (String alias : charset.aliases()) {
58
            encodingToCharsetCache = new HashMap<>();
59
            for (Charset charset: Charset.availableCharsets().values()) {
53
                encodingToCharsetCache.put(
60
                encodingToCharsetCache.put(
54
                        alias.toLowerCase(Locale.ENGLISH), charset);
61
                        charset.name().toLowerCase(Locale.ENGLISH), charset);
62
                for (String alias : charset.aliases()) {
63
                    encodingToCharsetCache.put(
64
                            alias.toLowerCase(Locale.ENGLISH), charset);
65
                }
55
            }
66
            }
67
        } else {
68
            encodingToCharsetCache = new ConcurrentHashMap<>();
69
            String[] charsetNames = preloadCharsets.split(",");
70
            for (String charsetName : charsetNames) {
71
                Charset charset = Charset.forName(charsetName);
72
                encodingToCharsetCache.put(
73
                        charset.name().toLowerCase(Locale.ENGLISH), charset);
74
                for (String alias : charset.aliases()) {
75
                    encodingToCharsetCache.put(
76
                            alias.toLowerCase(Locale.ENGLISH), charset);
77
                }
78
            }
56
        }
79
        }
57
    }
80
    }
58
81
Lines 74-82 Link Here
74
        Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
97
        Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
75
98
76
        if (charset == null) {
99
        if (charset == null) {
77
            // Pre-population of the cache means this must be invalid
100
            if (preloadAllCharsets) {
78
            throw new UnsupportedEncodingException(
101
                // Pre-population of the cache means this must be invalid
79
                    sm.getString("b2cConverter.unknownEncoding", lowerCaseEnc));
102
                throw new UnsupportedEncodingException(
103
                        sm.getString("b2cConverter.unknownEncoding", lowerCaseEnc));
104
            }
105
106
            try {
107
                charset = Charset.forName(lowerCaseEnc);
108
            } catch (UnsupportedCharsetException | IllegalCharsetNameException e) {
109
                throw new UnsupportedEncodingException(
110
                        sm.getString("b2cConverter.unknownEncoding", lowerCaseEnc));
111
            }
112
            encodingToCharsetCache.put(lowerCaseEnc, charset);
80
        }
113
        }
81
        return charset;
114
        return charset;
82
    }
115
    }
Lines 191-194 Link Here
191
            }
224
            }
192
        }
225
        }
193
    }
226
    }
227
194
}
228
}

Return to bug 57808