public final class ByteString extends java.lang.Object implements ByteSequence
| Modifier and Type | Method and Description |
|---|---|
ByteSequenceReader |
asReader()
Returns a
ByteSequenceReader which can be used to incrementally read and decode data from this byte string. |
byte |
byteAt(int index)
Returns the byte value at the specified index.
|
int |
compareTo(byte[] b, int offset, int length)
Compares this byte sequence with the specified byte array sub-sequence for order.
|
int |
compareTo(ByteSequence o)
Compares this byte sequence with the specified byte sequence for order.
|
byte[] |
copyTo(byte[] b)
Copies the contents of this byte sequence to the provided byte array.
|
byte[] |
copyTo(byte[] b, int offset)
Copies the contents of this byte sequence to the specified location in the provided byte array.
|
ByteStringBuilder |
copyTo(ByteStringBuilder builder)
Appends the entire contents of this byte sequence to the provided
ByteStringBuilder. |
java.io.OutputStream |
copyTo(java.io.OutputStream stream)
Copies the entire contents of this byte sequence to the provided
OutputStream. |
static ByteString |
empty()
Returns an empty byte string.
|
boolean |
equals(byte[] b, int offset, int length)
Indicates whether the provided byte array sub-sequence is equal to this byte sequence.
|
boolean |
equals(java.lang.Object o)
Indicates whether the provided object is equal to this byte string.
|
int |
hashCode()
Returns a hash code for this byte string.
|
static ByteString |
hexValueOf(int i)
Returns a byte string following those steps: - The integer is transformed into its hexadecimal representation with 8 digits.
|
int |
length()
Returns the length of this byte sequence.
|
ByteString |
subSequence(int start, int end)
Returns a new byte sequence that is a subsequence of this byte sequence.
|
byte[] |
toByteArray()
Returns a byte array containing the bytes in this sequence in the same order as this sequence.
|
ByteString |
toByteString()
Returns the
ByteString representation of this byte sequence. |
char[] |
toCharArray()
Returns the UTF-8 decoded char array representation of this byte sequence.
|
java.lang.String |
toHex()
Returns a string representation of the contents of this byte sequence using hexadecimal characters and a space between each byte.
|
void |
toHex(java.lang.StringBuilder builder, int indent)
Appends a string representation of the data in this byte sequence to the given buffer using the specified indent.
|
void |
toHexPlusAscii(java.lang.StringBuilder builder, int indent)
Appends a string representation of the data in this byte sequence to the given buffer using the specified indent.
|
int |
toInt()
Returns the integer value represented by the first four bytes of this byte string in big-endian order.
|
long |
toLong()
Returns the long value represented by the first eight bytes of this byte string in big-endian order.
|
java.lang.String |
toString()
Returns the UTF-8 decoded string representation of this byte sequence.
|
static ByteString |
valueOf(char[] chars)
Returns a byte string containing the UTF-8 encoded bytes of the provided char array.
|
static ByteString |
valueOf(int i)
Returns a byte string containing the big-endian encoded bytes of the provided integer.
|
static ByteString |
valueOf(long l)
Returns a byte string containing the big-endian encoded bytes of the provided long.
|
static ByteString |
valueOf(java.lang.Object o)
Returns a byte string containing the provided object.
|
static ByteString |
valueOf(java.lang.String s)
Returns a byte string containing the UTF-8 encoded bytes of the provided string.
|
static ByteString |
wrap(byte[] b)
Returns a byte string that wraps the provided byte array.
|
static ByteString |
wrap(byte[] b, int offset, int length)
Returns a byte string that wraps a subsequence of the provided byte array.
|
public static ByteString empty()
public static ByteString valueOf(int i)
i - The integer to encode.public static ByteString hexValueOf(int i)
i - The integer to encode.public static ByteString valueOf(long l)
l - The long to encode.public static ByteString valueOf(java.lang.Object o)
ByteSequence then it is converted to a byte string using the toByteString() method. Otherwise a new byte string is created containing the UTF-8 encoded bytes of the string representation of the provided object.o - The object to use.public static ByteString valueOf(java.lang.String s)
s - The string to use.public static ByteString valueOf(char[] chars)
chars - The char array to use.public static ByteString wrap(byte[] b)
NOTE: this method takes ownership of the provided byte array and, therefore, the byte array MUST NOT be altered directly after this method returns.
b - The byte array to wrap.public static ByteString wrap(byte[] b, int offset, int length) throws java.lang.IndexOutOfBoundsException
NOTE: this method takes ownership of the provided byte array and, therefore, the byte array MUST NOT be altered directly after this method returns.
b - The byte array to wrap.offset - The offset of the byte array to be used; must be non-negative and no larger than b.length .length - The length of the byte array to be used; must be non-negative and no larger than b.length - offset.java.lang.IndexOutOfBoundsException - If offset is negative or if length is negative or if offset + length is greater than b.length.public ByteSequenceReader asReader()
ByteSequenceReader which can be used to incrementally read and decode data from this byte string.asReader in interface ByteSequenceByteSequenceReader which can be used to incrementally read and decode data from this byte string.
public byte byteAt(int index)
throws java.lang.IndexOutOfBoundsException
An index ranges from zero to length() - 1. The first byte value of the sequence is at index zero, the next at index one, and so on, as for array indexing.
byteAt in interface ByteSequenceindex - The index of the byte to be returned.java.lang.IndexOutOfBoundsException - If the index argument is negative or not less than length().
public int compareTo(byte[] b,
int offset,
int length)
throws java.lang.IndexOutOfBoundsException
compareTo in interface ByteSequenceb - The byte array to compare.offset - The offset of the sub-sequence in the byte array to be compared; must be non-negative and no larger than b.length .length - The length of the sub-sequence in the byte array to be compared; must be non-negative and no larger than b.length - offset.java.lang.IndexOutOfBoundsException - If offset is negative or if length is negative or if offset + length is greater than b.length.public int compareTo(ByteSequence o)
compareTo in interface java.lang.Comparable<ByteSequence>compareTo in interface ByteSequenceo - The byte sequence to be compared.public byte[] copyTo(byte[] b)
Copying will stop when either the entire content of this sequence has been copied or if the end of the provided byte array has been reached.
An invocation of the form:
src.copyTo(b)Behaves in exactly the same way as the invocation:
src.copyTo(b, 0);
copyTo in interface ByteSequenceb - The byte array to which bytes are to be copied.
public byte[] copyTo(byte[] b,
int offset)
throws java.lang.IndexOutOfBoundsException
Copying will stop when either the entire content of this sequence has been copied or if the end of the provided byte array has been reached.
An invocation of the form:
src.copyTo(b, offset)Behaves in exactly the same way as the invocation:
int len = Math.min(src.length(), b.length - offset); for (int i = 0; i < len; i++) b[offset + i] = src.get(i);Except that it is potentially much more efficient.
copyTo in interface ByteSequenceb - The byte array to which bytes are to be copied.offset - The offset within the array of the first byte to be written; must be non-negative and no larger than b.length.java.lang.IndexOutOfBoundsException - If offset is negative.public ByteStringBuilder copyTo(ByteStringBuilder builder)
ByteStringBuilder.copyTo in interface ByteSequencebuilder - The builder to copy to.
public java.io.OutputStream copyTo(java.io.OutputStream stream)
throws java.io.IOException
OutputStream.copyTo in interface ByteSequencestream - The OutputStream to copy to.OutputStream.java.io.IOException - If an error occurs while writing to the OutputStream.
public boolean equals(byte[] b,
int offset,
int length)
throws java.lang.IndexOutOfBoundsException
equals in interface ByteSequenceb - The byte array for which to make the determination.offset - The offset of the sub-sequence in the byte array to be compared; must be non-negative and no larger than b.length .length - The length of the sub-sequence in the byte array to be compared; must be non-negative and no larger than b.length - offset.true if the content of the provided byte array sub-sequence is equal to that of this byte sequence, or false if not.java.lang.IndexOutOfBoundsException - If offset is negative or if length is negative or if offset + length is greater than b.length.public boolean equals(java.lang.Object o)
equals in interface ByteSequenceequals in class java.lang.Objecto - The object for which to make the determination.true if the provided object is a byte sequence whose content is equal to that of this byte string, or false if not.public int hashCode()
hashCode in interface ByteSequencehashCode in class java.lang.Objectpublic int length()
length in interface ByteSequencepublic ByteString subSequence(int start, int end) throws java.lang.IndexOutOfBoundsException
The subsequence starts with the byte value at the specified start index and ends with the byte value at index end - 1. The length (in bytes) of the returned sequence is end - start, so if start == end then an empty sequence is returned.
NOTE: changes to the underlying byte sequence (if mutable) may render the returned sub-sequence invalid.
subSequence in interface ByteSequencestart - The start index, inclusive.end - The end index, exclusive.java.lang.IndexOutOfBoundsException - If start or end are negative, if end is greater than length(), or if start is greater than end.public byte[] toByteArray()
An invocation of the form:
src.toByteArray()Behaves in exactly the same way as the invocation:
src.copyTo(new byte[src.length()]);
toByteArray in interface ByteSequencepublic ByteString toByteString()
ByteString representation of this byte sequence.toByteString in interface ByteSequenceByteString representation of this byte sequence.public java.lang.String toHex()
public void toHexPlusAscii(java.lang.StringBuilder builder,
int indent)
The data will be formatted with sixteen hex bytes in a row followed by the ASCII representation, then wrapping to a new line as necessary. The state of the byte buffer is not changed.
builder - The buffer to which the information is to be appended.indent - The number of spaces to indent the output.
public void toHex(java.lang.StringBuilder builder,
int indent)
The data will be formatted with sixteen hex bytes in a row then wrapping to a new line as necessary. The state of the byte buffer is not changed.
builder - The buffer to which the information is to be appended.indent - The number of spaces to indent the output.public char[] toCharArray()
public int toInt()
throws java.lang.IndexOutOfBoundsException
java.lang.IndexOutOfBoundsException - If this byte string has less than four bytes.
public long toLong()
throws java.lang.IndexOutOfBoundsException
java.lang.IndexOutOfBoundsException - If this byte string has less than eight bytes.public java.lang.String toString()
toString in interface ByteSequencetoString in class java.lang.Object