Class CharChunk

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

    public final class CharChunk
    extends java.lang.Object
    implements Chunk, java.lang.Cloneable, java.io.Serializable
    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.
    Author:
    dac@sun.com, James Todd [gonzo@sun.com], Costin Manolache, Remy Maucherat
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.nio.charset.Charset DEFAULT_HTTP_CHARSET
      Default encoding used to convert to strings.
    • Constructor Summary

      Constructors 
      Constructor Description
      CharChunk()
      Creates a new, uninitialized CharChunk object.
      CharChunk​(int size)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void allocate​(int initial, int limit)  
      void append​(char b)  
      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.StringBuffer sb)
      Add data to the buffer
      void append​(java.lang.String s, int off, int len)
      Append a string to the buffer
      void append​(CharChunk src)  
      void delete​(int start, int end)  
      boolean endsWith​(java.lang.String s)  
      void ensureCapacity​(int size)  
      boolean equals​(byte[] b)
      Compares the message bytes to the specified byte array representing ASCII characters.
      boolean equals​(byte[] b2, int off2, int len2)  
      boolean equals​(char[] b2, int off2, int len2)  
      boolean equals​(java.lang.CharSequence s)
      Compares the message bytes to the specified String object.
      boolean equals​(java.lang.Object o)  
      boolean equals​(CharChunk cc)  
      boolean equalsIgnoreCase​(byte[] b)
      Compares the message bytes to the specified byte array representing ASCII characters.
      boolean equalsIgnoreCase​(byte[] b, int offset, int len)
      Compares the message bytes to the specified byte array representing ASCII characters.
      boolean equalsIgnoreCase​(char[] b, int offset, int len)
      Compares the message bytes to the specified char array representing ASCII characters.
      boolean equalsIgnoreCase​(java.lang.CharSequence s)
      Compares the message bytes to the specified String object.
      boolean equalsIgnoreCaseLowerCase​(byte[] b)
      Compares the char chunk to the specified byte array representing lower-case ASCII characters.
      void flushBuffer()  
      char[] getBuffer()  
      char[] getChars()  
      CharChunk getClone()  
      int getEnd()  
      int getInt()  
      int getLength()
      Returns the length of the bytes.
      int getLimit()  
      int getStart()
      Returns the start offset of the bytes.
      int hash()  
      int hashCode()  
      int hashIgnoreCase()  
      int indexOf​(char c)  
      static int indexOf​(char[] chars, int off, int cend, char qq)  
      int indexOf​(char c, int starting)
      Returns true if the message bytes starts with the specified string.
      int indexOf​(java.lang.String s, int fromIndex)  
      int indexOf​(java.lang.String src, int srcOff, int srcLen, int myOff)  
      boolean isNull()  
      protected void notifyDirectUpdate()
      Notify the Chunk that its content is going to be changed directly
      void recycle()
      Resets the message bytes to an uninitialized state.
      void reset()  
      protected void resetStringCache()  
      void set​(BufferChunk bufferChunk, java.nio.charset.Charset encoding)
      Set BufferChunk content to CharChunk using given Charset.
      void set​(ByteChunk byteChunk, java.nio.charset.Charset encoding)
      Set ByteChunk content to CharChunk using given Charset.
      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)  
      void setEnd​(int i)  
      void setLimit​(int limit)
      Maximum amount of data in this buffer.
      void setOptimizedWrite​(boolean optimizedWrite)  
      void setStart​(int start)
      Returns the start offset of the bytes.
      boolean startsWith​(java.lang.String s)
      Returns true if the message bytes starts with the specified string.
      boolean startsWithIgnoreCase​(java.lang.String s, int pos)
      Returns true if the message bytes starts with the specified string.
      int substract()  
      int substract​(char[] src, int off, int len)  
      int substract​(CharChunk src)  
      java.lang.String toString()  
      java.lang.String toString​(int start, int end)  
      java.lang.String toStringInternal()  
      void trimLeft()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DEFAULT_HTTP_CHARSET

        public static final java.nio.charset.Charset DEFAULT_HTTP_CHARSET
        Default encoding used to convert to strings. It should be UTF8, as most standards seem to converge, but the servlet API requires 8859_1, and this object is used mostly for servlets.
    • Constructor Detail

      • CharChunk

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

        public CharChunk​(int size)
    • Method Detail

      • isNull

        public boolean isNull()
      • recycle

        public void recycle()
        Resets the message bytes to an uninitialized state.
      • reset

        public void reset()
      • allocate

        public void allocate​(int initial,
                             int limit)
      • ensureCapacity

        public void ensureCapacity​(int size)
      • setOptimizedWrite

        public void setOptimizedWrite​(boolean optimizedWrite)
      • setChars

        public void setChars​(char[] c,
                             int off,
                             int len)
      • setLimit

        public void setLimit​(int limit)
        Maximum amount of data in this buffer. If -1 or not set, the buffer will grow indefinitely. Can be smaller than the current buffer size ( which will not shrink ). When the limit is reached, the buffer will be flushed ( if out is set ) or throw exception.
      • getLimit

        public int getLimit()
      • setCharInputChannel

        public void setCharInputChannel​(CharChunk.CharInputChannel in)
        When the buffer is empty, read the data from 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.
      • getChars

        public char[] getChars()
      • getBuffer

        public char[] getBuffer()
      • getStart

        public int getStart()
        Returns the start offset of the bytes. For output this is the end of the buffer.
        Specified by:
        getStart in interface Chunk
      • setStart

        public void setStart​(int start)
        Returns the start offset of the bytes.
        Specified by:
        setStart in interface Chunk
      • getLength

        public int getLength()
        Returns the length of the bytes.
        Specified by:
        getLength in interface Chunk
      • getEnd

        public int getEnd()
        Specified by:
        getEnd in interface Chunk
      • setEnd

        public void setEnd​(int i)
        Specified by:
        setEnd in interface Chunk
      • append

        public void append​(char b)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • append

        public void append​(CharChunk src)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • append

        public void append​(char[] src,
                           int off,
                           int len)
                    throws java.io.IOException
        Add data to the buffer
        Throws:
        java.io.IOException
      • append

        public void append​(java.lang.StringBuffer sb)
                    throws java.io.IOException
        Add data to the buffer
        Throws:
        java.io.IOException
      • append

        public void append​(java.lang.String s)
                    throws java.io.IOException
        Append a string to the buffer
        Throws:
        java.io.IOException
      • append

        public void append​(java.lang.String s,
                           int off,
                           int len)
                    throws java.io.IOException
        Append a string to the buffer
        Throws:
        java.io.IOException
      • delete

        public void delete​(int start,
                           int end)
        Specified by:
        delete in interface Chunk
      • substract

        public int substract()
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • substract

        public int substract​(CharChunk src)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • substract

        public int substract​(char[] src,
                             int off,
                             int len)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • flushBuffer

        public void flushBuffer()
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • notifyDirectUpdate

        protected void notifyDirectUpdate()
        Notify the Chunk that its content is going to be changed directly
      • resetStringCache

        protected void resetStringCache()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toString

        public java.lang.String toString​(int start,
                                         int end)
        Specified by:
        toString in interface Chunk
      • toStringInternal

        public java.lang.String toStringInternal()
      • getInt

        public int getInt()
      • set

        public void set​(ByteChunk byteChunk,
                        java.nio.charset.Charset encoding)
                 throws java.io.CharConversionException
        Set ByteChunk content to CharChunk using given Charset.
        Throws:
        java.io.CharConversionException
      • set

        public void set​(BufferChunk bufferChunk,
                        java.nio.charset.Charset encoding)
                 throws java.io.CharConversionException
        Set BufferChunk content to CharChunk using given Charset.
        Throws:
        java.io.CharConversionException
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • equals

        public boolean equals​(java.lang.CharSequence 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​(byte[] b)
        Compares the message bytes to the specified byte array representing ASCII characters.
        Parameters:
        b - the byte[] to compare
        Returns:
        true if the comparison succeeded, false otherwise
        Since:
        2.3
      • equalsIgnoreCase

        public boolean equalsIgnoreCase​(java.lang.CharSequence 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​(byte[] b)
        Compares the message bytes to the specified byte array representing ASCII characters.
        Parameters:
        b - the byte[] to compare
        Returns:
        true if the comparison succeeded, false otherwise
        Since:
        2.1.2
      • equalsIgnoreCase

        public boolean equalsIgnoreCase​(byte[] b,
                                        int offset,
                                        int len)
        Compares the message bytes to the specified byte array representing ASCII characters.
        Parameters:
        b - the byte[] to compare
        Returns:
        true if the comparison succeeded, false otherwise
        Since:
        2.3
      • equalsIgnoreCase

        public boolean equalsIgnoreCase​(char[] b,
                                        int offset,
                                        int len)
        Compares the message bytes to the specified char array representing ASCII characters.
        Parameters:
        b - the char[] to compare
        Returns:
        true if the comparison succeeded, false otherwise
        Since:
        2.3
      • equalsIgnoreCaseLowerCase

        public boolean equalsIgnoreCaseLowerCase​(byte[] b)
        Compares the char chunk to the specified byte array representing lower-case ASCII characters.
        Parameters:
        b - the byte[] to compare
        Returns:
        true if the comparison succeeded, false otherwise
        Since:
        2.1.2
      • equals

        public boolean equals​(CharChunk cc)
      • equals

        public boolean equals​(char[] b2,
                              int off2,
                              int len2)
      • equals

        public boolean equals​(byte[] b2,
                              int off2,
                              int len2)
      • startsWith

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

        public boolean startsWithIgnoreCase​(java.lang.String s,
                                            int pos)
        Returns true if the message bytes starts with the specified string.
        Parameters:
        s - the string
      • endsWith

        public boolean endsWith​(java.lang.String s)
      • hash

        public int hash()
      • hashIgnoreCase

        public int hashIgnoreCase()
      • indexOf

        public int indexOf​(char c)
      • indexOf

        public int indexOf​(char c,
                           int starting)
        Returns true if the message bytes starts with the specified string.
        Specified by:
        indexOf in interface Chunk
        Parameters:
        c - the character
      • indexOf

        public static int indexOf​(char[] chars,
                                  int off,
                                  int cend,
                                  char qq)
      • indexOf

        public int indexOf​(java.lang.String s,
                           int fromIndex)
        Specified by:
        indexOf in interface Chunk
      • indexOf

        public int indexOf​(java.lang.String src,
                           int srcOff,
                           int srcLen,
                           int myOff)
      • trimLeft

        public void trimLeft()