Class StringCache


  • public class StringCache
    extends java.lang.Object
    This class implements a String cache for ByteChunk and CharChunk.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  StringCache.ByteEntry
      Cache entry for byte chunk to String mapping.
      protected static class  StringCache.CharEntry
      Cache entry for char chunk to String mapping.
    • 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 int compare​(ByteChunk name, byte[] compareTo)
      Compare given byte chunk with byte array.
      protected static int compare​(CharChunk name, char[] compareTo)
      Compare given char chunk with char array.
      protected static java.lang.String find​(ByteChunk name)
      Deprecated.
      Unused.
      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.
      protected static java.lang.String find​(CharChunk name)
      Find an entry given its name in the cache and return the associated String.
      protected static int findClosest​(ByteChunk name, StringCache.ByteEntry[] array, int len)
      Find an entry given its name in a sorted array of map elements.
      protected static int findClosest​(CharChunk name, StringCache.CharEntry[] array, int len)
      Find an entry given its name in a sorted array of map elements.
      int getAccessCount()
      Returns the total number of cache lookups performed.
      boolean getByteEnabled()
      Returns whether the byte chunk string cache is enabled.
      int getCacheSize()
      Returns the maximum size of the string cache.
      boolean getCharEnabled()
      Returns whether the char chunk string cache is enabled.
      int getHitCount()
      Returns the number of cache hits.
      int getTrainThreshold()
      Returns the number of toString calls required before the cache is built.
      void reset()
      Resets all cache statistics and clears the cache.
      void setByteEnabled​(boolean byteEnabled)
      Sets whether the byte chunk string cache is enabled.
      void setCacheSize​(int cacheSize)
      Sets the maximum size of the string cache.
      void setCharEnabled​(boolean charEnabled)
      Sets whether the char chunk string cache is enabled.
      void setTrainThreshold​(int trainThreshold)
      Sets the number of toString calls required before the cache is built.
      static java.lang.String toString​(ByteChunk bc)
      Converts the given ByteChunk to a String using the cache.
      static java.lang.String toString​(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.String toString​(CharChunk cc)
      Converts the given CharChunk to a String using the cache.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.
      • 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.
      • accessCount

        protected static int accessCount
        Access count.
      • hitCount

        protected static int hitCount
        Hit count.
    • Constructor Detail

      • StringCache

        public StringCache()
        Constructs a new StringCache with default settings.
    • 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 convert
        malformedInputAction - action for malformed input
        unmappableCharacterAction - 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 compare
        compareTo - 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. Use find(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 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 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 find
        array - The array in which to look
        len - The effective length of the array
        Returns:
        the position of the best match