Class AsciiString

  • All Implemented Interfaces:
    CharSequence, Comparable<CharSequence>

    public final class AsciiString
    extends Object
    implements CharSequence, Comparable<CharSequence>
    A string which has been encoded into a character encoding whose character always takes a single byte, similarly to ASCII. It internally keeps its content in a byte array unlike String, which uses a character array, for reduced memory footprint and faster data transfer from/to byte-based data structures such as a byte array and ByteBuffer. It is often used in conjunction with Headers that require a CharSequence.

    This class was designed to provide an immutable array of bytes, and caches some internal state based upon the value of this array. However underlying access to this byte array is provided via not copying the array on construction or array(). If any changes are made to the underlying byte array it is the user's responsibility to call arrayChanged() so the state of this class can be reset.

    • Constructor Detail

      • AsciiString

        public AsciiString​(byte[] value)
        Initialize this byte string based upon a byte array. A copy will be made.
      • AsciiString

        public AsciiString​(byte[] value,
                           boolean copy)
        Initialize this byte string based upon a byte array. copy determines if a copy is made or the array is shared.
      • AsciiString

        public AsciiString​(byte[] value,
                           int start,
                           int length,
                           boolean copy)
        Construct a new instance from a byte[] array.
        Parameters:
        copy - true then a copy of the memory will be made. false the underlying memory will be shared.
      • AsciiString

        public AsciiString​(ByteBuffer value,
                           boolean copy)
        Initialize an instance based upon the underlying storage from value. There is a potential to share the underlying array storage if ByteBuffer.hasArray() is true. if copy is true a copy will be made of the memory. if copy is false the underlying storage will be shared, if possible.
      • AsciiString

        public AsciiString​(ByteBuffer value,
                           int start,
                           int length,
                           boolean copy)
        Initialize an AsciiString based upon the underlying storage from value. There is a potential to share the underlying array storage if ByteBuffer.hasArray() is true. if copy is true a copy will be made of the memory. if copy is false the underlying storage will be shared, if possible.
      • AsciiString

        public AsciiString​(char[] value)
        Create a copy of value into this instance assuming ASCII encoding.
      • AsciiString

        public AsciiString​(char[] value,
                           int start,
                           int length)
        Create a copy of value into this instance assuming ASCII encoding. The copy will start at index start and copy length bytes.
      • AsciiString

        public AsciiString​(char[] value,
                           Charset charset)
        Create a copy of value into this instance using the encoding type of charset.
      • AsciiString

        public AsciiString​(char[] value,
                           Charset charset,
                           int start,
                           int length)
        Create a copy of value into a this instance using the encoding type of charset. The copy will start at index start and copy length bytes.
      • AsciiString

        public AsciiString​(CharSequence value)
        Create a copy of value into this instance assuming ASCII encoding.
      • AsciiString

        public AsciiString​(CharSequence value,
                           int start,
                           int length)
        Create a copy of value into this instance assuming ASCII encoding. The copy will start at index start and copy length bytes.
      • AsciiString

        public AsciiString​(CharSequence value,
                           Charset charset)
        Create a copy of value into this instance using the encoding type of charset.
      • AsciiString

        public AsciiString​(CharSequence value,
                           Charset charset,
                           int start,
                           int length)
        Create a copy of value into this instance using the encoding type of charset. The copy will start at index start and copy length bytes.
    • Method Detail

      • forEachByte

        public int forEachByte​(ByteProcessor visitor)
        Iterates over the readable bytes of this buffer with the specified processor in ascending order.
        Returns:
        -1 if the processor iterated to or beyond the end of the readable bytes. The last-visited index If the ByteProcessor.process(byte) returned false.
      • forEachByte

        public int forEachByte​(int index,
                               int length,
                               ByteProcessor visitor)
        Iterates over the specified area of this buffer with the specified processor in ascending order. (i.e. index, (index + 1), .. (index + length - 1)).
        Returns:
        -1 if the processor iterated to or beyond the end of the specified area. The last-visited index If the ByteProcessor.process(byte) returned false.
      • forEachByteDesc

        public int forEachByteDesc​(ByteProcessor visitor)
        Iterates over the readable bytes of this buffer with the specified processor in descending order.
        Returns:
        -1 if the processor iterated to or beyond the beginning of the readable bytes. The last-visited index If the ByteProcessor.process(byte) returned false.
      • forEachByteDesc

        public int forEachByteDesc​(int index,
                                   int length,
                                   ByteProcessor visitor)
        Iterates over the specified area of this buffer with the specified processor in descending order. (i.e. (index + length - 1), (index + length - 2), ... index).
        Returns:
        -1 if the processor iterated to or beyond the beginning of the specified area. The last-visited index If the ByteProcessor.process(byte) returned false.
      • byteAt

        public byte byteAt​(int index)
      • isEmpty

        public boolean isEmpty()
        Determine if this instance has 0 length.
      • length

        public int length()
        The length in bytes of this instance.
        Specified by:
        length in interface CharSequence
      • arrayChanged

        public void arrayChanged()
        During normal use cases the AsciiString should be immutable, but if the underlying array is shared, and changes then this needs to be called.
      • isEntireArrayUsed

        public boolean isEntireArrayUsed()
        Determine if the storage represented by array() is entirely used.
        See Also:
        array()
      • toByteArray

        public byte[] toByteArray()
        Converts this string to a byte array.
      • toByteArray

        public byte[] toByteArray​(int start,
                                  int end)
        Converts a subset of this string to a byte array. The subset is defined by the range [start, end).
      • copy

        public void copy​(int srcIdx,
                         byte[] dst,
                         int dstIdx,
                         int length)
        Copies the content of this string to a byte array.
        Parameters:
        srcIdx - the starting offset of characters to copy.
        dst - the destination byte array.
        dstIdx - the starting offset in the destination byte array.
        length - the number of characters to copy.
      • charAt

        public char charAt​(int index)
        Specified by:
        charAt in interface CharSequence
      • contains

        public boolean contains​(CharSequence cs)
        Determines if this String contains the sequence of characters in the CharSequence passed.
        Parameters:
        cs - the character sequence to search for.
        Returns:
        true if the sequence of characters are contained in this string, otherwise false.
      • compareTo

        public int compareTo​(CharSequence string)
        Compares the specified string to this string using the ASCII values of the characters. Returns 0 if the strings contain the same characters in the same order. Returns a negative integer if the first non-equal character in this string has an ASCII value which is less than the ASCII value of the character at the same position in the specified string, or if this string is a prefix of the specified string. Returns a positive integer if the first non-equal character in this string has a ASCII value which is greater than the ASCII value of the character at the same position in the specified string, or if the specified string is a prefix of this string.
        Specified by:
        compareTo in interface Comparable<CharSequence>
        Parameters:
        string - the string to compare.
        Returns:
        0 if the strings are equal, a negative integer if this string is before the specified string, or a positive integer if this string is after the specified string.
        Throws:
        NullPointerException - if string is null.
      • concat

        public AsciiString concat​(CharSequence string)
        Concatenates this string and the specified string.
        Parameters:
        string - the string to concatenate
        Returns:
        a new string which is the concatenation of this string and the specified string.
      • endsWith

        public boolean endsWith​(CharSequence suffix)
        Compares the specified string to this string to determine if the specified string is a suffix.
        Parameters:
        suffix - the suffix to look for.
        Returns:
        true if the specified string is a suffix of this string, false otherwise.
        Throws:
        NullPointerException - if suffix is null.
      • contentEqualsIgnoreCase

        public boolean contentEqualsIgnoreCase​(CharSequence string)
        Compares the specified string to this string ignoring the case of the characters and returns true if they are equal.
        Parameters:
        string - the string to compare.
        Returns:
        true if the specified string is equal to this string, false otherwise.
      • toCharArray

        public char[] toCharArray()
        Copies the characters in this string to a character array.
        Returns:
        a character array containing the characters of this string.
      • toCharArray

        public char[] toCharArray​(int start,
                                  int end)
        Copies the characters in this string to a character array.
        Returns:
        a character array containing the characters of this string.
      • copy

        public void copy​(int srcIdx,
                         char[] dst,
                         int dstIdx,
                         int length)
        Copied the content of this string to a character array.
        Parameters:
        srcIdx - the starting offset of characters to copy.
        dst - the destination character array.
        dstIdx - the starting offset in the destination byte array.
        length - the number of characters to copy.
      • subSequence

        public AsciiString subSequence​(int start)
        Copies a range of characters into a new string.
        Parameters:
        start - the offset of the first character (inclusive).
        Returns:
        a new string containing the characters from start to the end of the string.
        Throws:
        IndexOutOfBoundsException - if start < 0 or start > length().
      • subSequence

        public AsciiString subSequence​(int start,
                                       int end)
        Copies a range of characters into a new string.
        Specified by:
        subSequence in interface CharSequence
        Parameters:
        start - the offset of the first character (inclusive).
        end - The index to stop at (exclusive).
        Returns:
        a new string containing the characters from start to the end of the string.
        Throws:
        IndexOutOfBoundsException - if start < 0 or start > length().
      • subSequence

        public AsciiString subSequence​(int start,
                                       int end,
                                       boolean copy)
        Either copy or share a subset of underlying sub-sequence of bytes.
        Parameters:
        start - the offset of the first character (inclusive).
        end - The index to stop at (exclusive).
        copy - If true then a copy of the underlying storage will be made. If false then the underlying storage will be shared.
        Returns:
        a new string containing the characters from start to the end of the string.
        Throws:
        IndexOutOfBoundsException - if start < 0 or start > length().
      • indexOf

        public int indexOf​(CharSequence string)
        Searches in this string for the first index of the specified string. The search for the string starts at the beginning and moves towards the end of this string.
        Parameters:
        string - the string to find.
        Returns:
        the index of the first character of the specified string in this string, -1 if the specified string is not a substring.
        Throws:
        NullPointerException - if string is null.
      • indexOf

        public int indexOf​(CharSequence subString,
                           int start)
        Searches in this string for the index of the specified string. The search for the string starts at the specified offset and moves towards the end of this string.
        Parameters:
        subString - the string to find.
        start - the starting offset.
        Returns:
        the index of the first character of the specified string in this string, -1 if the specified string is not a substring.
        Throws:
        NullPointerException - if subString is null.
      • indexOf

        public int indexOf​(char ch,
                           int start)
        Searches in this string for the index of the specified char ch. The search for the char starts at the specified offset start and moves towards the end of this string.
        Parameters:
        ch - the char to find.
        start - the starting offset.
        Returns:
        the index of the first occurrence of the specified char ch in this string, -1 if found no occurrence.
      • lastIndexOf

        public int lastIndexOf​(CharSequence string)
        Searches in this string for the last index of the specified string. The search for the string starts at the end and moves towards the beginning of this string.
        Parameters:
        string - the string to find.
        Returns:
        the index of the first character of the specified string in this string, -1 if the specified string is not a substring.
        Throws:
        NullPointerException - if string is null.
      • lastIndexOf

        public int lastIndexOf​(CharSequence subString,
                               int start)
        Searches in this string for the index of the specified string. The search for the string starts at the specified offset and moves towards the beginning of this string.
        Parameters:
        subString - the string to find.
        start - the starting offset.
        Returns:
        the index of the first character of the specified string in this string , -1 if the specified string is not a substring.
        Throws:
        NullPointerException - if subString is null.
      • regionMatches

        public boolean regionMatches​(int thisStart,
                                     CharSequence string,
                                     int start,
                                     int length)
        Compares the specified string to this string and compares the specified range of characters to determine if they are the same.
        Parameters:
        thisStart - the starting offset in this string.
        string - the string to compare.
        start - the starting offset in the specified string.
        length - the number of characters to compare.
        Returns:
        true if the ranges of characters are equal, false otherwise
        Throws:
        NullPointerException - if string is null.
      • regionMatches

        public boolean regionMatches​(boolean ignoreCase,
                                     int thisStart,
                                     CharSequence string,
                                     int start,
                                     int length)
        Compares the specified string to this string and compares the specified range of characters to determine if they are the same. When ignoreCase is true, the case of the characters is ignored during the comparison.
        Parameters:
        ignoreCase - specifies if case should be ignored.
        thisStart - the starting offset in this string.
        string - the string to compare.
        start - the starting offset in the specified string.
        length - the number of characters to compare.
        Returns:
        true if the ranges of characters are equal, false otherwise.
        Throws:
        NullPointerException - if string is null.
      • replace

        public AsciiString replace​(char oldChar,
                                   char newChar)
        Copies this string replacing occurrences of the specified character with another character.
        Parameters:
        oldChar - the character to replace.
        newChar - the replacement character.
        Returns:
        a new string with occurrences of oldChar replaced by newChar.
      • startsWith

        public boolean startsWith​(CharSequence prefix)
        Compares the specified string to this string to determine if the specified string is a prefix.
        Parameters:
        prefix - the string to look for.
        Returns:
        true if the specified string is a prefix of this string, false otherwise
        Throws:
        NullPointerException - if prefix is null.
      • startsWith

        public boolean startsWith​(CharSequence prefix,
                                  int start)
        Compares the specified string to this string, starting at the specified offset, to determine if the specified string is a prefix.
        Parameters:
        prefix - the string to look for.
        start - the starting offset.
        Returns:
        true if the specified string occurs in this string at the specified offset, false otherwise.
        Throws:
        NullPointerException - if prefix is null.
      • toLowerCase

        public AsciiString toLowerCase()
        Converts the characters in this string to lowercase, using the default Locale.
        Returns:
        a new string containing the lowercase characters equivalent to the characters in this string.
      • toUpperCase

        public AsciiString toUpperCase()
        Converts the characters in this string to uppercase, using the default Locale.
        Returns:
        a new string containing the uppercase characters equivalent to the characters in this string.
      • trim

        public static CharSequence trim​(CharSequence c)
        Copies this string removing white space characters from the beginning and end of the string, and tries not to copy if possible.
        Parameters:
        c - The CharSequence to trim.
        Returns:
        a new string with characters <= \\u0020 removed from the beginning and the end.
      • trim

        public AsciiString trim()
        Duplicates this string removing white space characters from the beginning and end of the string, without copying.
        Returns:
        a new string with characters <= \\u0020 removed from the beginning and the end.
      • contentEquals

        public boolean contentEquals​(CharSequence a)
        Compares a CharSequence to this String to determine if their contents are equal.
        Parameters:
        a - the character sequence to compare to.
        Returns:
        true if equal, otherwise false
      • matches

        public boolean matches​(String expr)
        Determines whether this string matches a given regular expression.
        Parameters:
        expr - the regular expression to be matched.
        Returns:
        true if the expression matches, otherwise false.
        Throws:
        PatternSyntaxException - if the syntax of the supplied regular expression is not valid.
        NullPointerException - if expr is null.
      • split

        public AsciiString[] split​(String expr,
                                   int max)
        Splits this string using the supplied regular expression expr. The parameter max controls the behavior how many times the pattern is applied to the string.
        Parameters:
        expr - the regular expression used to divide the string.
        max - the number of entries in the resulting array.
        Returns:
        an array of Strings created by separating the string along matches of the regular expression.
        Throws:
        NullPointerException - if expr is null.
        PatternSyntaxException - if the syntax of the supplied regular expression is not valid.
        See Also:
        Pattern.split(CharSequence, int)
      • split

        public AsciiString[] split​(char delim)
        Splits the specified String with the specified delimiter.
      • hashCode

        public int hashCode()

        Provides a case-insensitive hash code for Ascii like byte strings.

        Overrides:
        hashCode in class Object
      • toString

        public String toString​(int start)
        Translates the entire byte string to a String using the charset encoding.
        See Also:
        toString(int, int)
      • toString

        public String toString​(int start,
                               int end)
        Translates the [start, end) range of this byte string to a String.
      • parseBoolean

        public boolean parseBoolean()
      • parseChar

        public char parseChar()
      • parseChar

        public char parseChar​(int start)
      • parseShort

        public short parseShort()
      • parseShort

        public short parseShort​(int radix)
      • parseShort

        public short parseShort​(int start,
                                int end)
      • parseShort

        public short parseShort​(int start,
                                int end,
                                int radix)
      • parseInt

        public int parseInt()
      • parseInt

        public int parseInt​(int radix)
      • parseInt

        public int parseInt​(int start,
                            int end)
      • parseInt

        public int parseInt​(int start,
                            int end,
                            int radix)
      • parseLong

        public long parseLong()
      • parseLong

        public long parseLong​(int radix)
      • parseLong

        public long parseLong​(int start,
                              int end)
      • parseLong

        public long parseLong​(int start,
                              int end,
                              int radix)
      • parseFloat

        public float parseFloat()
      • parseFloat

        public float parseFloat​(int start,
                                int end)
      • parseDouble

        public double parseDouble()
      • parseDouble

        public double parseDouble​(int start,
                                  int end)
      • cached

        public static AsciiString cached​(String string)
        Returns an AsciiString containing the given string and retains/caches the input string for later use in toString(). Used for the constants (which already stored in the JVM's string table) and in cases where the guaranteed use of the toString() method.
      • hashCode

        public static int hashCode​(CharSequence value)
        Returns the case-insensitive hash code of the specified string. Note that this method uses the same hashing algorithm with hashCode() so that you can put both AsciiStrings and arbitrary CharSequences into the same headers.
      • contains

        public static boolean contains​(CharSequence a,
                                       CharSequence b)
        Determine if a contains b in a case sensitive manner.
      • containsIgnoreCase

        public static boolean containsIgnoreCase​(CharSequence a,
                                                 CharSequence b)
        Determine if a contains b in a case insensitive manner.
      • contentEqualsIgnoreCase

        public static boolean contentEqualsIgnoreCase​(CharSequence a,
                                                      CharSequence b)
        Returns true if both CharSequence's are equals when ignore the case. This only supports 8-bit ASCII.
      • contentEquals

        public static boolean contentEquals​(CharSequence a,
                                            CharSequence b)
        Returns true if the content of both CharSequence's are equals. This only supports 8-bit ASCII.
      • regionMatches

        public static boolean regionMatches​(CharSequence cs,
                                            boolean ignoreCase,
                                            int csStart,
                                            CharSequence string,
                                            int start,
                                            int length)
        This methods make regionMatches operation correctly for any chars in strings
        Parameters:
        cs - the CharSequence to be processed
        ignoreCase - specifies if case should be ignored.
        csStart - the starting offset in the cs CharSequence
        string - the CharSequence to compare.
        start - the starting offset in the specified string.
        length - the number of characters to compare.
        Returns:
        true if the ranges of characters are equal, false otherwise.
      • regionMatchesAscii

        public static boolean regionMatchesAscii​(CharSequence cs,
                                                 boolean ignoreCase,
                                                 int csStart,
                                                 CharSequence string,
                                                 int start,
                                                 int length)
        This is optimized version of regionMatches for string with ASCII chars only
        Parameters:
        cs - the CharSequence to be processed
        ignoreCase - specifies if case should be ignored.
        csStart - the starting offset in the cs CharSequence
        string - the CharSequence to compare.
        start - the starting offset in the specified string.
        length - the number of characters to compare.
        Returns:
        true if the ranges of characters are equal, false otherwise.
      • indexOfIgnoreCase

        public static int indexOfIgnoreCase​(CharSequence str,
                                            CharSequence searchStr,
                                            int startPos)

        Case in-sensitive find of the first index within a CharSequence from the specified position.

        A null CharSequence will return -1. A negative start position is treated as zero. An empty ("") search CharSequence always matches. A start position greater than the string length only matches an empty search CharSequence.

         AsciiString.indexOfIgnoreCase(null, *, *)          = -1
         AsciiString.indexOfIgnoreCase(*, null, *)          = -1
         AsciiString.indexOfIgnoreCase("", "", 0)           = 0
         AsciiString.indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
         AsciiString.indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
         AsciiString.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
         AsciiString.indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
         AsciiString.indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
         AsciiString.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
         AsciiString.indexOfIgnoreCase("aabaabaa", "", 2)   = 2
         AsciiString.indexOfIgnoreCase("abc", "", 9)        = -1
         
        Parameters:
        str - the CharSequence to check, may be null
        searchStr - the CharSequence to find, may be null
        startPos - the start position, negative treated as zero
        Returns:
        the first index of the search CharSequence (always ≥ startPos), -1 if no match or null string input
      • indexOfIgnoreCaseAscii

        public static int indexOfIgnoreCaseAscii​(CharSequence str,
                                                 CharSequence searchStr,
                                                 int startPos)

        Case in-sensitive find of the first index within a CharSequence from the specified position. This method optimized and works correctly for ASCII CharSequences only

        A null CharSequence will return -1. A negative start position is treated as zero. An empty ("") search CharSequence always matches. A start position greater than the string length only matches an empty search CharSequence.

         AsciiString.indexOfIgnoreCase(null, *, *)          = -1
         AsciiString.indexOfIgnoreCase(*, null, *)          = -1
         AsciiString.indexOfIgnoreCase("", "", 0)           = 0
         AsciiString.indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
         AsciiString.indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
         AsciiString.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
         AsciiString.indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
         AsciiString.indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
         AsciiString.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
         AsciiString.indexOfIgnoreCase("aabaabaa", "", 2)   = 2
         AsciiString.indexOfIgnoreCase("abc", "", 9)        = -1
         
        Parameters:
        str - the CharSequence to check, may be null
        searchStr - the CharSequence to find, may be null
        startPos - the start position, negative treated as zero
        Returns:
        the first index of the search CharSequence (always ≥ startPos), -1 if no match or null string input
      • indexOf

        public static int indexOf​(CharSequence cs,
                                  char searchChar,
                                  int start)

        Finds the first index in the CharSequence that matches the specified character.

        Parameters:
        cs - the CharSequence to be processed, not null
        searchChar - the char to be searched for
        start - the start index, negative starts at the string start
        Returns:
        the index where the search char was found, -1 if char searchChar is not found or cs == null
      • toLowerCase

        public static char toLowerCase​(char c)
        If the character is uppercase - converts the character to lowercase, otherwise returns the character as it is. Only for ASCII characters.
        Returns:
        lowercase ASCII character equivalent
      • isUpperCase

        public static boolean isUpperCase​(byte value)
      • isUpperCase

        public static boolean isUpperCase​(char value)
      • c2b

        public static byte c2b​(char c)
      • b2c

        public static char b2c​(byte b)