Class CharChunk

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.CharSequence, java.lang.Cloneable

    public final class CharChunk
    extends AbstractChunk
    implements java.lang.CharSequence
    Utilities to manipulate char chunks. While String is the easiest way to manipulate chars ( search, substrings, etc), it is known to not be the most efficient solution - Strings are designed as immutable and secure objects.
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  CharChunk.CharInputChannel
      Input interface, used when the buffer is empty.
      static interface  CharChunk.CharOutputChannel
      When we need more space we'll either grow the buffer ( up to the limit ) or send it to a channel.
    • Constructor Summary

      Constructors 
      Constructor Description
      CharChunk()
      Creates a new, uninitialized CharChunk object.
      CharChunk​(int initial)
      Creates a new CharChunk with the specified initial buffer size.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void allocate​(int initial, int limit)
      Allocates a buffer of the specified size.
      void append​(char c)
      Appends a single character to the buffer.
      void append​(char[] src, int off, int len)
      Add data to the buffer.
      void append​(java.lang.String s)
      Append a string to the buffer.
      void append​(java.lang.String s, int off, int len)
      Append a string to the buffer.
      void append​(CharChunk src)
      Appends the contents of the source CharChunk to this buffer.
      char charAt​(int index)  
      CharChunk clone()  
      boolean endsWith​(java.lang.String s)
      Checks if the buffer ends with the specified string.
      boolean equals​(char[] b2, int off2, int len2)
      Compares this CharChunk to a character array.
      boolean equals​(java.lang.Object obj)
      boolean equals​(java.lang.String s)
      Compares the message bytes to the specified String object.
      boolean equals​(CharChunk cc)
      Compares this CharChunk to another CharChunk.
      boolean equalsIgnoreCase​(java.lang.String s)
      Compares the message bytes to the specified String object.
      void flushBuffer()
      Send the buffer to the sink.
      char[] getBuffer()
      Returns the underlying character buffer.
      protected int getBufferElement​(int index)
      Returns the element at the specified index in the buffer.
      char[] getChars()
      Returns the character buffer.
      int indexOf​(char c)
      Returns the index of the first occurrence of the given character.
      static int indexOf​(char[] chars, int start, int end, char s)
      Returns the first instance of the given character in the given char array between the specified start and end.
      int indexOf​(char c, int starting)
      Returns the first instance of the given character in this CharChunk starting at the specified char.
      int length()  
      void makeSpace​(int count)
      Make space for len chars.
      void setCharInputChannel​(CharChunk.CharInputChannel in)
      When the buffer is empty, read the data from the input channel.
      void setCharOutputChannel​(CharChunk.CharOutputChannel out)
      When the buffer is full, write the data to the output channel.
      void setChars​(char[] c, int off, int len)
      Sets the buffer to the specified sub array of characters.
      boolean startsWith​(java.lang.String s)
      Checks if the buffer starts with the specified string.
      boolean startsWithIgnoreCase​(java.lang.String s, int pos)
      Returns true if the buffer starts with the specified string.
      java.lang.CharSequence subSequence​(int start, int end)  
      int subtract()
      Subtracts a single character from the buffer.
      int subtract​(char[] dest, int off, int len)
      Subtracts characters from the buffer into the destination array.
      java.lang.String toString()  
      java.lang.String toStringInternal()
      Returns the string representation of the chunk contents.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.CharSequence

        chars, codePoints
    • Constructor Detail

      • CharChunk

        public CharChunk()
        Creates a new, uninitialized CharChunk object.
      • CharChunk

        public CharChunk​(int initial)
        Creates a new CharChunk with the specified initial buffer size.
        Parameters:
        initial - the initial buffer size
    • Method Detail

      • clone

        public CharChunk clone()
                        throws java.lang.CloneNotSupportedException
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException
      • allocate

        public void allocate​(int initial,
                             int limit)
        Allocates a buffer of the specified size.
        Parameters:
        initial - the initial buffer size
        limit - the maximum buffer size
      • setChars

        public void setChars​(char[] c,
                             int off,
                             int len)
        Sets the buffer to the specified sub array of characters.
        Parameters:
        c - the characters
        off - the start offset of the characters
        len - the length of the characters
      • getChars

        public char[] getChars()
        Returns the character buffer.
        Returns:
        the character buffer
      • getBuffer

        public char[] getBuffer()
        Returns the underlying character buffer.
        Returns:
        the underlying character buffer
      • setCharInputChannel

        public void setCharInputChannel​(CharChunk.CharInputChannel in)
        When the buffer is empty, read the data from the input channel.
        Parameters:
        in - The input channel
      • setCharOutputChannel

        public void setCharOutputChannel​(CharChunk.CharOutputChannel out)
        When the buffer is full, write the data to the output channel. Also used when large amount of data is appended. If not set, the buffer will grow to the limit.
        Parameters:
        out - The output channel
      • append

        public void append​(char c)
                    throws java.io.IOException
        Appends a single character to the buffer.
        Parameters:
        c - the character to append
        Throws:
        java.io.IOException - Writing overflow data to the output channel failed
      • append

        public void append​(CharChunk src)
                    throws java.io.IOException
        Appends the contents of the source CharChunk to this buffer.
        Parameters:
        src - the source CharChunk
        Throws:
        java.io.IOException - Writing overflow data to the output channel failed
      • append

        public void append​(char[] src,
                           int off,
                           int len)
                    throws java.io.IOException
        Add data to the buffer.
        Parameters:
        src - Char array
        off - Offset
        len - Length
        Throws:
        java.io.IOException - Writing overflow data to the output channel failed
      • append

        public void append​(java.lang.String s)
                    throws java.io.IOException
        Append a string to the buffer.
        Parameters:
        s - The string
        Throws:
        java.io.IOException - Writing overflow data to the output channel failed
      • append

        public void append​(java.lang.String s,
                           int off,
                           int len)
                    throws java.io.IOException
        Append a string to the buffer.
        Parameters:
        s - The string
        off - Offset
        len - Length
        Throws:
        java.io.IOException - Writing overflow data to the output channel failed
      • subtract

        public int subtract()
                     throws java.io.IOException
        Subtracts a single character from the buffer.
        Returns:
        the character or -1 if end of stream
        Throws:
        java.io.IOException - If an I/O error occurs
      • subtract

        public int subtract​(char[] dest,
                            int off,
                            int len)
                     throws java.io.IOException
        Subtracts characters from the buffer into the destination array.
        Parameters:
        dest - the destination array
        off - the offset in the destination array
        len - the maximum number of characters to read
        Returns:
        the number of characters read or -1 if end of stream
        Throws:
        java.io.IOException - If an I/O error occurs
      • flushBuffer

        public void flushBuffer()
                         throws java.io.IOException
        Send the buffer to the sink. Called by append() when the limit is reached. You can also call it explicitly to force the data to be written.
        Throws:
        java.io.IOException - Writing overflow data to the output channel failed
      • makeSpace

        public void makeSpace​(int count)
        Make space for len chars. If len is small, allocate a reserve space too. Never grow bigger than the limit or AbstractChunk.ARRAY_MAX_SIZE.
        Parameters:
        count - The size
      • toString

        public java.lang.String toString()
        Specified by:
        toString in interface java.lang.CharSequence
        Overrides:
        toString in class java.lang.Object
      • toStringInternal

        public java.lang.String toStringInternal()
        Returns the string representation of the chunk contents.
        Returns:
        the string representation
      • equals

        public boolean equals​(java.lang.String s)
        Compares the message bytes to the specified String object.
        Parameters:
        s - the String to compare
        Returns:
        true if the comparison succeeded, false otherwise
      • equalsIgnoreCase

        public boolean equalsIgnoreCase​(java.lang.String s)
        Compares the message bytes to the specified String object.
        Parameters:
        s - the String to compare
        Returns:
        true if the comparison succeeded, false otherwise
      • equals

        public boolean equals​(CharChunk cc)
        Compares this CharChunk to another CharChunk.
        Parameters:
        cc - the CharChunk to compare to
        Returns:
        true if the CharChunks are equal
      • equals

        public boolean equals​(char[] b2,
                              int off2,
                              int len2)
        Compares this CharChunk to a character array.
        Parameters:
        b2 - the character array to compare to
        off2 - the offset in the character array
        len2 - the length of the character array
        Returns:
        true if the contents are equal
      • startsWith

        public boolean startsWith​(java.lang.String s)
        Checks if the buffer starts with the specified string.
        Parameters:
        s - the string to check
        Returns:
        true if the buffer starts with the specified string
      • startsWithIgnoreCase

        public boolean startsWithIgnoreCase​(java.lang.String s,
                                            int pos)
        Returns true if the buffer starts with the specified string.
        Parameters:
        s - the string
        pos - The position
        Returns:
        true if the start matches
      • endsWith

        public boolean endsWith​(java.lang.String s)
        Checks if the buffer ends with the specified string.
        Parameters:
        s - the string to check
        Returns:
        true if the buffer ends with the specified string
      • getBufferElement

        protected int getBufferElement​(int index)
        Description copied from class: AbstractChunk
        Returns the element at the specified index in the buffer.
        Specified by:
        getBufferElement in class AbstractChunk
        Parameters:
        index - the element location in the buffer
        Returns:
        the element
      • indexOf

        public int indexOf​(char c)
        Returns the index of the first occurrence of the given character.
        Parameters:
        c - the character to find
        Returns:
        the index of the character, or -1 if not found
      • indexOf

        public int indexOf​(char c,
                           int starting)
        Returns the first instance of the given character in this CharChunk starting at the specified char. If the character is not found, -1 is returned.
        Parameters:
        c - The character
        starting - The start position
        Returns:
        The position of the first instance of the character or -1 if the character is not found.
      • indexOf

        public static int indexOf​(char[] chars,
                                  int start,
                                  int end,
                                  char s)
        Returns the first instance of the given character in the given char array between the specified start and end.
        Parameters:
        chars - The array to search
        start - The point to start searching from in the array
        end - The point to stop searching in the array
        s - The character to search for
        Returns:
        The position of the first instance of the character or -1 if the character is not found.
      • charAt

        public char charAt​(int index)
        Specified by:
        charAt in interface java.lang.CharSequence
      • subSequence

        public java.lang.CharSequence subSequence​(int start,
                                                  int end)
        Specified by:
        subSequence in interface java.lang.CharSequence
      • length

        public int length()
        Specified by:
        length in interface java.lang.CharSequence