Class StringCache

java.lang.Object
org.apache.tomcat.util.buf.StringCache

public class StringCache extends Object
This class implements a String cache for ByteChunk and CharChunk.
  • Field Details

    • 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 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 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.
  • 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

      public static 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 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 convert
      malformedInputAction - action for malformed input
      unmappableCharacterAction - action for unmappable characters
      Returns:
      the resulting String
      Throws:
      CharacterCodingException - if a coding error occurs
    • toString

      public static 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 compare
      compareTo - The compared to data
      Returns:
      -1, 0 or +1 if inferior, equal, or superior to the String.
    • 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 find
      malformedInputAction - Action to take if a malformed input is encountered
      unmappableCharacterAction - 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 find
      array - The array in which to look
      len - 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 compare
      compareTo - The compared to data
      Returns:
      -1, 0 or +1 if inferior, equal, or superior to the String.
    • find

      protected static 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 find
      array - The array in which to look
      len - The effective length of the array
      Returns:
      the position of the best match