@CheckReturnValue public abstract class ByteString extends Object implements Iterable<Byte>
byte[], String,
ByteBuffer, InputStream, OutputStream. Also provides a conversion to
CodedInputStream.
Like String, the contents of a ByteString can never be observed to change, not even in the presence
of a data race or incorrect API usage in the client code.
Substring is supported by sharing the reference to the immutable underlying bytes. Concatenation is likewise
supported without copying (long strings) by building a tree of pieces in RopeByteString.
| Modifier and Type | Class and Description |
|---|---|
static interface |
ByteString.ByteIterator
This interface extends
Iterator<Byte>, so that we can return an unboxed byte. |
static class |
ByteString.Output
Outputs to a
ByteString instance. |
| Modifier and Type | Field and Description |
|---|---|
static ByteString |
EMPTY
Empty
ByteString. |
| Modifier and Type | Method and Description |
|---|---|
abstract ByteBuffer |
asReadOnlyByteBuffer()
Constructs a read-only
java.nio.ByteBuffer whose content is equal to the contents of this byte string. |
abstract List<ByteBuffer> |
asReadOnlyByteBufferList()
Constructs a list of read-only
java.nio.ByteBuffer objects such that the concatenation of their contents
is equal to the contents of this byte string. |
abstract byte |
byteAt(int index)
Gets the byte at the given index.
|
ByteString |
concat(ByteString other)
Concatenate the given
ByteString to this one. |
static ByteString |
copyFrom(byte[] bytes)
Copies the given bytes into a
ByteString. |
static ByteString |
copyFrom(byte[] bytes,
int offset,
int size)
Copies the given bytes into a
ByteString. |
static ByteString |
copyFrom(ByteBuffer bytes)
Copies the remaining bytes from a
java.nio.ByteBuffer into a ByteString. |
static ByteString |
copyFrom(ByteBuffer bytes,
int size)
Copies the next
size bytes from a java.nio.ByteBuffer into a ByteString. |
static ByteString |
copyFrom(Iterable<ByteString> byteStrings)
Concatenates all byte strings in the iterable and returns the result.
|
static ByteString |
copyFromUtf8(String text)
Encodes
text into a sequence of UTF-8 bytes and returns the result as a ByteString. |
void |
copyTo(byte[] target,
int offset)
Copies bytes into a buffer at the given offset.
|
abstract void |
copyTo(ByteBuffer target)
Copies bytes into a ByteBuffer.
|
protected abstract void |
copyToInternal(byte[] target,
int sourceOffset,
int targetOffset,
int numberToCopy)
Internal (package private) implementation of
copyTo(byte[],int,int,int). |
static ByteString |
empty()
Returns an empty
ByteString of size 0. |
boolean |
endsWith(ByteString suffix)
Tests if this bytestring ends with the specified suffix.
|
boolean |
equals(Object o)
Indicates whether some other object is "equal to" this one.
|
protected abstract boolean |
equalsInternal(ByteString other)
Internal portion of the equals check: as a precondition the caller has already checked most fast properties that
are common (including reference identity, null, size, cached hash codes if available) are the same.
|
static ByteString |
fromHex(String hexString)
Returns a
ByteString from a hexadecimal String. |
protected abstract int |
getTreeDepth()
Return the depth of the tree representing this
ByteString, if any, whose root is this node. |
int |
hashCode()
Compute the hashCode using the traditional algorithm from
ByteString. |
protected abstract boolean |
isBalanced()
Return
true if this ByteString is literal (a leaf node) or a flat-enough tree in the sense of
RopeByteString. |
boolean |
isEmpty()
Returns
true if the size is 0, false otherwise. |
abstract boolean |
isValidUtf8()
Tells whether this
ByteString represents a well-formed UTF-8 byte sequence, such that the original bytes
can be converted to a String object and then round tripped back to bytes without loss. |
ByteString.ByteIterator |
iterator()
Return a
ByteString.ByteIterator over the bytes in the ByteString. |
abstract CodedInputStream |
newCodedInput()
Creates a
CodedInputStream which can be used to read the bytes. |
abstract InputStream |
newInput()
Creates an
InputStream which can be used to read the bytes. |
static ByteString.Output |
newOutput()
Creates a new
ByteString.Output. |
static ByteString.Output |
newOutput(int initialCapacity)
Creates a new
ByteString.Output with the given initial capacity. |
protected abstract int |
partialHash(int h,
int offset,
int length)
Compute the hash across the value bytes starting with the given hash, and return the result.
|
protected int |
peekCachedHashCode()
Return the cached hash code if available.
|
static ByteString |
readFrom(InputStream streamToDrain)
Completely reads the given stream's bytes into a
ByteString, blocking if necessary until all bytes are
read through to the end of the stream. |
static ByteString |
readFrom(InputStream streamToDrain,
int chunkSize)
Completely reads the given stream's bytes into a
ByteString, blocking if necessary until all bytes are
read through to the end of the stream. |
static ByteString |
readFrom(InputStream streamToDrain,
int minChunkSize,
int maxChunkSize)
Helper method that takes the chunk size range as a parameter.
|
abstract int |
size()
Gets the number of bytes.
|
boolean |
startsWith(ByteString prefix)
Tests if this bytestring starts with the specified prefix.
|
ByteString |
substring(int beginIndex)
Return the substring from
beginIndex, inclusive, to the end of the string. |
abstract ByteString |
substring(int beginIndex,
int endIndex)
Return the substring from
beginIndex, inclusive, to endIndex, exclusive. |
ByteString |
substringNoCopy(int beginIndex)
Return the substring from
beginIndex, inclusive, to the end of the string. |
abstract ByteString |
substringNoCopy(int beginIndex,
int endIndex)
Return the substring from
beginIndex, inclusive, to endIndex, exclusive. |
byte[] |
toByteArray()
Copies bytes to a
byte[]. |
String |
toString()
Returns a string representation of the object.
|
protected abstract String |
toStringInternal(String charsetName)
Constructs a new
String by decoding the bytes using the specified charset name. |
String |
toStringUtf8()
Constructs a new
String by decoding the bytes as UTF-8. |
static Comparator<ByteString> |
unsignedLexicographicalComparator()
Returns a
Comparator which compares ByteString-s lexicographically as sequences of unsigned bytes
(i.e. |
abstract void |
writeTo(OutputStream out)
Writes a copy of the contents of this byte string to the specified output stream argument.
|
public static final ByteString EMPTY
ByteString.public abstract ByteBuffer asReadOnlyByteBuffer()
java.nio.ByteBuffer whose content is equal to the contents of this byte string.
The result uses the same backing array as the byte string, if possible.public abstract List<ByteBuffer> asReadOnlyByteBufferList()
java.nio.ByteBuffer objects such that the concatenation of their contents
is equal to the contents of this byte string. The result uses the same backing arrays as the byte string.
By returning a list, implementations of this method may be able to avoid copying even when there are multiple backing arrays.
public abstract byte byteAt(int index)
ByteString.ByteIterator returned by iterator(), and call
substringNoCopy(int, int) first if necessary.index - index of byteIndexOutOfBoundsException - index < 0 or index >= sizepublic final ByteString concat(ByteString other)
ByteString to this one. Short concatenations, of total size smaller than
CONCATENATE_BY_COPY_SIZE, are produced by copying the underlying bytes (as per Rope.java,
BAP95 . In
general, the concatenate involves no copying.other - string to concatenateByteString instanceIllegalArgumentException - if the combined size of the two byte strings exceeds Integer.MAX_VALUEpublic static ByteString copyFrom(byte[] bytes)
ByteString.bytes - to copyByteStringpublic static ByteString copyFrom(byte[] bytes, int offset, int size)
ByteString.bytes - source arrayoffset - offset in source arraysize - number of bytes to copyByteStringIndexOutOfBoundsException - if offset or size are out of boundspublic static ByteString copyFrom(ByteBuffer bytes)
java.nio.ByteBuffer into a ByteString.bytes - sourceBufferByteStringpublic static ByteString copyFrom(ByteBuffer bytes, int size)
size bytes from a java.nio.ByteBuffer into a ByteString.bytes - source buffersize - number of bytes to copyByteStringIndexOutOfBoundsException - if size > bytes.remaining()public static ByteString copyFrom(Iterable<ByteString> byteStrings)
The returned ByteString is not necessarily a unique object. If the list is empty, the returned object is
the singleton empty ByteString. If the list has only one element, that ByteString will be
returned without copying.
byteStrings - strings to be concatenatedByteStringIllegalArgumentException - if the combined size of the byte strings exceeds Integer.MAX_VALUEpublic static ByteString copyFromUtf8(String text)
text into a sequence of UTF-8 bytes and returns the result as a ByteString.text - source stringByteStringpublic void copyTo(byte[] target,
int offset)
To copy a subset of bytes, you call this method on the return value of substring(int, int). Example:
byteString.substring(start, end).copyTo(target, offset)
target - buffer to copy intooffset - in the target bufferIndexOutOfBoundsException - if the offset is negative or too largepublic abstract void copyTo(ByteBuffer target)
To copy a subset of bytes, you call this method on the return value of substring(int, int). Example:
byteString.substringNoCopy(start, end).copyTo(target)
target - ByteBuffer to copy into.ReadOnlyBufferException - if the target is read-onlyBufferOverflowException - if the target's remaining() space is not large enough to hold the data.protected abstract void copyToInternal(byte[] target,
int sourceOffset,
int targetOffset,
int numberToCopy)
copyTo(byte[],int,int,int). It assumes that all error
checking has already been performed and that numberToCopy > 0.public static final ByteString empty()
ByteString of size 0.public final boolean endsWith(ByteString suffix)
String.endsWith(String)suffix - the suffix.true if the byte sequence represented by the argument is a suffix of the byte sequence
represented by this string; false otherwise.public final boolean equals(Object o)
Object
The equals method implements an equivalence relation on non-null object references:
x, x.equals(x)
should return true.
x and y,
x.equals(y) should return true if and only if y.equals(x) returns
true.
x, y, and
z, if x.equals(y) returns true and y.equals(z) returns
true, then x.equals(z) should return true.
x and y, multiple
invocations of x.equals(y) consistently return true or consistently return
false, provided no information used in equals comparisons on the objects is
modified.
x, x.equals(null) should return
false.
The equals method for class Object implements the most discriminating possible
equivalence relation on objects; that is, for any non-null reference values x and
y, this method returns true if and only if x and y refer to the
same object (x == y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method
is overridden, so as to maintain the general contract for the hashCode method, which
states that equal objects must have equal hash codes.
equals in class Objecto - the reference object with which to compare.true if this object is the same as the obj argument; false otherwise.Object.hashCode(),
HashMapprotected abstract boolean equalsInternal(ByteString other)
public static ByteString fromHex(@CompileTimeConstant String hexString)
ByteString from a hexadecimal String.hexString - String of hexadecimal digits to create ByteString from.NumberFormatException - if the hexString does not contain a parsable hex String.protected abstract int getTreeDepth()
ByteString, if any, whose root is this node. If this is a
leaf node, return 0.public final int hashCode()
ByteString.hashCode in class ObjectObject.equals(java.lang.Object),
System.identityHashCode(java.lang.Object)protected abstract boolean isBalanced()
true if this ByteString is literal (a leaf node) or a flat-enough tree in the sense of
RopeByteString.public final boolean isEmpty()
true if the size is 0, false otherwise.public abstract boolean isValidUtf8()
ByteString represents a well-formed UTF-8 byte sequence, such that the original bytes
can be converted to a String object and then round tripped back to bytes without loss.
More precisely, returns true whenever:
Arrays.equals(byteString.toByteArray(), new String(byteString.toByteArray(), UTF_8).getBytes(UTF_8))
This method returns false for "overlong" byte sequences, as well as for 3-byte sequences that would map
to a surrogate character, in accordance with the restricted definition of UTF-8 introduced in Unicode 3.1. Note
that the UTF-8 decoder included in Oracle's JDK has been modified to also reject "overlong" byte sequences, but
(as of 2011) still accepts 3-byte surrogate character byte sequences.
See the Unicode Standard, Table 3-6 UTF-8 Bit Distribution, and Table 3-7 Well Formed UTF-8 Byte Sequences.
ByteString are a well-formed UTF-8 byte sequencepublic ByteString.ByteIterator iterator()
ByteString.ByteIterator over the bytes in the ByteString. To avoid auto-boxing, you may get the
iterator manually and call ByteString.ByteIterator.nextByte().public abstract CodedInputStream newCodedInput()
CodedInputStream which can be used to read the bytes. Using this is often more efficient than
creating a CodedInputStream that wraps the result of newInput().public abstract InputStream newInput()
InputStream which can be used to read the bytes.
The InputStream returned by this method is guaranteed to be completely non-blocking. The method
InputStream.available() returns the number of bytes remaining in the stream. The methods
InputStream.read(byte[]), InputStream.read(byte[],int,int) and InputStream.skip(long)
will read/skip as many bytes as are available. The method InputStream.markSupported() returns
true.
The methods in the returned InputStream might not be thread safe.
public static ByteString.Output newOutput()
ByteString.Output. Call ByteString.Output.toByteString() to create the ByteString instance.
A ByteString.Output offers the same functionality as a ByteArrayOutputStream, except that it
returns a ByteString rather than a byte
array.
OutputStream for building a ByteStringpublic static ByteString.Output newOutput(int initialCapacity)
ByteString.Output with the given initial capacity. Call ByteString.Output.toByteString() to create the
ByteString instance.
A ByteString.Output offers the same functionality as a ByteArrayOutputStream, except that it
returns a ByteString rather than a byte array.
initialCapacity - estimate of number of bytes to be writtenOutputStream for building a ByteStringprotected abstract int partialHash(int h,
int offset,
int length)
h - starting hash valueoffset - offset into this value to start looking at data valueslength - number of data values to include in the hash computationprotected final int peekCachedHashCode()
public static ByteString readFrom(InputStream streamToDrain) throws IOException
ByteString, blocking if necessary until all bytes are
read through to the end of the stream.
Performance notes: The returned ByteString is an immutable tree of byte arrays ("chunks") of the
stream data. The first chunk is small, with subsequent chunks each being double the size, up to 8K.
Each byte read from the input stream will be copied twice to ensure that the resulting ByteString is truly immutable.
streamToDrain - The source stream, which is read completely but not closed.ByteString which is made up of chunks of various sizes, depending on the behavior of the
underlying stream.IOException - if there is a problem reading the underlying streamIllegalArgumentException - if the stream supplies more than Integer.MAX_VALUE bytespublic static ByteString readFrom(InputStream streamToDrain, int chunkSize) throws IOException
ByteString, blocking if necessary until all bytes are
read through to the end of the stream.
Performance notes: The returned ByteString is an immutable tree of byte arrays ("chunks") of the
stream data. The chunkSize parameter sets the size of these byte arrays.
Each byte read from the input stream will be copied twice to ensure that the resulting ByteString is truly immutable.
streamToDrain - The source stream, which is read completely but not closed.chunkSize - The size of the chunks in which to read the stream.ByteString which is made up of chunks of the given size.IOException - if there is a problem reading the underlying streamIllegalArgumentException - if the stream supplies more than Integer.MAX_VALUE bytespublic static ByteString readFrom(InputStream streamToDrain, int minChunkSize, int maxChunkSize) throws IOException
streamToDrain - the source stream, which is read completely but not closedminChunkSize - the minimum size of the chunks in which to read the streammaxChunkSize - the maximum size of the chunks in which to read the streamByteString which is made up of chunks within the given size rangeIOException - if there is a problem reading the underlying streamIllegalArgumentException - if the stream supplies more than Integer.MAX_VALUE bytespublic abstract int size()
public final boolean startsWith(ByteString prefix)
String.startsWith(String)prefix - the prefix.true if the byte sequence represented by the argument is a prefix of the byte sequence
represented by this string; false otherwise.public final ByteString substring(int beginIndex)
beginIndex, inclusive, to the end of the string.beginIndex - start at this indexIndexOutOfBoundsException - if beginIndex < 0 or beginIndex > size().public abstract ByteString substring(int beginIndex, int endIndex)
beginIndex, inclusive, to endIndex, exclusive.beginIndex - start at this indexendIndex - the last character is the one before this indexIndexOutOfBoundsException - if beginIndex < 0, endIndex > size(), or beginIndex > endIndex.public final ByteString substringNoCopy(int beginIndex)
beginIndex, inclusive, to the end of the string. Unlike substring(int)
this method tries to avoid copies of the underlying data where possible, but may still copy in some situations.beginIndex - start at this indexIndexOutOfBoundsException - if beginIndex < 0 or beginIndex > size().public abstract ByteString substringNoCopy(int beginIndex, int endIndex)
beginIndex, inclusive, to endIndex, exclusive. Unlike
substring(int, int) this method tries to avoid copies of the underlying data where possible, but may
still copy in some situations.beginIndex - start at this indexendIndex - the last character is the one before this indexIndexOutOfBoundsException - if beginIndex < 0, endIndex > size(), or beginIndex > endIndex.public final byte[] toByteArray()
byte[].public final String toString()
ObjecttoString method returns a
string that "textually represents" this object. The result should be a concise but informative
representation that is easy for a person to read. It is recommended that all subclasses override
this method.
The toString method for class Object returns a string consisting of the name of
the class of which the object is an instance, the at-sign character `@', and the unsigned
hexadecimal representation of the hash code of the object. In other words, this method returns a
string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
protected abstract String toStringInternal(String charsetName) throws UnsupportedEncodingException
String by decoding the bytes using the specified charset name.charsetName - charset name to use for decodingUnsupportedEncodingException - if the charset is not supportedpublic final String toStringUtf8()
String by decoding the bytes as UTF-8.public static Comparator<ByteString> unsignedLexicographicalComparator()
Comparator which compares ByteString-s lexicographically as sequences of unsigned bytes
(i.e. values between 0 and 255, inclusive).
For example, (byte) -1 is considered to be greater than (byte) 1 because it is interpreted as an
unsigned value, 255:
`-1` -> 0b11111111 (two's complement) -> 255
`1` -> 0b00000001 -> 1
public abstract void writeTo(OutputStream out) throws IOException
out - the output stream to which to write the data.IOException - if an I/O error occurs.