Package org.apache.calcite.avatica.util
Class ByteString
- java.lang.Object
-
- org.apache.calcite.avatica.util.ByteString
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<ByteString>
public class ByteString extends java.lang.Object implements java.lang.Comparable<ByteString>, java.io.Serializable
Collection of bytes.ByteString is to bytes what
Stringis to chars: It is immutable, implements equality (hashCode()andequals(java.lang.Object)), comparison (compareTo(org.apache.calcite.avatica.util.ByteString)) andserializationcorrectly.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]bytesprivate static char[]DIGITSstatic ByteStringEMPTYAn empty byte string.
-
Constructor Summary
Constructors Modifier Constructor Description ByteString(byte[] bytes)Creates a ByteString.privateByteString(byte[] bytes, boolean dummy)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description bytebyteAt(int i)Returns the byte at a given position in the byte string.java.lang.Objectclone()intcompareTo(ByteString that)ByteStringconcat(ByteString other)Returns a ByteString consisting of the concatenation of this and another string.private static bytedecodeHex(char c)booleanequals(java.lang.Object obj)byte[]getBytes()Returns a copy of the byte array.inthashCode()intindexOf(ByteString seek)Returns the position at whichseekfirst occurs in this byte string, or -1 if it does not occur.intindexOf(ByteString seek, int start)Returns the position at whichseekfirst occurs in this byte string, starting at the specified index, or -1 if it does not occur.intlength()Returns the number of bytes in this byte string.static ByteStringof(java.lang.String string, int base)Creates a byte string from a hexadecimal or binary string.static ByteStringofBase64(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.ByteStringsubstring(int start)Returns a ByteString that starts at a given position.ByteStringsubstring(int start, int end)Returns a ByteString that consists of a given range.java.lang.StringtoBase64String()Returns this byte string in Base64 format.java.lang.StringtoString()Returns this byte string in hexadecimal format.static java.lang.StringtoString(byte[] bytes, int base)Returns the given byte array in hexadecimal format.java.lang.StringtoString(int base)Returns this byte string in a given base.
-
-
-
Field Detail
-
bytes
private final byte[] bytes
-
EMPTY
public static final ByteString EMPTY
An empty byte string.
-
DIGITS
private static final char[] DIGITS
-
-
Method Detail
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
compareTo
public int compareTo(ByteString that)
- Specified by:
compareToin interfacejava.lang.Comparable<ByteString>
-
toString
public java.lang.String toString()
Returns this byte string in hexadecimal format.- Overrides:
toStringin classjava.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 bytesbase- 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 asByteString(new byte[] {0xDE, 0xAD}).- Parameters:
string- Array of bytesbase- 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 stringbase- 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:
clonein classjava.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 theindexargument is negative or not less thanlength()
-
substring
public ByteString substring(int start, int end)
Returns a ByteString that consists of a given range.- Parameters:
start- Start of rangeend- 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 whichseekfirst occurs in this byte string, or -1 if it does not occur.
-
indexOf
public int indexOf(ByteString seek, int start)
Returns the position at whichseekfirst occurs in this byte string, starting at the specified index, or -1 if it does not occur.
-
-