final class NioByteString extends ByteString.LeafByteString
ByteString
that wraps around a ByteBuffer
.ByteString.ByteIterator, ByteString.CodedBuilder, ByteString.LeafByteString, ByteString.Output
Modifier and Type | Field and Description |
---|---|
private java.nio.ByteBuffer |
buffer |
CONCATENATE_BY_COPY_SIZE, EMPTY, MAX_READ_FROM_CHUNK_SIZE, MIN_READ_FROM_CHUNK_SIZE
Constructor and Description |
---|
NioByteString(java.nio.ByteBuffer buffer) |
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
ByteString substring starting at offset. |
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.
|
private void |
readObject(java.io.ObjectInputStream in)
Magic method that lets us override deserialization behavior.
|
int |
size()
Gets the number of bytes.
|
private java.nio.ByteBuffer |
slice(int beginIndex,
int endIndex)
Creates a slice of a range of this buffer.
|
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. |
private java.lang.Object |
writeReplace()
Magic method that lets us override serialization behavior.
|
(package private) void |
writeTo(ByteOutput output)
Writes this
ByteString to the provided ByteOutput . |
void |
writeTo(java.io.OutputStream out)
Writes a copy of the contents of this byte string to the specified output stream argument.
|
(package private) void |
writeToInternal(java.io.OutputStream out,
int sourceOffset,
int numberToWrite)
Internal version of
ByteString.writeTo(OutputStream,int,int) that assumes
all error checking has already been done. |
getTreeDepth, isBalanced
checkIndex, 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, writeTo
private java.lang.Object writeReplace()
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException
java.io.IOException
public byte byteAt(int index)
ByteString
ByteString.ByteIterator
returned by ByteString.iterator()
, and call ByteString.substring(int, int)
first if necessary.byteAt
in class ByteString
index
- index of bytepublic int size()
ByteString
size
in class ByteString
public ByteString substring(int beginIndex, int endIndex)
ByteString
beginIndex
, inclusive, to endIndex
, exclusive.substring
in class ByteString
beginIndex
- start at this indexendIndex
- the last character is the one before this indexprotected void copyToInternal(byte[] target, int sourceOffset, int targetOffset, int numberToCopy)
ByteString
ByteString.copyTo(byte[],int,int,int)
.
It assumes that all error checking has already been performed and that
numberToCopy > 0
.copyToInternal
in class ByteString
public void copyTo(java.nio.ByteBuffer target)
ByteString
copyTo
in class ByteString
target
- ByteBuffer to copy into.public void writeTo(java.io.OutputStream out) throws java.io.IOException
ByteString
writeTo
in class ByteString
out
- the output stream to which to write the data.java.io.IOException
- if an I/O error occurs.boolean equalsRange(ByteString other, int offset, int length)
ByteString.LeafByteString
ByteString
substring starting at offset.equalsRange
in class ByteString.LeafByteString
other
- what to compare a substring inoffset
- offset into otherlength
- number of bytes to comparevoid writeToInternal(java.io.OutputStream out, int sourceOffset, int numberToWrite) throws java.io.IOException
ByteString
ByteString.writeTo(OutputStream,int,int)
that assumes
all error checking has already been done.writeToInternal
in class ByteString
java.io.IOException
void writeTo(ByteOutput output) throws java.io.IOException
ByteString
ByteString
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 ByteString
output
- the output target to receive the bytesjava.io.IOException
- if an I/O error occursUnsafeByteOperations.unsafeWriteTo(ByteString, ByteOutput)
public java.nio.ByteBuffer asReadOnlyByteBuffer()
ByteString
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.asReadOnlyByteBuffer
in class ByteString
public java.util.List<java.nio.ByteBuffer> asReadOnlyByteBufferList()
ByteString
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.
asReadOnlyByteBufferList
in class ByteString
protected java.lang.String toStringInternal(java.nio.charset.Charset charset)
ByteString
String
by decoding the bytes using the
specified charset.toStringInternal
in class ByteString
charset
- encode using this charsetpublic boolean isValidUtf8()
ByteString
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,
Table 3-7. Well Formed UTF-8 Byte Sequences.
isValidUtf8
in class ByteString
ByteString
are a
well-formed UTF-8 byte sequenceprotected int partialIsValidUtf8(int state, int offset, int length)
ByteString
ByteString
segments.partialIsValidUtf8
in class ByteString
state
- 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 boolean equals(java.lang.Object other)
equals
in class ByteString
protected int partialHash(int h, int offset, int length)
ByteString
partialHash
in class ByteString
h
- starting hash valueoffset
- offset into this value to start looking at data valueslength
- number of data values to include in the hash computationpublic java.io.InputStream newInput()
ByteString
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.
newInput
in class ByteString
public CodedInputStream newCodedInput()
ByteString
CodedInputStream
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 ByteString
private java.nio.ByteBuffer slice(int beginIndex, int endIndex)
beginIndex
- the beginning index of the slice (inclusive).endIndex
- the end index of the slice (exclusive).