Class StringCache
java.lang.Object
org.apache.tomcat.util.buf.StringCache
This class implements a String cache for ByteChunk and CharChunk.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classCache entry for byte chunk to String mapping.protected static classCache entry for char chunk to String mapping. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static intAccess count.protected static StringCache.ByteEntry[]Cache for byte chunk.protected static inttoString count for byte chunk.protected static final HashMap<StringCache.ByteEntry, int[]> Statistics hash map for byte chunk.protected static booleanEnabled ?protected static intMaximum size of the string cache.protected static StringCache.CharEntry[]Cache for char chunk.protected static inttoString count for char chunk.protected static final HashMap<StringCache.CharEntry, int[]> Statistics hash map for char chunk.protected static booleanWhether the char chunk string cache is enabled.protected static intHit count.protected static final intMaximum length of strings to cache.protected static intNumber of toString calls required before the cache is built. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static intCompare given byte chunk with byte array.protected static intCompare given char chunk with char array.protected static final StringDeprecated.Unused.protected static Stringfind(ByteChunk name, CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) Find an entry given its name in the cache and return the associated String.protected static StringFind 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.intReturns the total number of cache lookups performed.booleanReturns whether the byte chunk string cache is enabled.intReturns the maximum size of the string cache.booleanReturns whether the char chunk string cache is enabled.intReturns the number of cache hits.intReturns 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 StringConverts the given ByteChunk to a String using the cache.static StringtoString(ByteChunk bc, CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) Converts the given ByteChunk to a String using the cache, with the specified error handling actions.static StringConverts the given CharChunk to a String using the cache.
-
Field Details
-
byteEnabled
protected static boolean byteEnabledEnabled ? -
charEnabled
protected static boolean charEnabledWhether the char chunk string cache is enabled. -
trainThreshold
protected static int trainThresholdNumber of toString calls required before the cache is built. -
cacheSize
protected static int cacheSizeMaximum size of the string cache. -
maxStringSize
protected static final int maxStringSizeMaximum length of strings to cache. -
bcStats
Statistics hash map for byte chunk. -
bcCount
protected static int bcCounttoString count for byte chunk. -
bcCache
Cache for byte chunk. -
ccStats
Statistics hash map for char chunk. -
ccCount
protected static int ccCounttoString count for char chunk. -
ccCache
Cache for char chunk. -
accessCount
protected static int accessCountAccess count. -
hitCount
protected static int hitCountHit count.
-
-
Constructor Details
-
StringCache
public StringCache()Constructs a new StringCache with default settings.
-
-
Method Details
-
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
-
toString
public static String toString(ByteChunk bc, CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) throws 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:
CharacterCodingException- if a coding error occurs
-
toString
-
compare
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.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 String find(ByteChunk name, CodingErrorAction malformedInputAction, 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
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
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
-
findClosest
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
-