Class ByteString

java.lang.Object
org.apache.calcite.avatica.util.ByteString
All Implemented Interfaces:
Serializable, Comparable<ByteString>

public class ByteString extends Object implements Comparable<ByteString>, Serializable
Collection of bytes.

ByteString is to bytes what String is to chars: It is immutable, implements equality (hashCode() and equals(java.lang.Object)), comparison (compareTo(org.apache.calcite.avatica.util.ByteString)) and serialization correctly.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final ByteString
    An empty byte string.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteString(byte[] bytes)
    Creates a ByteString.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte
    byteAt(int i)
    Returns the byte at a given position in the byte string.
     
    int
     
    Returns a ByteString consisting of the concatenation of this and another string.
    boolean
     
    byte[]
    Returns a copy of the byte array.
    int
     
    int
    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
    Returns the number of bytes in this byte string.
    static ByteString
    of(String string, int base)
    Creates a byte string from a hexadecimal or binary string.
    static ByteString
    ofBase64(String string)
    Creates a byte string from a Base64 string.
    static byte[]
    parse(String string, int base)
    Parses a hexadecimal or binary string to a byte array.
    static byte[]
    Parses a Base64 to a byte array.
    substring(int start)
    Returns a ByteString that starts at a given position.
    substring(int start, int end)
    Returns a ByteString that consists of a given range.
    Returns this byte string in Base64 format.
    Returns this byte string in hexadecimal format.
    static String
    toString(byte[] bytes, int base)
    Returns the given byte array in hexadecimal format.
    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 Details

    • EMPTY

      public static final ByteString EMPTY
      An empty byte string.
  • Constructor Details

    • ByteString

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

    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • compareTo

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

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

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

      public static 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 String toBase64String()
      Returns this byte string in Base64 format.
      Returns:
      Base64 string
    • of

      public static ByteString of(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(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
    • ofBase64

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

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

      public Object clone()
      Overrides:
      clone in class 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:
      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.