Class MessageBytes

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

    public final class MessageBytes
    extends java.lang.Object
    implements java.lang.Cloneable, java.io.Serializable
    This class is used to represent a sub array of bytes in an HTTP message. It represents all request/response elements. The byte/char conversions are delayed and cached. Everything is recyclable.

    The object can represent a byte[], a char[], or a (sub) String. All operations can be made in case-sensitive mode or not.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char[] EMPTY_CHAR_ARRAY
      Empty character array.
      static int T_BYTES
      getType() is T_BYTES if the object used to create the MessageBytes was a byte[].
      static int T_CHARS
      getType() is T_CHARS if the object used to create the MessageBytes was a char[].
      static int T_NULL
      Type is NULL.
      static int T_STR
      getType() is T_STR if the object used to create the MessageBytes was a String.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object clone()  
      void duplicate​(MessageBytes src)
      Copy the src into this MessageBytes, allocating more space if needed.
      boolean equals​(java.lang.Object obj)  
      boolean equals​(java.lang.String s)
      Compares the message bytes to the specified String object.
      boolean equals​(MessageBytes mb)
      Compare this message bytes to another.
      boolean equalsIgnoreCase​(java.lang.String s)
      Compares the message bytes to the specified String object.
      ByteChunk getByteChunk()
      Returns the byte chunk, representing the byte[] and offset/length.
      CharChunk getCharChunk()
      Returns the char chunk, representing the char[] and offset/length.
      java.nio.charset.Charset getCharset()
      Return the Charset used for string<->byte conversions.
      int getLength()
      Returns the length of the original buffer.
      long getLong()
      Convert the buffer to a long, cache the value.
      java.lang.String getString()
      Returns the string value.
      int getType()
      Return the type of the original content.
      int hashCode()  
      int indexOf​(java.lang.String s)
      Find the index of a string.
      int indexOf​(java.lang.String s, int starting)
      Find the index of a string.
      int indexOfIgnoreCase​(java.lang.String s, int starting)
      Find the index of a string, ignoring case.
      boolean isNull()
      Check if the message bytes is null.
      static MessageBytes newInstance()
      Construct a new MessageBytes instance.
      void recycle()
      Resets the message bytes to an uninitialized (NULL) state.
      void setBytes​(byte[] b, int off, int len)
      Sets the content to the specified sub array of bytes.
      void setChars​(char[] c, int off, int len)
      Sets the content to be a char[]
      void setCharset​(java.nio.charset.Charset charset)
      Set the Charset used for string<->byte conversions.
      void setLong​(long l)
      Set the buffer to the representation of a long.
      void setString​(java.lang.String s)
      Set the content to be a string
      boolean startsWithIgnoreCase​(java.lang.String s, int pos)
      Check if the message bytes starts with the specified string, ignoring case.
      void toBytes()
      Convert to bytes and fill the ByteChunk with the converted value.
      void toChars()
      Convert to char[] and fill the CharChunk.
      java.lang.String toString()
      Compute the string value.
      java.lang.String toStringType()
      Convert to String (if not already of the String type) and then return the String value.
      • Methods inherited from class java.lang.Object

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

      • T_STR

        public static final int T_STR
        getType() is T_STR if the object used to create the MessageBytes was a String.
        See Also:
        Constant Field Values
      • T_BYTES

        public static final int T_BYTES
        getType() is T_BYTES if the object used to create the MessageBytes was a byte[].
        See Also:
        Constant Field Values
      • T_CHARS

        public static final int T_CHARS
        getType() is T_CHARS if the object used to create the MessageBytes was a char[].
        See Also:
        Constant Field Values
      • EMPTY_CHAR_ARRAY

        public static final char[] EMPTY_CHAR_ARRAY
        Empty character array.
    • Method Detail

      • newInstance

        public static MessageBytes newInstance()
        Construct a new MessageBytes instance.
        Returns:
        the instance
      • clone

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

        public boolean isNull()
        Check if the message bytes is null.
        Returns:
        true if null
      • recycle

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

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

        public void setChars​(char[] c,
                             int off,
                             int len)
        Sets the content to be a char[]
        Parameters:
        c - the chars
        off - the start offset of the chars
        len - the length of the chars
      • setString

        public void setString​(java.lang.String s)
        Set the content to be a string
        Parameters:
        s - The string
      • toString

        public java.lang.String toString()
        Compute the string value.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the string
      • toStringType

        public java.lang.String toStringType()
        Convert to String (if not already of the String type) and then return the String value.
        Returns:
        The current value as a String
      • getType

        public int getType()
        Return the type of the original content. Can be T_STR, T_BYTES, T_CHARS or T_NULL
        Returns:
        the type
      • getByteChunk

        public ByteChunk getByteChunk()
        Returns the byte chunk, representing the byte[] and offset/length. Valid only if T_BYTES or after a conversion was made.
        Returns:
        the byte chunk
      • getCharChunk

        public CharChunk getCharChunk()
        Returns the char chunk, representing the char[] and offset/length. Valid only if T_CHARS or after a conversion was made.
        Returns:
        the char chunk
      • getString

        public java.lang.String getString()
        Returns the string value. Valid only if T_STR or after a conversion was made.
        Returns:
        the string
      • getCharset

        public java.nio.charset.Charset getCharset()
        Return the Charset used for string<->byte conversions.
        Returns:
        the Charset used for string<->byte conversions
      • setCharset

        public void setCharset​(java.nio.charset.Charset charset)
        Set the Charset used for string<->byte conversions.
        Parameters:
        charset - The charset
      • toBytes

        public void toBytes()
        Convert to bytes and fill the ByteChunk with the converted value.
      • toChars

        public void toChars()
        Convert to char[] and fill the CharChunk.

        Note: The conversion from bytes is not optimised - it converts to String first. However, Tomcat doesn't call this method to convert from bytes so there is no benefit from optimising that path.

      • getLength

        public int getLength()
        Returns the length of the original buffer.

        Note: The length in bytes may be different from the length in chars.

        Returns:
        the length
      • 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​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • equals

        public boolean equals​(MessageBytes mb)
        Compare this message bytes to another.
        Parameters:
        mb - the other message bytes
        Returns:
        true if equal
      • startsWithIgnoreCase

        public boolean startsWithIgnoreCase​(java.lang.String s,
                                            int pos)
        Check if the message bytes starts with the specified string, ignoring case.
        Parameters:
        s - the string
        pos - The start position
        Returns:
        true if the message bytes starts with the specified string
      • hashCode

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

        public int indexOf​(java.lang.String s,
                           int starting)
        Find the index of a string.
        Parameters:
        s - the string
        starting - the starting position
        Returns:
        the index
      • indexOf

        public int indexOf​(java.lang.String s)
        Find the index of a string.
        Parameters:
        s - the string
        Returns:
        the index
      • indexOfIgnoreCase

        public int indexOfIgnoreCase​(java.lang.String s,
                                     int starting)
        Find the index of a string, ignoring case.
        Parameters:
        s - the string
        starting - the starting position
        Returns:
        the index
      • duplicate

        public void duplicate​(MessageBytes src)
                       throws java.io.IOException
        Copy the src into this MessageBytes, allocating more space if needed.
        Parameters:
        src - The source
        Throws:
        java.io.IOException - Writing overflow data to the output channel failed
      • setLong

        public void setLong​(long l)
        Set the buffer to the representation of a long.
        Parameters:
        l - The long
      • getLong

        public long getLong()
        Convert the buffer to a long, cache the value. Used for headers conversion.
        Returns:
        the long value