Package okio

Class ByteString

  • All Implemented Interfaces:
    Serializable, Comparable<ByteString>

    public class ByteString
    extends Object
    implements Serializable, Comparable<ByteString>
    An immutable sequence of bytes.

    Byte strings compare lexicographically as a sequence of unsigned bytes. That is, the byte string ff sorts after 00. This is counter to the sort order of the corresponding bytes, where -1 sorts before 0.

    Full disclosure: this class provides untrusted input and output streams with raw access to the underlying byte array. A hostile stream implementation could keep a reference to the mutable byte string, violating the immutable guarantee of this class. For this reason a byte string's immutability guarantee cannot be relied upon for security in applets and other environments that run both trusted and untrusted code in the same process.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static ByteString EMPTY
      A singleton empty ByteString.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      String base64()
      Returns this byte string encoded as Base64.
      String base64Url()
      Returns this byte string encoded as URL-safe Base64.
      int compareTo​(ByteString byteString)
      Compares this object to the specified object to determine their relative order.
      static ByteString decodeBase64​(String base64)
      Decodes the Base64-encoded bytes and returns their value as a byte string.
      static ByteString decodeHex​(String hex)
      Decodes the hex-encoded bytes and returns their value a byte string.
      static ByteString encodeUtf8​(String s)
      Returns a new byte string containing the UTF-8 bytes of s.
      boolean equals​(Object o)
      Compares this instance with the specified object and indicates if they are equal.
      byte getByte​(int pos)
      Returns the byte at pos.
      int hashCode()
      Returns an integer hash code for this object.
      String hex()
      Returns this byte string encoded in hexadecimal.
      ByteString md5()
      Returns the MD5 hash of this byte string.
      static ByteString of​(byte... data)
      Returns a new byte string containing a clone of the bytes of data.
      static ByteString of​(byte[] data, int offset, int byteCount)
      Returns a new byte string containing a copy of byteCount bytes of data starting at offset.
      boolean rangeEquals​(int offset, byte[] other, int otherOffset, int byteCount)
      Returns true if the bytes of this in [offset..offset+byteCount) equal the bytes of other in [otherOffset..otherOffset+byteCount).
      boolean rangeEquals​(int offset, ByteString other, int otherOffset, int byteCount)
      Returns true if the bytes of this in [offset..offset+byteCount) equal the bytes of other in [otherOffset..otherOffset+byteCount).
      static ByteString read​(InputStream in, int byteCount)
      Reads count bytes from in and returns the result.
      ByteString sha256()
      Returns the SHA-256 hash of this byte string.
      int size()
      Returns the number of bytes in this ByteString.
      ByteString substring​(int beginIndex)
      Returns a byte string that is a substring of this byte string, beginning at the specified index until the end of this string.
      ByteString substring​(int beginIndex, int endIndex)
      Returns a byte string that is a substring of this byte string, beginning at the specified beginIndex and ends at the specified endIndex.
      ByteString toAsciiLowercase()
      Returns a byte string equal to this byte string, but with the bytes 'A' through 'Z' replaced with the corresponding byte in 'a' through 'z'.
      ByteString toAsciiUppercase()
      Returns a byte string equal to this byte string, but with the bytes 'a' through 'z' replaced with the corresponding byte in 'A' through 'Z'.
      byte[] toByteArray()
      Returns a byte array containing a copy of the bytes in this ByteString.
      String toString()
      Returns a string containing a concise, human-readable description of this object.
      String utf8()
      Constructs a new String by decoding the bytes as UTF-8.
      void write​(OutputStream out)
      Writes the contents of this byte string to out.
    • Field Detail

      • EMPTY

        public static final ByteString EMPTY
        A singleton empty ByteString.
    • Method Detail

      • of

        public static ByteString of​(byte... data)
        Returns a new byte string containing a clone of the bytes of data.
      • of

        public static ByteString of​(byte[] data,
                                    int offset,
                                    int byteCount)
        Returns a new byte string containing a copy of byteCount bytes of data starting at offset.
      • encodeUtf8

        public static ByteString encodeUtf8​(String s)
        Returns a new byte string containing the UTF-8 bytes of s.
      • utf8

        public String utf8()
        Constructs a new String by decoding the bytes as UTF-8.
      • base64

        public String base64()
        Returns this byte string encoded as Base64. In violation of the RFC, the returned string does not wrap lines at 76 columns.
      • md5

        public ByteString md5()
        Returns the MD5 hash of this byte string.
      • sha256

        public ByteString sha256()
        Returns the SHA-256 hash of this byte string.
      • decodeBase64

        public static ByteString decodeBase64​(String base64)
        Decodes the Base64-encoded bytes and returns their value as a byte string. Returns null if base64 is not a Base64-encoded sequence of bytes.
      • hex

        public String hex()
        Returns this byte string encoded in hexadecimal.
      • decodeHex

        public static ByteString decodeHex​(String hex)
        Decodes the hex-encoded bytes and returns their value a byte string.
      • toAsciiLowercase

        public ByteString toAsciiLowercase()
        Returns a byte string equal to this byte string, but with the bytes 'A' through 'Z' replaced with the corresponding byte in 'a' through 'z'. Returns this byte string if it contains no bytes in 'A' through 'Z'.
      • toAsciiUppercase

        public ByteString toAsciiUppercase()
        Returns a byte string equal to this byte string, but with the bytes 'a' through 'z' replaced with the corresponding byte in 'A' through 'Z'. Returns this byte string if it contains no bytes in 'a' through 'z'.
      • substring

        public ByteString substring​(int beginIndex)
        Returns a byte string that is a substring of this byte string, beginning at the specified index until the end of this string. Returns this byte string if beginIndex is 0.
      • substring

        public ByteString substring​(int beginIndex,
                                    int endIndex)
        Returns a byte string that is a substring of this byte string, beginning at the specified beginIndex and ends at the specified endIndex. Returns this byte string if beginIndex is 0 and endIndex is the length of this byte string.
      • getByte

        public byte getByte​(int pos)
        Returns the byte at pos.
      • size

        public int size()
        Returns the number of bytes in this ByteString.
      • toByteArray

        public byte[] toByteArray()
        Returns a byte array containing a copy of the bytes in this ByteString.
      • rangeEquals

        public boolean rangeEquals​(int offset,
                                   ByteString other,
                                   int otherOffset,
                                   int byteCount)
        Returns true if the bytes of this in [offset..offset+byteCount) equal the bytes of other in [otherOffset..otherOffset+byteCount). Returns false if either range is out of bounds.
      • rangeEquals

        public boolean rangeEquals​(int offset,
                                   byte[] other,
                                   int otherOffset,
                                   int byteCount)
        Returns true if the bytes of this in [offset..offset+byteCount) equal the bytes of other in [otherOffset..otherOffset+byteCount). Returns false if either range is out of bounds.
      • equals

        public boolean equals​(Object o)
        Description copied from class: Object
        Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

        The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

        The general contract for the equals and Object.hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

        Overrides:
        equals in class Object
        Parameters:
        o - the object to compare this instance with.
        Returns:
        true if the specified object is equal to this Object; false otherwise.
        See Also:
        Object.hashCode()
      • hashCode

        public int hashCode()
        Description copied from class: Object
        Returns an integer hash code for this object. By contract, any two objects for which Object.equals(java.lang.Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

        Note that hash values must not change over time unless information used in equals comparisons also changes.

        See Writing a correct hashCode method if you intend implementing your own hashCode method.

        Overrides:
        hashCode in class Object
        Returns:
        this object's hash code.
        See Also:
        Object.equals(java.lang.Object)
      • compareTo

        public int compareTo​(ByteString byteString)
        Description copied from interface: Comparable
        Compares this object to the specified object to determine their relative order.
        Specified by:
        compareTo in interface Comparable<ByteString>
        Parameters:
        byteString - the object to compare to this instance.
        Returns:
        a negative integer if this instance is less than another; a positive integer if this instance is greater than another; 0 if this instance has the same order as another.
      • toString

        public String toString()
        Description copied from class: Object
        Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
           getClass().getName() + '@' + Integer.toHexString(hashCode())

        See Writing a useful toString method if you intend implementing your own toString method.

        Overrides:
        toString in class Object
        Returns:
        a printable representation of this object.