Package org.apache.tomcat.util.buf
Class StringCache
- java.lang.Object
-
- org.apache.tomcat.util.buf.StringCache
-
public class StringCache extends java.lang.ObjectThis class implements a String cache for ByteChunk and CharChunk.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classStringCache.ByteEntryCache entry for byte chunk to String mapping.protected static classStringCache.CharEntryCache entry for char chunk to String mapping.
-
Field Summary
Fields Modifier and Type Field Description protected static intaccessCountAccess count.protected static StringCache.ByteEntry[]bcCacheCache for byte chunk.protected static intbcCounttoString count for byte chunk.protected static java.util.HashMap<StringCache.ByteEntry,int[]>bcStatsStatistics hash map for byte chunk.protected static booleanbyteEnabledEnabled ?protected static intcacheSizeMaximum size of the string cache.protected static StringCache.CharEntry[]ccCacheCache for char chunk.protected static intccCounttoString count for char chunk.protected static java.util.HashMap<StringCache.CharEntry,int[]>ccStatsStatistics hash map for char chunk.protected static booleancharEnabledWhether the char chunk string cache is enabled.protected static inthitCountHit count.protected static intmaxStringSizeMaximum length of strings to cache.protected static inttrainThresholdNumber of toString calls required before the cache is built.
-
Constructor Summary
Constructors Constructor Description StringCache()Constructs a new StringCache with default settings.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected static intcompare(ByteChunk name, byte[] compareTo)Compare given byte chunk with byte array.protected static intcompare(CharChunk name, char[] compareTo)Compare given char chunk with char array.protected static java.lang.Stringfind(ByteChunk name)Deprecated.Unused.protected static java.lang.Stringfind(ByteChunk name, java.nio.charset.CodingErrorAction malformedInputAction, java.nio.charset.CodingErrorAction unmappableCharacterAction)Find an entry given its name in the cache and return the associated String.protected static java.lang.Stringfind(CharChunk name)Find an entry given its name in the cache and return the associated String.protected static intfindClosest(ByteChunk name, StringCache.ByteEntry[] array, int len)Find an entry given its name in a sorted array of map elements.protected static intfindClosest(CharChunk name, StringCache.CharEntry[] array, int len)Find an entry given its name in a sorted array of map elements.intgetAccessCount()Returns the total number of cache lookups performed.booleangetByteEnabled()Returns whether the byte chunk string cache is enabled.intgetCacheSize()Returns the maximum size of the string cache.booleangetCharEnabled()Returns whether the char chunk string cache is enabled.intgetHitCount()Returns the number of cache hits.intgetTrainThreshold()Returns the number of toString calls required before the cache is built.voidreset()Resets all cache statistics and clears the cache.voidsetByteEnabled(boolean byteEnabled)Sets whether the byte chunk string cache is enabled.voidsetCacheSize(int cacheSize)Sets the maximum size of the string cache.voidsetCharEnabled(boolean charEnabled)Sets whether the char chunk string cache is enabled.voidsetTrainThreshold(int trainThreshold)Sets the number of toString calls required before the cache is built.static java.lang.StringtoString(ByteChunk bc)Converts the given ByteChunk to a String using the cache.static java.lang.StringtoString(ByteChunk bc, java.nio.charset.CodingErrorAction malformedInputAction, java.nio.charset.CodingErrorAction unmappableCharacterAction)Converts the given ByteChunk to a String using the cache, with the specified error handling actions.static java.lang.StringtoString(CharChunk cc)Converts the given CharChunk to a String using the cache.
-
-
-
Field Detail
-
byteEnabled
protected static boolean byteEnabled
Enabled ?
-
charEnabled
protected static boolean charEnabled
Whether the char chunk string cache is enabled.
-
trainThreshold
protected static int trainThreshold
Number of toString calls required before the cache is built.
-
cacheSize
protected static int cacheSize
Maximum size of the string cache.
-
maxStringSize
protected static final int maxStringSize
Maximum length of strings to cache.
-
bcStats
protected static final java.util.HashMap<StringCache.ByteEntry,int[]> bcStats
Statistics hash map for byte chunk.
-
bcCount
protected static int bcCount
toString count for byte chunk.
-
bcCache
protected static volatile StringCache.ByteEntry[] bcCache
Cache for byte chunk.
-
ccStats
protected static final java.util.HashMap<StringCache.CharEntry,int[]> ccStats
Statistics hash map for char chunk.
-
ccCount
protected static int ccCount
toString count for char chunk.
-
ccCache
protected static volatile StringCache.CharEntry[] ccCache
Cache for char chunk.
-
accessCount
protected static int accessCount
Access count.
-
hitCount
protected static int hitCount
Hit count.
-
-
Method Detail
-
getCacheSize
public int getCacheSize()
Returns the maximum size of the string cache.- Returns:
- the cache size
-
setCacheSize
public void setCacheSize(int cacheSize)
Sets the maximum size of the string cache.- Parameters:
cacheSize- the cache size
-
getByteEnabled
public boolean getByteEnabled()
Returns whether the byte chunk string cache is enabled.- Returns:
- true if byte chunk caching is enabled
-
setByteEnabled
public void setByteEnabled(boolean byteEnabled)
Sets whether the byte chunk string cache is enabled.- Parameters:
byteEnabled- true if byte chunk caching should be enabled
-
getCharEnabled
public boolean getCharEnabled()
Returns whether the char chunk string cache is enabled.- Returns:
- true if char chunk caching is enabled
-
setCharEnabled
public void setCharEnabled(boolean charEnabled)
Sets whether the char chunk string cache is enabled.- Parameters:
charEnabled- true if char chunk caching should be enabled
-
getTrainThreshold
public int getTrainThreshold()
Returns the number of toString calls required before the cache is built.- Returns:
- the training threshold
-
setTrainThreshold
public void setTrainThreshold(int trainThreshold)
Sets the number of toString calls required before the cache is built.- Parameters:
trainThreshold- the training threshold
-
getAccessCount
public int getAccessCount()
Returns the total number of cache lookups performed.- Returns:
- the access count
-
getHitCount
public int getHitCount()
Returns the number of cache hits.- Returns:
- the hit count
-
reset
public void reset()
Resets all cache statistics and clears the cache.
-
toString
public static java.lang.String toString(ByteChunk bc)
Converts the given ByteChunk to a String using the cache.- Parameters:
bc- the ByteChunk to convert- Returns:
- the resulting String
-
toString
public static java.lang.String toString(ByteChunk bc, java.nio.charset.CodingErrorAction malformedInputAction, java.nio.charset.CodingErrorAction unmappableCharacterAction) throws java.nio.charset.CharacterCodingException
Converts the given ByteChunk to a String using the cache, with the specified error handling actions.- Parameters:
bc- the ByteChunk to convertmalformedInputAction- action for malformed inputunmappableCharacterAction- action for unmappable characters- Returns:
- the resulting String
- Throws:
java.nio.charset.CharacterCodingException- if a coding error occurs
-
toString
public static java.lang.String toString(CharChunk cc)
Converts the given CharChunk to a String using the cache.- Parameters:
cc- the CharChunk to convert- Returns:
- the resulting String
-
compare
protected static int compare(ByteChunk name, byte[] compareTo)
Compare given byte chunk with byte array.- Parameters:
name- The name to comparecompareTo- The compared to data- Returns:
- -1, 0 or +1 if inferior, equal, or superior to the String.
-
find
@Deprecated protected static final java.lang.String find(ByteChunk name)
Deprecated.Unused. Will be removed in Tomcat 11. Usefind(ByteChunk, CodingErrorAction, CodingErrorAction)Find an entry given its name in the cache and return the associated String.- Parameters:
name- The name to find- Returns:
- the corresponding value
-
find
protected static java.lang.String find(ByteChunk name, java.nio.charset.CodingErrorAction malformedInputAction, java.nio.charset.CodingErrorAction unmappableCharacterAction)
Find an entry given its name in the cache and return the associated String.- Parameters:
name- The name to findmalformedInputAction- Action to take if a malformed input is encounteredunmappableCharacterAction- Action to take if an unmappable character is encountered- Returns:
- the corresponding value
-
findClosest
protected static int findClosest(ByteChunk name, StringCache.ByteEntry[] array, int len)
Find an entry given its name in a sorted array of map elements. This will return the index for the closest inferior or equal item in the given array.- Parameters:
name- The name to findarray- The array in which to looklen- The effective length of the array- Returns:
- the position of the best match
-
compare
protected static int compare(CharChunk name, char[] compareTo)
Compare given char chunk with char array.- Parameters:
name- The name to comparecompareTo- The compared to data- Returns:
- -1, 0 or +1 if inferior, equal, or superior to the String.
-
find
protected static java.lang.String find(CharChunk name)
Find an entry given its name in the cache and return the associated String.- Parameters:
name- The name to find- Returns:
- the corresponding value
-
findClosest
protected static int findClosest(CharChunk name, StringCache.CharEntry[] array, int len)
Find an entry given its name in a sorted array of map elements. This will return the index for the closest inferior or equal item in the given array.- Parameters:
name- The name to findarray- The array in which to looklen- The effective length of the array- Returns:
- the position of the best match
-
-