Top Description Inners Fields Constructors Methods
com.google.protobuf

public abstract Class ByteString

extends Object
implements Iterable<Byte>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Iterable
Known Direct Subclasses
com.google.protobuf.ByteString.LeafByteString, com.google.protobuf.RopeByteString
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .InputStream, .InvalidObjectException, .ObjectInputStream, .OutputStream, .Serializable, .UnsupportedEncodingException, java.nio.ByteBuffer, java.nio.charset.Charset, .UnsupportedCharsetException, java.util.ArrayList, .Arrays, .Collection, .Collections, .Comparator, .Iterator, .List, .NoSuchElementException

Immutable sequence of bytes. 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.

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.

Authors
[email protected] Bob Lee, [email protected] Kenton Varda, [email protected] Carl Haverl, [email protected] Martin Buchholz

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv abstract static class
private static class
ByteString.ArraysByteArrayCopier

Implementation of ByteArrayCopier which uses Arrays#copyOfRange.

private static class
ByteString.BoundedByteString

This class is used to represent the substring of a ByteString over a single byte array.

private static interface
ByteString.ByteArrayCopier

An interface to efficiently copy byte[].

public static interface
ByteString.ByteIterator

This interface extends Iterator<Byte>, so that we can return an unboxed byte.

pack-priv static class
pack-priv abstract static class
ByteString.LeafByteString

Base class for leaf ByteStrings (i.e. non-ropes).

private static class
ByteString.LiteralByteString

This class implements a com.google.protobuf.ByteString backed by a single array of bytes, contiguous in memory.

public static class
ByteString.Output

Outputs to a ByteString instance.

private static class
ByteString.SystemByteArrayCopier

Implementation of ByteArrayCopier which uses System#arraycopy.

Field Summary

Modifier and TypeField and Description
private static final ByteString.ByteArrayCopier
pack-priv static final int
CONCATENATE_BY_COPY_SIZE

When two strings to be concatenated have a combined length shorter than this, we just copy their bytes on concat(ByteString).

public static final ByteString
EMPTY

Empty ByteString.

private int
hash

Cached hash value.

pack-priv static final int
pack-priv static final int
MIN_READ_FROM_CHUNK_SIZE

When copying an InputStream into a ByteString with .readFrom(), the chunks in the underlying rope start at 256 bytes, but double each iteration up to 8192 bytes.

private static final int
private static final Comparator<ByteString>
UNSIGNED_LEXICOGRAPHICAL_COMPARATOR

Compares two ByteStrings lexicographically, treating their contents as unsigned byte values between 0 and 255 (inclusive).

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public abstract ByteBuffer

Returns:

wrapped bytes
asReadOnlyByteBuffer
()

Constructs a read-only java.nio.ByteBuffer whose content is equal to the contents of this byte string.

public abstract List<ByteBuffer>

Returns:

a list of wrapped bytes
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.

private static ByteString
balancedConcat(Iterator<ByteString> iterator, int length)

public abstract byte

Returns:

the value
byteAt
(int
index of byte
index
)

Gets the byte at the given index.

pack-priv static void
checkIndex(int
the index position to be tested
index
,
int
the length of the array
size
)

Checks that the given index falls within the specified array size.

pack-priv static int

Returns:

the length of the range.
checkRange
(int
the start index of the range (inclusive)
startIndex
,
int
the end index of the range (exclusive)
endIndex
,
int
the size of the array.
size
)

Checks that the given range falls within the bounds of an array

public final ByteString

Returns:

a new ByteString instance
concat
(ByteString
string to concatenate
other
)

Concatenate the given ByteString to this one.

public static ByteString

Returns:

new ByteString
copyFrom
(byte[]
source array
bytes
,
int
offset in source array
offset
,
int
number of bytes to copy
size
)

Copies the given bytes into a ByteString.

public static ByteString

Returns:

new ByteString
copyFrom
(byte[]
to copy
bytes
)

Copies the given bytes into a ByteString.

public static ByteString

Returns:

new ByteString
copyFrom
(ByteBuffer
source buffer
bytes
,
int
number of bytes to copy
size
)

Copies the next size bytes from a java.nio.ByteBuffer into a ByteString.

public static ByteString

Returns:

new ByteString
copyFrom
(ByteBuffer
sourceBuffer
bytes
)

Copies the remaining bytes from a java.nio.ByteBuffer into a ByteString.

public static ByteString

Returns:

new ByteString
copyFrom
(String
source string
text
,
String
encoding to use
charsetName
)

Encodes text into a sequence of bytes using the named charset and returns the result as a ByteString.

public static ByteString

Returns:

new ByteString
copyFrom
(String
source string
text
,
Charset
encode using this charset
charset
)

Encodes text into a sequence of bytes using the named charset and returns the result as a ByteString.

public static ByteString

Returns:

new ByteString
copyFrom
(Iterable<ByteString>
strings to be concatenated
byteStrings
)

Concatenates all byte strings in the iterable and returns the result.

public static ByteString

Returns:

new ByteString
copyFromUtf8
(String
source string
text
)

Encodes text into a sequence of UTF-8 bytes and returns the result as a ByteString.

public void
copyTo(byte[]
buffer to copy into
target
,
int
in the target buffer
offset
)

Copies bytes into a buffer at the given offset.

public final void
copyTo(byte[]
buffer to copy into
target
,
int
offset within these bytes
sourceOffset
,
int
offset within the target buffer
targetOffset
,
int
number of bytes to copy
numberToCopy
)

Deprecated Instead, call byteString.substring(sourceOffset, sourceOffset + numberToCopy).copyTo(target, targetOffset)
Copies bytes into a buffer.
public abstract void
copyTo(ByteBuffer
ByteBuffer to copy into.
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).

public final boolean

Returns:

true if the byte sequence represented by the argument is a suffix of the byte sequence represented by this string; false otherwise.
endsWith
(ByteString
the suffix.
suffix
)

Tests if this bytestring ends with the specified suffix.

public abstract boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
protected abstract int

Returns:

tree depth or zero
getTreeDepth
()

Return the depth of the tree representing this ByteString, if any, whose root is this node.

public final int

Returns:

hashCode value
hashCode
()

Overrides java.lang.Object.hashCode.

Compute the hashCode using the traditional algorithm from ByteString.
pack-priv abstract byte

Returns:

the value
internalByteAt
(int
index of byte
index
)

Gets the byte at the given index, assumes bounds checking has already been performed.

protected abstract boolean

Returns:

true if the tree is flat enough
isBalanced
()

Return true if this ByteString is literal (a leaf node) or a flat-enough tree in the sense of RopeByteString.

public final boolean

Returns:

true if this is zero bytes long
isEmpty
()

Returns true if the size is 0, false otherwise.

public abstract boolean

Returns:

whether the bytes in this ByteString are a well-formed UTF-8 byte sequence
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.

public ByteString.ByteIterator

Returns:

the iterator
iterator
()

Implements java.lang.Iterable.iterator.

Return a ByteString.ByteIterator over the bytes in the ByteString.
pack-priv static ByteString.CodedBuilder

Returns:

the builder
newCodedBuilder
(int
The target byte size of the ByteString. You must write exactly this many bytes before building the result.
size
)

Constructs a new ByteString builder, which allows you to efficiently construct a ByteString by writing to a CodedOutputStream.

public abstract CodedInputStream

Returns:

stream based on wrapped data
newCodedInput
()

Creates a CodedInputStream which can be used to read the bytes.

public abstract InputStream

Returns:

an input stream that returns the bytes of this byte string.
newInput
()

Creates an InputStream which can be used to read the bytes.

public static ByteString.Output

Returns:

OutputStream for building a ByteString
newOutput
(int
estimate of number of bytes to be written
initialCapacity
)

Creates a new Output with the given initial capacity.

public static ByteString.Output

Returns:

OutputStream for building a ByteString
newOutput
()

Creates a new Output.

protected abstract int

Returns:

ending hash value
partialHash
(int
starting hash value
h
,
int
offset into this value to start looking at data values
offset
,
int
number of data values to include in the hash computation
length
)

Compute the hash across the value bytes starting with the given hash, and return the result.

protected abstract int

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.
partialIsValidUtf8
(int
either 0 (if this is the initial decoding operation) or the value returned from a call to a partial decoding method for the previous bytes
state
,
int
offset of the first byte to check
offset
,
int
number of bytes to check
length
)

Tells whether the given byte sequence is a well-formed, malformed, or incomplete UTF-8 byte sequence.

protected final int

Returns:

value of cached hash code or 0 if not computed yet
peekCachedHashCode
()

Return the cached hash code if available.

private static ByteString

Returns:

A chunk of the desired size, or else a chunk as large as was available when end of stream was reached. Returns null if the given stream had no more data in it.
readChunk
(InputStream in, final int chunkSize)

Blocks until a chunk of the given size can be made from the stream, or EOF is reached.

public static ByteString

Returns:

A new ByteString which is made up of chunks of various sizes, depending on the behavior of the underlying stream.
readFrom
(InputStream
The source stream, which is read completely but not closed.
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.

public static ByteString

Returns:

A new ByteString which is made up of chunks of the given size.
readFrom
(InputStream
The source stream, which is read completely but not closed.
streamToDrain
,
int
The size of the chunks in which to read the stream.
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.

public static ByteString
readFrom(InputStream streamToDrain, int minChunkSize, int maxChunkSize)

public abstract int

Returns:

size in bytes
size
()

Gets the number of bytes.

public final boolean

Returns:

true if the byte sequence represented by the argument is a prefix of the byte sequence represented by this string; false otherwise.
startsWith
(ByteString
the prefix.
prefix
)

Tests if this bytestring starts with the specified prefix.

public final ByteString

Returns:

substring sharing underlying data
substring
(int
start at this index
beginIndex
)

Return the substring from beginIndex, inclusive, to the end of the string.

public abstract ByteString

Returns:

substring sharing underlying data
substring
(int
start at this index
beginIndex
,
int
the last character is the one before this index
endIndex
)

Return the substring from beginIndex, inclusive, to endIndex, exclusive.

public final byte[]

Returns:

copied bytes
toByteArray
()

Copies bytes to a byte[].

private static int
toInt(byte value)

Returns the value of the given byte as an integer, interpreting the byte as an unsigned value.

public final String

Returns:

new string
toString
(String
encode using this charset
charsetName
)

Constructs a new String by decoding the bytes using the specified charset.

public final String

Returns:

new string
toString
(Charset
encode using this charset
charset
)

Constructs a new String by decoding the bytes using the specified charset.

public final String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
protected abstract String

Returns:

new string
toStringInternal
(Charset
encode using this charset
charset
)

Constructs a new String by decoding the bytes using the specified charset.

public final String

Returns:

new string using UTF-8 encoding
toStringUtf8
()

Constructs a new String by decoding the bytes as UTF-8.

public static Comparator<ByteString>
unsignedLexicographicalComparator()

Returns a Comparator which compares ByteString-s lexicographically as sequences of unsigned bytes (i.e. values between 0 and 255, inclusive).

pack-priv static ByteString
wrap(ByteBuffer buffer)

Wraps the given bytes into a ByteString.

pack-priv static ByteString
wrap(byte[] bytes)

Wraps the given bytes into a ByteString.

pack-priv static ByteString
wrap(byte[] bytes, int offset, int length)

Wraps the given bytes into a ByteString.

public abstract void
writeTo(OutputStream
the output stream to which to write the data.
out
)

Writes a copy of the contents of this byte string to the specified output stream argument.

pack-priv final void
writeTo(OutputStream
the output stream to which to write the data.
out
,
int
offset within these bytes
sourceOffset
,
int
number of bytes to write
numberToWrite
)

Writes a specified part of this byte string to an output stream.

pack-priv abstract void
writeTo(ByteOutput
the output target to receive the bytes
byteOutput
)

Writes this ByteString to the provided ByteOutput.

pack-priv abstract void
writeToInternal(OutputStream out, int sourceOffset, int numberToWrite)

Internal version of writeTo(OutputStream, int, int) that assumes all error checking has already been done.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait