Class ByteString

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] bytes  
      private static char[] DIGITS  
      static ByteString EMPTY
      An empty byte string.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        ByteString​(byte[] bytes)
      Creates a ByteString.
      private ByteString​(byte[] bytes, boolean dummy)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte byteAt​(int i)
      Returns the byte at a given position in the byte string.
      java.lang.Object clone()  
      int compareTo​(ByteString that)  
      ByteString concat​(ByteString other)
      Returns a ByteString consisting of the concatenation of this and another string.
      private static byte decodeHex​(char c)  
      boolean equals​(java.lang.Object obj)  
      byte[] getBytes()
      Returns a copy of the byte array.
      int hashCode()  
      int indexOf​(ByteString seek)
      Returns the position at which seek first occurs in this byte string, or -1 if it does not occur.
      int indexOf​(ByteString seek, int start)
      Returns the position at which seek first occurs in this byte string, starting at the specified index, or -1 if it does not occur.
      int length()
      Returns the number of bytes in this byte string.
      static ByteString of​(java.lang.String string, int base)
      Creates a byte string from a hexadecimal or binary string.
      static ByteString ofBase64​(java.lang.String string)
      Creates a byte string from a Base64 string.
      static byte[] parse​(java.lang.String string, int base)
      Parses a hexadecimal or binary string to a byte array.
      static byte[] parseBase64​(java.lang.String string)
      Parses a Base64 to a byte array.
      ByteString substring​(int start)
      Returns a ByteString that starts at a given position.
      ByteString substring​(int start, int end)
      Returns a ByteString that consists of a given range.
      java.lang.String toBase64String()
      Returns this byte string in Base64 format.
      java.lang.String toString()
      Returns this byte string in hexadecimal format.
      static java.lang.String toString​(byte[] bytes, int base)
      Returns the given byte array in hexadecimal format.
      java.lang.String toString​(int base)
      Returns this byte string in a given base.
      • Methods inherited from class java.lang.Object

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

      • bytes

        private final byte[] bytes
      • EMPTY

        public static final ByteString EMPTY
        An empty byte string.
      • DIGITS

        private static final char[] DIGITS
    • Constructor Detail

      • ByteString

        public ByteString​(byte[] bytes)
        Creates a ByteString.
        Parameters:
        bytes - Bytes
      • ByteString

        private ByteString​(byte[] bytes,
                           boolean dummy)
    • Method Detail

      • hashCode

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

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • compareTo

        public int compareTo​(ByteString that)
        Specified by:
        compareTo in interface java.lang.Comparable<ByteString>
      • toString

        public java.lang.String toString()
        Returns this byte string in hexadecimal format.
        Overrides:
        toString in class java.lang.Object
        Returns:
        Hexadecimal string
      • toString

        public java.lang.String toString​(int base)
        Returns this byte string in a given base.
        Returns:
        String in given base
      • toString

        public static java.lang.String toString​(byte[] bytes,
                                                int base)
        Returns the given byte array in hexadecimal format.

        For example, toString(new byte[] {0xDE, 0xAD}) returns "DEAD".

        Parameters:
        bytes - Array of bytes
        base - Base (2 or 16)
        Returns:
        String
      • toBase64String

        public java.lang.String toBase64String()
        Returns this byte string in Base64 format.
        Returns:
        Base64 string
      • of

        public static ByteString of​(java.lang.String string,
                                    int base)
        Creates a byte string from a hexadecimal or binary string.

        For example, of("DEAD", 16) returns the same as ByteString(new byte[] {0xDE, 0xAD}).

        Parameters:
        string - Array of bytes
        base - Base (2 or 16)
        Returns:
        String
      • parse

        public static byte[] parse​(java.lang.String string,
                                   int base)
        Parses a hexadecimal or binary string to a byte array.
        Parameters:
        string - Hexadecimal or binary string
        base - Base (2 or 16)
        Returns:
        Byte array
      • decodeHex

        private static byte decodeHex​(char c)
      • ofBase64

        public static ByteString ofBase64​(java.lang.String string)
        Creates a byte string from a Base64 string.
        Parameters:
        string - Base64 string
        Returns:
        Byte string
      • parseBase64

        public static byte[] parseBase64​(java.lang.String string)
        Parses a Base64 to a byte array.
        Parameters:
        string - Base64 string
        Returns:
        Byte array
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.lang.Object
      • length

        public int length()
        Returns the number of bytes in this byte string.
        Returns:
        Length of this byte string
      • byteAt

        public byte byteAt​(int i)
        Returns the byte at a given position in the byte string.
        Parameters:
        i - Index
        Returns:
        Byte at given position
        Throws:
        java.lang.IndexOutOfBoundsException - if the index argument is negative or not less than length()
      • substring

        public ByteString substring​(int start,
                                    int end)
        Returns a ByteString that consists of a given range.
        Parameters:
        start - Start of range
        end - Position after end of range
        Returns:
        Substring
      • substring

        public ByteString substring​(int start)
        Returns a ByteString that starts at a given position.
        Parameters:
        start - Start of range
        Returns:
        Substring
      • getBytes

        public byte[] getBytes()
        Returns a copy of the byte array.
      • concat

        public ByteString concat​(ByteString other)
        Returns a ByteString consisting of the concatenation of this and another string.
        Parameters:
        other - Byte string to concatenate
        Returns:
        Combined byte string
      • indexOf

        public int indexOf​(ByteString seek)
        Returns the position at which seek first occurs in this byte string, or -1 if it does not occur.
      • indexOf

        public int indexOf​(ByteString seek,
                           int start)
        Returns the position at which seek first occurs in this byte string, starting at the specified index, or -1 if it does not occur.