Class ByteChunk

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

    public final class ByteChunk
    extends java.lang.Object
    implements Chunk, java.lang.Cloneable, java.io.Serializable
    This class is used to represent a chunk of bytes, and utilities to manipulate byte[]. The buffer can be modified and used for both input and output.
    Author:
    dac@sun.com, James Todd [gonzo@sun.com], Costin Manolache, Remy Maucherat
    See Also:
    Serialized Form
    • Constructor Summary

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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void allocate​(int initial, int limit)  
      void append​(byte b)  
      void append​(byte[] src, int off, int len)
      Add data to the buffer
      void append​(char c)  
      void append​(ByteChunk src)  
      static byte[] convertToBytes​(java.lang.String value)
      Convert specified String to a byte array.
      void delete​(int start, int end)  
      boolean equals​(byte[] bytes)
      Compares the message bytes to the specified byte array.
      boolean equals​(byte[] b2, int off2, int len2)  
      static boolean equals​(byte[] b1, int b1Offs, int b1Len, byte[] b2, int b2Offs, int b2Len)  
      static boolean equals​(byte[] b, int offs, int len, java.lang.String s)  
      boolean equals​(char[] c2, int off2, int len2)  
      boolean equals​(java.lang.Object o)  
      boolean equals​(java.lang.String s)
      Compares the message bytes to the specified String object.
      boolean equals​(ByteChunk bb)  
      boolean equals​(CharChunk cc)  
      boolean equalsIgnoreCase​(byte[] b)  
      boolean equalsIgnoreCase​(byte[] b, int offset, int len)  
      static boolean equalsIgnoreCase​(byte[] b1, int b1Offs, int b1Len, byte[] b2, int b2Offs, int b2Len)  
      static boolean equalsIgnoreCase​(byte[] b, int offset, int len, java.lang.String s)
      Compares the message bytes to the specified String object.
      boolean equalsIgnoreCase​(java.lang.String s)
      Compares the message bytes to the specified String object.
      boolean equalsIgnoreCaseLowerCase​(byte[] cmpTo)  
      static boolean equalsIgnoreCaseLowerCase​(byte[] buffer, int start, int end, byte[] cmpTo)
      Compares the buffer chunk to the specified byte array representing lower-case ASCII characters.
      static int findChar​(byte[] buf, int start, int end, char c)
      Find a character, no side effects.
      static int findChars​(byte[] buf, int start, int end, byte[] c)
      Find a character, no side effects.
      static int findNotChars​(byte[] buf, int start, int end, byte[] c)
      Find the first character !
      void flushBuffer()  
      byte[] getBuffer()
      Returns the message bytes.
      byte[] getBytes()
      Returns the message bytes.
      java.nio.charset.Charset getCharset()  
      ByteChunk getClone()  
      int getEnd()  
      int getInt()  
      int getLength()
      Returns the length of the bytes.
      int getLimit()  
      long getLong()  
      int getOffset()  
      int getStart()
      Returns the start offset of the bytes.
      int hash()  
      int hashCode()  
      int hashIgnoreCase()  
      static int indexOf​(byte[] bytes, int off, int end, 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 fromIdx)  
      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 buff to an uninitialized state.
      void recycleAndReset()  
      void reset()  
      protected void resetStringCache()  
      void setByteInputChannel​(ByteChunk.ByteInputChannel in)
      When the buffer is empty, read the data from the input channel.
      void setByteOutputChannel​(ByteChunk.ByteOutputChannel out)
      When the buffer is full, write the data to the output channel.
      void setBytes​(byte[] b, int off, int len)
      Sets the message bytes to the specified sub-array of bytes.
      void setCharset​(java.nio.charset.Charset charset)  
      void setEnd​(int i)  
      void setLimit​(int limit)
      Maximum amount of data in this buffer.
      void setOffset​(int off)  
      void setOptimizedWrite​(boolean optimizedWrite)  
      void setStart​(int start)  
      boolean startsWith​(byte[] b2)  
      static boolean startsWith​(byte[] buffer, int start, int end, byte[] cmpTo)  
      boolean startsWith​(java.lang.String s)
      Returns true if the message bytes starts with the specified string.
      boolean startsWith​(java.lang.String s, int offset)
      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​(byte[] src, int off, int len)  
      int substract​(ByteChunk src)  
      java.lang.String toString()  
      java.lang.String toString​(int start, int end)  
      java.lang.String toString​(java.nio.charset.Charset charset)  
      java.lang.String toStringInternal()  
      void trimLeft()  
      • Methods inherited from class java.lang.Object

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

      • ByteChunk

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

        public ByteChunk​(int initial)
    • Method Detail

      • isNull

        public boolean isNull()
      • recycle

        public void recycle()
        Resets the message buff to an uninitialized state.
      • recycleAndReset

        public void recycleAndReset()
      • reset

        public void reset()
      • resetStringCache

        protected void resetStringCache()
      • allocate

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

        public void setBytes​(byte[] b,
                             int off,
                             int len)
        Sets the message bytes to the specified sub-array of bytes.
        Parameters:
        b - the ascii bytes
        off - the start offset of the bytes
        len - the length of the bytes
      • setOptimizedWrite

        public void setOptimizedWrite​(boolean optimizedWrite)
      • getCharset

        public java.nio.charset.Charset getCharset()
      • setCharset

        public void setCharset​(java.nio.charset.Charset charset)
      • getBytes

        public byte[] getBytes()
        Returns the message bytes.
      • getBuffer

        public byte[] getBuffer()
        Returns the message bytes.
      • 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
      • getOffset

        public int getOffset()
      • setStart

        public void setStart​(int start)
        Specified by:
        setStart in interface Chunk
      • setOffset

        public void setOffset​(int off)
      • getLength

        public int getLength()
        Returns the length of the bytes. XXX need to clean this up
        Specified by:
        getLength in interface Chunk
      • 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()
      • setByteInputChannel

        public void setByteInputChannel​(ByteChunk.ByteInputChannel in)
        When the buffer is empty, read the data from the input channel.
      • setByteOutputChannel

        public void setByteOutputChannel​(ByteChunk.ByteOutputChannel 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.
      • getEnd

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

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

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

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

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

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

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

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

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

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

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

        public int substract​(byte[] 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
      • trimLeft

        public void trimLeft()
      • 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
      • toString

        public java.lang.String toString​(java.nio.charset.Charset charset)
      • toStringInternal

        public java.lang.String toStringInternal()
      • getInt

        public int getInt()
      • getLong

        public long getLong()
      • 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.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​(byte[] bytes)
        Compares the message bytes to the specified byte array.
        Parameters:
        bytes - the byte[] to compare
        Returns:
        true if the comparison succeeded, false otherwise
        Since:
        2.3
      • 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
      • equalsIgnoreCase

        public boolean equalsIgnoreCase​(byte[] b)
      • equalsIgnoreCase

        public boolean equalsIgnoreCase​(byte[] b,
                                        int offset,
                                        int len)
      • equalsIgnoreCaseLowerCase

        public boolean equalsIgnoreCaseLowerCase​(byte[] cmpTo)
      • equals

        public boolean equals​(ByteChunk bb)
      • equals

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

        public boolean equals​(CharChunk cc)
      • equals

        public boolean equals​(char[] c2,
                              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
      • startsWith

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

        public boolean startsWith​(byte[] b2)
      • 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
        pos - The position
      • indexOf

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

        public int hash()
      • hashIgnoreCase

        public int hashIgnoreCase()
      • equals

        public static boolean equals​(byte[] b1,
                                     int b1Offs,
                                     int b1Len,
                                     byte[] b2,
                                     int b2Offs,
                                     int b2Len)
      • equals

        public static boolean equals​(byte[] b,
                                     int offs,
                                     int len,
                                     java.lang.String s)
      • equalsIgnoreCase

        public static boolean equalsIgnoreCase​(byte[] b1,
                                               int b1Offs,
                                               int b1Len,
                                               byte[] b2,
                                               int b2Offs,
                                               int b2Len)
      • equalsIgnoreCase

        public static boolean equalsIgnoreCase​(byte[] b,
                                               int offset,
                                               int len,
                                               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
      • equalsIgnoreCaseLowerCase

        public static boolean equalsIgnoreCaseLowerCase​(byte[] buffer,
                                                        int start,
                                                        int end,
                                                        byte[] cmpTo)
        Compares the buffer chunk to the specified byte array representing lower-case ASCII characters.
        Parameters:
        buffer - the byte[] to compare
        start - buffer start
        end - buffer end
        cmpTo - byte[] to compare against
        Returns:
        true if the comparison succeeded, false otherwise
        Since:
        2.3
      • startsWith

        public static boolean startsWith​(byte[] buffer,
                                         int start,
                                         int end,
                                         byte[] cmpTo)
      • 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
        starting - The start position
      • indexOf

        public static int indexOf​(byte[] bytes,
                                  int off,
                                  int end,
                                  char qq)
      • findChar

        public static int findChar​(byte[] buf,
                                   int start,
                                   int end,
                                   char c)
        Find a character, no side effects.
        Returns:
        index of char if found, -1 if not
      • findChars

        public static int findChars​(byte[] buf,
                                    int start,
                                    int end,
                                    byte[] c)
        Find a character, no side effects.
        Returns:
        index of char if found, -1 if not
      • findNotChars

        public static int findNotChars​(byte[] buf,
                                       int start,
                                       int end,
                                       byte[] c)
        Find the first character != c
        Returns:
        index of char if found, -1 if not
      • convertToBytes

        public static byte[] convertToBytes​(java.lang.String value)
        Convert specified String to a byte array.
        Parameters:
        value - to convert to byte array
        Returns:
        the byte array value