Package 

Class ByteString

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

    
    public class ByteString
     implements Serializable, Comparable<ByteString>
                        

    An immutable sequence of bytes.

    Byte strings compare lexicographically as a sequence of unsigned bytes. That is, the byte string {@code ff} sorts after {@code 00}. This is counter to the sort order of the corresponding bytes, where {@code -1} sorts before {@code 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.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final static ByteString EMPTY
    • Method Summary

      Modifier and Type Method Description
      static ByteString of(Array<byte> data) Returns a new byte string containing a clone of the bytes of {@code data}.
      static ByteString of(Array<byte> data, int offset, int byteCount) Returns a new byte string containing a copy of {@code byteCount} bytes of {@code data} startingat {@code offset}.
      static ByteString of(ByteBuffer data)
      static ByteString encodeUtf8(String s) Returns a new byte string containing the {@code UTF-8} bytes of {@code s}.
      static ByteString encodeString(String s, Charset charset) Returns a new byte string containing the {@code charset}-encoded bytes of {@code s}.
      String utf8() Constructs a new {@code String} by decoding the bytes as {@code UTF-8}.
      String string(Charset charset) Constructs a new {@code String} by decoding the bytes using {@code charset}.
      String base64() Returns this byte string encoded as Base64.
      ByteString md5() Returns the 128-bit MD5 hash of this byte string.
      ByteString sha1() Returns the 160-bit SHA-1 hash of this byte string.
      ByteString sha256() Returns the 256-bit SHA-256 hash of this byte string.
      ByteString sha512() Returns the 512-bit SHA-512 hash of this byte string.
      ByteString hmacSha1(ByteString key) Returns the 160-bit SHA-1 HMAC of this byte string.
      ByteString hmacSha256(ByteString key) Returns the 256-bit SHA-256 HMAC of this byte string.
      ByteString hmacSha512(ByteString key) Returns the 512-bit SHA-512 HMAC of this byte string.
      String base64Url() Returns this byte string encoded as URL-safeBase64.
      static ByteString decodeBase64(String base64) Decodes the Base64-encoded bytes and returns their value as a byte string.
      String hex() Returns this byte string encoded in hexadecimal.
      static ByteString decodeHex(String hex) Decodes the hex-encoded bytes and returns their value a byte string.
      static ByteString read(InputStream in, int byteCount) Reads {@code count} bytes from {@code in} and returns the result.
      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'.
      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'.
      ByteString substring(int beginIndex) Returns a byte string that is a substring of this byte string, beginning at the specifiedindex 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 {@code beginIndex} and ends at the specified {@code endIndex}.
      byte getByte(int pos) Returns the byte at {@code pos}.
      int size() Returns the number of bytes in this ByteString.
      Array<byte> toByteArray() Returns a byte array containing a copy of the bytes in this {@code ByteString}.
      ByteBuffer asByteBuffer() Returns a {@code ByteBuffer} view of the bytes in this {@code ByteString}.
      void write(OutputStream out) Writes the contents of this byte string to {@code out}.
      boolean rangeEquals(int offset, ByteString other, int otherOffset, int byteCount) Returns true if the bytes of this in {@code [offset..offset+byteCount)} equal the bytes of {@code other} in {@code [otherOffset..otherOffset+byteCount)}.
      boolean rangeEquals(int offset, Array<byte> other, int otherOffset, int byteCount) Returns true if the bytes of this in {@code [offset..offset+byteCount)} equal the bytes of {@code other} in {@code [otherOffset..otherOffset+byteCount)}.
      final boolean startsWith(ByteString prefix)
      final boolean startsWith(Array<byte> prefix)
      final boolean endsWith(ByteString suffix)
      final boolean endsWith(Array<byte> suffix)
      final int indexOf(ByteString other)
      final int indexOf(ByteString other, int fromIndex)
      final int indexOf(Array<byte> other)
      int indexOf(Array<byte> other, int fromIndex)
      final int lastIndexOf(ByteString other)
      final int lastIndexOf(ByteString other, int fromIndex)
      final int lastIndexOf(Array<byte> other)
      int lastIndexOf(Array<byte> other, int fromIndex)
      boolean equals(Object o)
      int hashCode()
      int compareTo(ByteString byteString)
      String toString() Returns a human-readable string that describes the contents of this byte string.
      • Methods inherited from class java.lang.Comparable

        compareTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • of

         static ByteString of(Array<byte> data)

        Returns a new byte string containing a clone of the bytes of {@code data}.

      • of

         static ByteString of(Array<byte> data, int offset, int byteCount)

        Returns a new byte string containing a copy of {@code byteCount} bytes of {@code data} startingat {@code offset}.

      • utf8

         String utf8()

        Constructs a new {@code String} by decoding the bytes as {@code UTF-8}.

      • string

         String string(Charset charset)

        Constructs a new {@code String} by decoding the bytes using {@code charset}.

      • base64

         String base64()

        Returns this byte string encoded as Base64. In violation of theRFC, the returned string does not wrap lines at 76 columns.

      • md5

         ByteString md5()

        Returns the 128-bit MD5 hash of this byte string.

      • sha1

         ByteString sha1()

        Returns the 160-bit SHA-1 hash of this byte string.

      • sha256

         ByteString sha256()

        Returns the 256-bit SHA-256 hash of this byte string.

      • sha512

         ByteString sha512()

        Returns the 512-bit SHA-512 hash of this byte string.

      • decodeBase64

        @Nullable() static ByteString decodeBase64(String base64)

        Decodes the Base64-encoded bytes and returns their value as a byte string.Returns null if {@code base64} is not a Base64-encoded sequence of bytes.

      • hex

         String hex()

        Returns this byte string encoded in hexadecimal.

      • read

         static ByteString read(InputStream in, int byteCount)

        Reads {@code count} bytes from {@code in} and returns the result.

      • toAsciiLowercase

         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

         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

         ByteString substring(int beginIndex)

        Returns a byte string that is a substring of this byte string, beginning at the specifiedindex until the end of this string. Returns this byte string if {@code beginIndex} is 0.

      • substring

         ByteString substring(int beginIndex, int endIndex)

        Returns a byte string that is a substring of this byte string, beginning at the specified {@code beginIndex} and ends at the specified {@code endIndex}. Returns this byte string if {@code beginIndex} is 0 and {@code endIndex} is the length of this byte string.

      • getByte

         byte getByte(int pos)

        Returns the byte at {@code pos}.

      • size

         int size()

        Returns the number of bytes in this ByteString.

      • toByteArray

         Array<byte> toByteArray()

        Returns a byte array containing a copy of the bytes in this {@code ByteString}.

      • asByteBuffer

         ByteBuffer asByteBuffer()

        Returns a {@code ByteBuffer} view of the bytes in this {@code ByteString}.

      • write

         void write(OutputStream out)

        Writes the contents of this byte string to {@code out}.

      • rangeEquals

         boolean rangeEquals(int offset, ByteString other, int otherOffset, int byteCount)

        Returns true if the bytes of this in {@code [offset..offset+byteCount)} equal the bytes of {@code other} in {@code [otherOffset..otherOffset+byteCount)}. Returns false if either range isout of bounds.

      • rangeEquals

         boolean rangeEquals(int offset, Array<byte> other, int otherOffset, int byteCount)

        Returns true if the bytes of this in {@code [offset..offset+byteCount)} equal the bytes of {@code other} in {@code [otherOffset..otherOffset+byteCount)}. Returns false if either range isout of bounds.

      • toString

         String toString()

        Returns a human-readable string that describes the contents of this byte string. Typically thisis a string like {@code [text=Hello]} or {@code [hex=0000ffff]}.