Class NioByteString
- All Implemented Interfaces:
Serializable
,Iterable<Byte>
ByteString
that wraps around a ByteBuffer
.-
Nested Class Summary
Nested classes/interfaces inherited from class com.google.protobuf.ByteString
ByteString.AbstractByteIterator, ByteString.ByteIterator, ByteString.CodedBuilder, ByteString.LeafByteString, ByteString.Output
-
Field Summary
FieldsFields inherited from class com.google.protobuf.ByteString
CONCATENATE_BY_COPY_SIZE, EMPTY, MAX_READ_FROM_CHUNK_SIZE, MIN_READ_FROM_CHUNK_SIZE
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConstructs a read-onlyjava.nio.ByteBuffer
whose content is equal to the contents of this byte string.Constructs a list of read-onlyjava.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
(ByteBuffer target) Copies bytes into a ByteBuffer.protected void
copyToInternal
(byte[] target, int sourceOffset, int targetOffset, int numberToCopy) Internal (package private) implementation ofByteString.copyTo(byte[],int,int,int)
.boolean
(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 anotherByteString
substring starting at offset.byte
internalByteAt
(int index) Gets the byte at the given index, assumes bounds checking has already been performed.boolean
Tells whether thisByteString
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.Creates aCodedInputStream
which can be used to read the bytes.newInput()
Creates anInputStream
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
Magic method that lets us override deserialization behavior.int
size()
Gets the number of bytes.private ByteBuffer
slice
(int beginIndex, int endIndex) Creates a slice of a range of this buffer.substring
(int beginIndex, int endIndex) Return the substring frombeginIndex
, inclusive, toendIndex
, exclusive.protected String
toStringInternal
(Charset charset) Constructs a newString
by decoding the bytes using the specified charset.private Object
Magic method that lets us override serialization behavior.(package private) void
writeTo
(ByteOutput output) Writes thisByteString
to the providedByteOutput
.void
writeTo
(OutputStream out) Writes a copy of the contents of this byte string to the specified output stream argument.(package private) void
writeToInternal
(OutputStream out, int sourceOffset, int numberToWrite) Internal version ofByteString.writeTo(OutputStream,int,int)
that assumes all error checking has already been done.Methods inherited from class com.google.protobuf.ByteString.LeafByteString
getTreeDepth, isBalanced, writeToReverse
Methods inherited from class com.google.protobuf.ByteString
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, unsignedLexicographicalComparator, wrap, wrap, wrap, writeTo
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
buffer
-
-
Constructor Details
-
NioByteString
NioByteString(ByteBuffer buffer)
-
-
Method Details
-
writeReplace
Magic method that lets us override serialization behavior. -
readObject
Magic method that lets us override deserialization behavior.- Throws:
IOException
-
byteAt
public byte byteAt(int index) Description copied from class:ByteString
Gets the byte at the given index. This method should be used only for random access to individual bytes. To access bytes sequentially, use theByteString.ByteIterator
returned byByteString.iterator()
, and callByteString.substring(int, int)
first if necessary.- Specified by:
byteAt
in classByteString
- Parameters:
index
- index of byte- Returns:
- the value
-
internalByteAt
public byte internalByteAt(int index) Description copied from class:ByteString
Gets the byte at the given index, assumes bounds checking has already been performed.- Specified by:
internalByteAt
in classByteString
- Parameters:
index
- index of byte- Returns:
- the value
-
size
public int size()Description copied from class:ByteString
Gets the number of bytes.- Specified by:
size
in classByteString
- Returns:
- size in bytes
-
substring
Description copied from class:ByteString
Return the substring frombeginIndex
, inclusive, toendIndex
, exclusive.- Specified by:
substring
in classByteString
- Parameters:
beginIndex
- start at this indexendIndex
- the last character is the one before this index- Returns:
- substring sharing underlying data
-
copyToInternal
protected void copyToInternal(byte[] target, int sourceOffset, int targetOffset, int numberToCopy) Description copied from class:ByteString
Internal (package private) implementation ofByteString.copyTo(byte[],int,int,int)
. It assumes that all error checking has already been performed and thatnumberToCopy > 0
.- Specified by:
copyToInternal
in classByteString
-
copyTo
Description copied from class:ByteString
Copies bytes into a ByteBuffer.To copy a subset of bytes, you call this method on the return value of
ByteString.substring(int, int)
. Example:byteString.substring(start, end).copyTo(target)
- Specified by:
copyTo
in classByteString
- Parameters:
target
- ByteBuffer to copy into.
-
writeTo
Description copied from class:ByteString
Writes a copy of the contents of this byte string to the specified output stream argument.- Specified by:
writeTo
in classByteString
- Parameters:
out
- the output stream to which to write the data.- Throws:
IOException
- if an I/O error occurs.
-
equalsRange
Description copied from class:ByteString.LeafByteString
Check equality of the substring of given length of this object starting at zero with anotherByteString
substring starting at offset.- Specified by:
equalsRange
in classByteString.LeafByteString
- Parameters:
other
- what to compare a substring inoffset
- offset into otherlength
- number of bytes to compare- Returns:
- true for equality of substrings, else false.
-
writeToInternal
Description copied from class:ByteString
Internal version ofByteString.writeTo(OutputStream,int,int)
that assumes all error checking has already been done.- Specified by:
writeToInternal
in classByteString
- Throws:
IOException
-
writeTo
Description copied from class:ByteString
Writes thisByteString
to the providedByteOutput
. Calling this method may result in multiple operations on the targetByteOutput
.This method may expose internal backing buffers of the
ByteString
to theByteOutput
in order to avoid additional copying overhead. It would be possible for a maliciousByteOutput
to corrupt theByteString
. Use with caution!- Specified by:
writeTo
in classByteString
- Parameters:
output
- the output target to receive the bytes- Throws:
IOException
- if an I/O error occurs- See Also:
-
asReadOnlyByteBuffer
Description copied from class:ByteString
Constructs a read-onlyjava.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.- Specified by:
asReadOnlyByteBuffer
in classByteString
- Returns:
- wrapped bytes
-
asReadOnlyByteBufferList
Description copied from class:ByteString
Constructs a list of read-onlyjava.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.
- Specified by:
asReadOnlyByteBufferList
in classByteString
- Returns:
- a list of wrapped bytes
-
toStringInternal
Description copied from class:ByteString
Constructs a newString
by decoding the bytes using the specified charset.- Specified by:
toStringInternal
in classByteString
- Parameters:
charset
- encode using this charset- Returns:
- new string
-
isValidUtf8
public boolean isValidUtf8()Description copied from class:ByteString
Tells whether thisByteString
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.- Specified by:
isValidUtf8
in classByteString
- Returns:
- whether the bytes in this
ByteString
are a well-formed UTF-8 byte sequence
-
partialIsValidUtf8
protected int partialIsValidUtf8(int state, int offset, int length) Description copied from class:ByteString
Tells whether the given byte sequence is a well-formed, malformed, or incomplete UTF-8 byte sequence. This method accepts and returns a partial state result, allowing the bytes for a complete UTF-8 byte sequence to be composed from multipleByteString
segments.- Specified by:
partialIsValidUtf8
in classByteString
- Parameters:
state
- either0
(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- Returns:
-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.
-
equals
- Specified by:
equals
in classByteString
-
partialHash
protected int partialHash(int h, int offset, int length) Description copied from class:ByteString
Compute the hash across the value bytes starting with the given hash, and return the result. This is used to compute the hash across strings represented as a set of pieces by allowing the hash computation to be continued from piece to piece.- Specified by:
partialHash
in classByteString
- Parameters:
h
- starting hash valueoffset
- offset into this value to start looking at data valueslength
- number of data values to include in the hash computation- Returns:
- ending hash value
-
newInput
Description copied from class:ByteString
Creates anInputStream
which can be used to read the bytes.The
InputStream
returned by this method is guaranteed to be completely non-blocking. The methodInputStream.available()
returns the number of bytes remaining in the stream. The methodsInputStream.read(byte[])
,InputStream.read(byte[],int,int)
andInputStream.skip(long)
will read/skip as many bytes as are available. The methodInputStream.markSupported()
returnstrue
.The methods in the returned
InputStream
might not be thread safe.- Specified by:
newInput
in classByteString
- Returns:
- an input stream that returns the bytes of this byte string.
-
newCodedInput
Description copied from class:ByteString
Creates aCodedInputStream
which can be used to read the bytes. Using this is often more efficient than creating aCodedInputStream
that wraps the result ofByteString.newInput()
.- Specified by:
newCodedInput
in classByteString
- Returns:
- stream based on wrapped data
-
slice
Creates a slice of a range of this buffer.- Parameters:
beginIndex
- the beginning index of the slice (inclusive).endIndex
- the end index of the slice (exclusive).- Returns:
- the requested slice.
-