Class ByteString (3.19.4)

public abstract class ByteString implements Iterable<Byte>, Serializable

Immutable sequence of bytes. Provides conversions to and from byte[], java.lang.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.

Inheritance

Object > ByteString

Static Fields

EMPTY

public static final ByteString EMPTY

Empty ByteString.

Field Value
Type Description
ByteString

Static Methods

copyFrom(byte[] bytes)

public static ByteString copyFrom(byte[] bytes)

Copies the given bytes into a ByteString.

Parameter
Name Description
bytes byte[]

to copy

Returns
Type Description
ByteString

new ByteString

copyFrom(byte[] bytes, int offset, int size)

public static ByteString copyFrom(byte[] bytes, int offset, int size)

Copies the given bytes into a ByteString.

Parameters
Name Description
bytes byte[]

source array

offset int

offset in source array

size int

number of bytes to copy

Returns
Type Description
ByteString

new ByteString

copyFrom(Iterable<ByteString> byteStrings)

public static ByteString copyFrom(Iterable<ByteString> byteStrings)

Concatenates all byte strings in the iterable and returns the result. This is designed to run in O(list size), not O(total bytes).

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.