private static class ByteString.LiteralByteString extends ByteString.LeafByteString
ByteString backed by a
single array of bytes, contiguous in memory. It supports substring by
pointing to only a sub-range of the underlying byte array, meaning that a
substring will reference the full byte-array of the string it's made from,
exactly as with String.ByteString.ByteIterator, ByteString.CodedBuilder, ByteString.LeafByteString, ByteString.Output| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
bytes |
private static long |
serialVersionUID |
CONCATENATE_BY_COPY_SIZE, EMPTY, MAX_READ_FROM_CHUNK_SIZE, MIN_READ_FROM_CHUNK_SIZE| Constructor and Description |
|---|
LiteralByteString(byte[] bytes)
Creates a
LiteralByteString backed by the given array, without
copying. |
| Modifier and Type | Method and Description |
|---|---|
java.nio.ByteBuffer |
asReadOnlyByteBuffer()
Constructs a read-only
java.nio.ByteBuffer whose content
is equal to the contents of this byte string. |
java.util.List<java.nio.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. |
byte |
byteAt(int index)
Gets the byte at the given index.
|
void |
copyTo(java.nio.ByteBuffer target)
Copies bytes into a ByteBuffer.
|
protected void |
copyToInternal(byte[] target,
int sourceOffset,
int targetOffset,
int numberToCopy)
Internal (package private) implementation of
ByteString.copyTo(byte[],int,int,int). |
boolean |
equals(java.lang.Object other) |
(package private) boolean |
equalsRange(ByteString other,
int offset,
int length)
Check equality of the substring of given length of this object starting at
zero with another
LiteralByteString substring starting at offset. |
protected int |
getOffsetIntoBytes()
Offset into
bytes[] to use, non-zero for substrings. |
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. |
CodedInputStream |
newCodedInput()
Creates a
CodedInputStream which can be used to read the bytes. |
java.io.InputStream |
newInput()
Creates an
InputStream which can be used to read the bytes. |
protected 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 |
partialIsValidUtf8(int state,
int offset,
int length)
Tells whether the given byte sequence is a well-formed, malformed, or
incomplete UTF-8 byte sequence.
|
int |
size()
Gets the number of bytes.
|
ByteString |
substring(int beginIndex,
int endIndex)
Return the substring from
beginIndex, inclusive, to endIndex, exclusive. |
protected java.lang.String |
toStringInternal(java.nio.charset.Charset charset)
Constructs a new
String by decoding the bytes using the
specified charset. |
(package private) void |
writeTo(ByteOutput output)
Writes this
ByteString to the provided ByteOutput. |
void |
writeTo(java.io.OutputStream outputStream)
Writes a copy of the contents of this byte string to the specified output stream argument.
|
(package private) void |
writeToInternal(java.io.OutputStream outputStream,
int sourceOffset,
int numberToWrite)
Internal version of
ByteString.writeTo(OutputStream,int,int) that assumes
all error checking has already been done. |
getTreeDepth, isBalancedcheckIndex, checkRange, concat, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFromUtf8, copyTo, copyTo, endsWith, hashCode, isEmpty, iterator, newCodedBuilder, newOutput, newOutput, peekCachedHashCode, readFrom, readFrom, readFrom, startsWith, substring, toByteArray, toString, toString, toString, toStringUtf8, wrap, wrap, wrap, writeToprivate static final long serialVersionUID
protected final byte[] bytes
LiteralByteString(byte[] bytes)
LiteralByteString backed by the given array, without
copying.bytes - array to wrappublic byte byteAt(int index)
ByteStringByteString.ByteIterator returned by ByteString.iterator(), and call ByteString.substring(int, int) first if necessary.byteAt in class ByteStringindex - index of bytepublic int size()
ByteStringsize in class ByteStringpublic final ByteString substring(int beginIndex, int endIndex)
ByteStringbeginIndex, inclusive, to endIndex, exclusive.substring in class ByteStringbeginIndex - start at this indexendIndex - the last character is the one before this indexprotected void copyToInternal(byte[] target,
int sourceOffset,
int targetOffset,
int numberToCopy)
ByteStringByteString.copyTo(byte[],int,int,int).
It assumes that all error checking has already been performed and that
numberToCopy > 0.copyToInternal in class ByteStringpublic final void copyTo(java.nio.ByteBuffer target)
ByteStringcopyTo in class ByteStringtarget - ByteBuffer to copy into.public final java.nio.ByteBuffer asReadOnlyByteBuffer()
ByteStringjava.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.asReadOnlyByteBuffer in class ByteStringpublic final java.util.List<java.nio.ByteBuffer> asReadOnlyByteBufferList()
ByteStringjava.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.
asReadOnlyByteBufferList in class ByteStringpublic final void writeTo(java.io.OutputStream outputStream)
throws java.io.IOException
ByteStringwriteTo in class ByteStringoutputStream - the output stream to which to write the data.java.io.IOException - if an I/O error occurs.final void writeToInternal(java.io.OutputStream outputStream,
int sourceOffset,
int numberToWrite)
throws java.io.IOException
ByteStringByteString.writeTo(OutputStream,int,int) that assumes
all error checking has already been done.writeToInternal in class ByteStringjava.io.IOExceptionfinal void writeTo(ByteOutput output) throws java.io.IOException
ByteStringByteString to the provided ByteOutput. Calling
this method may result in multiple operations on the target ByteOutput.
This method may expose internal backing buffers of the ByteString to the ByteOutput in order to avoid additional copying overhead. It would be possible for a malicious
ByteOutput to corrupt the ByteString. Use with caution!
writeTo in class ByteStringoutput - the output target to receive the bytesjava.io.IOException - if an I/O error occursUnsafeByteOperations.unsafeWriteTo(ByteString, ByteOutput)protected final java.lang.String toStringInternal(java.nio.charset.Charset charset)
ByteStringString by decoding the bytes using the
specified charset.toStringInternal in class ByteStringcharset - encode using this charsetpublic final boolean isValidUtf8()
ByteStringByteString 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,
Table 3-7. Well Formed UTF-8 Byte Sequences.
isValidUtf8 in class ByteStringByteString are a
well-formed UTF-8 byte sequenceprotected final int partialIsValidUtf8(int state,
int offset,
int length)
ByteStringByteString segments.partialIsValidUtf8 in class ByteStringstate - either 0 (if this is the initial decoding operation)
or the value returned from a call to a partial decoding method for the
previous bytesoffset - offset of the first byte to checklength - number of bytes to check-1 if the partial byte sequence is definitely malformed,
0 if it is well-formed (no additional input needed), or, if the
byte sequence is "incomplete", i.e. apparently terminated in the middle of
a character, an opaque integer "state" value containing enough information
to decode the character when passed to a subsequent invocation of a
partial decoding method.public final boolean equals(java.lang.Object other)
equals in class ByteStringfinal boolean equalsRange(ByteString other, int offset, int length)
LiteralByteString substring starting at offset.equalsRange in class ByteString.LeafByteStringother - what to compare a substring inoffset - offset into otherlength - number of bytes to compareprotected final int partialHash(int h,
int offset,
int length)
ByteStringpartialHash in class ByteStringh - starting hash valueoffset - offset into this value to start looking at data valueslength - number of data values to include in the hash computationpublic final java.io.InputStream newInput()
ByteStringInputStream 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.
newInput in class ByteStringpublic final CodedInputStream newCodedInput()
ByteStringCodedInputStream which can be used to read the bytes.
Using this is often more efficient than creating a CodedInputStream
that wraps the result of ByteString.newInput().newCodedInput in class ByteStringprotected int getOffsetIntoBytes()
bytes[] to use, non-zero for substrings.