Class HttpString

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<HttpString>

    public final class HttpString
    extends java.lang.Object
    implements java.lang.Comparable<HttpString>, java.io.Serializable
    An HTTP case-insensitive Latin-1 string.
    Author:
    David M. Lloyd
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static HttpString EMPTY
      Empty HttpString instance.
    • Constructor Summary

      Constructors 
      Constructor Description
      HttpString​(byte[] bytes)
      Construct a new instance.
      HttpString​(byte[] bytes, int offset, int length)
      Construct a new instance.
      HttpString​(java.lang.String string)
      Construct a new instance from a String.
      HttpString​(java.nio.ByteBuffer buffer)
      Construct a new instance by reading the remaining bytes from a buffer.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void appendTo​(java.nio.ByteBuffer buffer)
      Append to a byte buffer.
      byte byteAt​(int idx)
      Get the byte at an index.
      int compareTo​(HttpString other)
      Compare this string to another in a case-insensitive manner.
      void copyTo​(byte[] dst, int offs)
      Copy all the bytes from this string to the given array at the given offset.
      void copyTo​(byte[] dst, int offs, int len)
      Copy len bytes from this string to the given array at the given offset.
      void copyTo​(int srcOffs, byte[] dst, int offs, int len)
      Copy len bytes from this string at offset srcOffs to the given array at the given offset.
      boolean equals​(HttpString other)
      Determine if this HttpString is equal to another.
      boolean equals​(java.lang.Object other)
      Determine if this HttpString is equal to another.
      boolean equalToString​(java.lang.String headerName)  
      int hashCode()
      Get the hash code.
      int length()
      Get the string length.
      java.lang.String toString()
      Get the String representation of this HttpString.
      static HttpString tryFromString​(java.lang.String string)
      Attempt to convert a String to an HttpString.
      void writeTo​(java.io.OutputStream output)
      Append to an output stream.
      • Methods inherited from class java.lang.Object

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

      • EMPTY

        public static final HttpString EMPTY
        Empty HttpString instance.
    • Constructor Detail

      • HttpString

        public HttpString​(byte[] bytes)
        Construct a new instance.
        Parameters:
        bytes - the byte array to copy
      • HttpString

        public HttpString​(byte[] bytes,
                          int offset,
                          int length)
        Construct a new instance.
        Parameters:
        bytes - the byte array to copy
        offset - the offset into the array to start copying
        length - the number of bytes to copy
      • HttpString

        public HttpString​(java.nio.ByteBuffer buffer)
        Construct a new instance by reading the remaining bytes from a buffer.
        Parameters:
        buffer - the buffer to read
      • HttpString

        public HttpString​(java.lang.String string)
        Construct a new instance from a String. The String will be used as the cached toString() value for this HttpString.
        Parameters:
        string - the source string
    • Method Detail

      • tryFromString

        public static HttpString tryFromString​(java.lang.String string)
        Attempt to convert a String to an HttpString. If the string cannot be converted, null is returned.
        Parameters:
        string - the string to try
        Returns:
        the HTTP string, or null if the string is not in a compatible encoding
      • length

        public int length()
        Get the string length.
        Returns:
        the string length
      • byteAt

        public byte byteAt​(int idx)
        Get the byte at an index.
        Returns:
        the byte at an index
      • copyTo

        public void copyTo​(int srcOffs,
                           byte[] dst,
                           int offs,
                           int len)
        Copy len bytes from this string at offset srcOffs to the given array at the given offset.
        Parameters:
        srcOffs - the source offset
        dst - the destination
        offs - the destination offset
        len - the number of bytes to copy
      • copyTo

        public void copyTo​(byte[] dst,
                           int offs,
                           int len)
        Copy len bytes from this string to the given array at the given offset.
        Parameters:
        dst - the destination
        offs - the destination offset
        len - the number of bytes
      • copyTo

        public void copyTo​(byte[] dst,
                           int offs)
        Copy all the bytes from this string to the given array at the given offset.
        Parameters:
        dst - the destination
        offs - the destination offset
      • appendTo

        public void appendTo​(java.nio.ByteBuffer buffer)
        Append to a byte buffer.
        Parameters:
        buffer - the buffer to append to
      • writeTo

        public void writeTo​(java.io.OutputStream output)
                     throws java.io.IOException
        Append to an output stream.
        Parameters:
        output - the stream to write to
        Throws:
        java.io.IOException - if an error occurs
      • compareTo

        public int compareTo​(HttpString other)
        Compare this string to another in a case-insensitive manner.
        Specified by:
        compareTo in interface java.lang.Comparable<HttpString>
        Parameters:
        other - the other string
        Returns:
        -1, 0, or 1
      • hashCode

        public int hashCode()
        Get the hash code.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code
      • equals

        public boolean equals​(java.lang.Object other)
        Determine if this HttpString is equal to another.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        other - the other object
        Returns:
        true if they are equal, false otherwise
      • equals

        public boolean equals​(HttpString other)
        Determine if this HttpString is equal to another.
        Parameters:
        other - the other object
        Returns:
        true if they are equal, false otherwise
      • toString

        public java.lang.String toString()
        Get the String representation of this HttpString.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the string
      • equalToString

        public boolean equalToString​(java.lang.String headerName)