Class ByteString (3.19.4) Stay organized with collections Save and categorize content based on your preferences.
publicabstractclassByteStringimplementsIterable<Byte>,SerializableImmutable sequence of bytes. Provides conversions to and frombyte[],java.lang.String,ByteBuffer,InputStream,OutputStream. Also provides a conversion toCodedInputStream.
LikeString, the contents of aByteString 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 inRopeByteString.
Inherited Members
Static Fields
EMPTY
publicstaticfinalByteStringEMPTYEmptyByteString.
| Field Value | |
|---|---|
| Type | Description |
ByteString | |
Static Methods
copyFrom(byte[] bytes)
publicstaticByteStringcopyFrom(byte[]bytes)Copies the given bytes into aByteString.
| Parameter | |
|---|---|
| Name | Description |
bytes | byte[]to copy |
| Returns | |
|---|---|
| Type | Description |
ByteString | new |
copyFrom(byte[] bytes, int offset, int size)
publicstaticByteStringcopyFrom(byte[]bytes,intoffset,intsize)Copies the given bytes into aByteString.
| Parameters | |
|---|---|
| Name | Description |
bytes | byte[]source array |
offset | intoffset in source array |
size | intnumber of bytes to copy |
| Returns | |
|---|---|
| Type | Description |
ByteString | new |
copyFrom(Iterable<ByteString> byteStrings)
publicstaticByteStringcopyFrom(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 returnedByteString is not necessarily a unique object. If the list is empty, the returned object is the singleton emptyByteString. If the list has only one element, thatByteString will be returned without copying.
| Parameter | |
|---|---|
| Name | Description |
byteStrings | Iterable<ByteString>strings to be concatenated |
| Returns | |
|---|---|
| Type | Description |
ByteString | new |
copyFrom(String text, String charsetName)
publicstaticByteStringcopyFrom(Stringtext,StringcharsetName)Encodestext into a sequence of bytes using the named charset and returns the result as aByteString.
| Parameters | |
|---|---|
| Name | Description |
text | Stringsource string |
charsetName | Stringencoding to use |
| Returns | |
|---|---|
| Type | Description |
ByteString | new |
| Exceptions | |
|---|---|
| Type | Description |
UnsupportedEncodingException | if the encoding isn't found |
copyFrom(String text, Charset charset)
publicstaticByteStringcopyFrom(Stringtext,Charsetcharset)Encodestext into a sequence of bytes using the named charset and returns the result as aByteString.
| Parameters | |
|---|---|
| Name | Description |
text | Stringsource string |
charset | Charsetencode using this charset |
| Returns | |
|---|---|
| Type | Description |
ByteString | new |
copyFrom(ByteBuffer bytes)
publicstaticByteStringcopyFrom(ByteBufferbytes)Copies the remaining bytes from ajava.nio.ByteBuffer into aByteString.
| Parameter | |
|---|---|
| Name | Description |
bytes | ByteBuffersourceBuffer |
| Returns | |
|---|---|
| Type | Description |
ByteString | new |
copyFrom(ByteBuffer bytes, int size)
publicstaticByteStringcopyFrom(ByteBufferbytes,intsize)Copies the nextsize bytes from ajava.nio.ByteBuffer into a ByteString.
| Parameters | |
|---|---|
| Name | Description |
bytes | ByteBuffersource buffer |
size | intnumber of bytes to copy |
| Returns | |
|---|---|
| Type | Description |
ByteString | new |
copyFromUtf8(String text)
publicstaticByteStringcopyFromUtf8(Stringtext)Encodestext into a sequence of UTF-8 bytes and returns the result as a ByteString.
| Parameter | |
|---|---|
| Name | Description |
text | Stringsource string |
| Returns | |
|---|---|
| Type | Description |
ByteString | new |
newOutput()
publicstaticByteString.OutputnewOutput()Creates a newOutput. CallOutput#toByteString() to create the ByteString instance.
AByteString.Output offers the same functionality as aByteArrayOutputStream, except that it returns aByteString rather than abyte array.
| Returns | |
|---|---|
| Type | Description |
ByteString.Output |
|
newOutput(int initialCapacity)
publicstaticByteString.OutputnewOutput(intinitialCapacity)Creates a newOutput with the given initial capacity. CallOutput#toByteString() to create theByteString instance.
AByteString.Output offers the same functionality as aByteArrayOutputStream, except that it returns aByteString rather than abyte array.
| Parameter | |
|---|---|
| Name | Description |
initialCapacity | intestimate of number of bytes to be written |
| Returns | |
|---|---|
| Type | Description |
ByteString.Output |
|
readFrom(InputStream streamToDrain)
publicstaticByteStringreadFrom(InputStreamstreamToDrain)Completely reads the given stream's bytes into aByteString, blocking if necessary until all bytes are read through to the end of the stream.
Performance notes: The returnedByteString is an immutable tree of byte arrays ("chunks") of the stream data. The first chunk is small, with subsequent chunks each being double the size, up to 8K.
Each byte read from the input stream will be copied twice to ensure that the resulting ByteString is truly immutable.
| Parameter | |
|---|---|
| Name | Description |
streamToDrain | InputStreamThe source stream, which is read completely but not closed. |
| Returns | |
|---|---|
| Type | Description |
ByteString | A new |
| Exceptions | |
|---|---|
| Type | Description |
IOException | if there is a problem reading the underlying stream |
readFrom(InputStream streamToDrain, int chunkSize)
publicstaticByteStringreadFrom(InputStreamstreamToDrain,intchunkSize)Completely reads the given stream's bytes into aByteString, blocking if necessary until all bytes are read through to the end of the stream.
Performance notes: The returnedByteString is an immutable tree of byte arrays ("chunks") of the stream data. The chunkSize parameter sets the size of these byte arrays.
Each byte read from the input stream will be copied twice to ensure that the resulting ByteString is truly immutable.
| Parameters | |
|---|---|
| Name | Description |
streamToDrain | InputStreamThe source stream, which is read completely but not closed. |
chunkSize | intThe size of the chunks in which to read the stream. |
| Returns | |
|---|---|
| Type | Description |
ByteString | A new |
| Exceptions | |
|---|---|
| Type | Description |
IOException | if there is a problem reading the underlying stream |
readFrom(InputStream streamToDrain, int minChunkSize, int maxChunkSize)
publicstaticByteStringreadFrom(InputStreamstreamToDrain,intminChunkSize,intmaxChunkSize)Helper method that takes the chunk size range as a parameter.
| Parameters | |
|---|---|
| Name | Description |
streamToDrain | InputStreamthe source stream, which is read completely but not closed |
minChunkSize | intthe minimum size of the chunks in which to read the stream |
maxChunkSize | intthe maximum size of the chunks in which to read the stream |
| Returns | |
|---|---|
| Type | Description |
ByteString | a new |
| Exceptions | |
|---|---|
| Type | Description |
IOException | if there is a problem reading the underlying stream |
unsignedLexicographicalComparator()
publicstaticComparator<ByteString>unsignedLexicographicalComparator()Returns aComparator which comparesByteString-s lexicographically as sequences of unsigned bytes (i.e. values between 0 and 255, inclusive).
For example,(byte) -1 is considered to be greater than(byte) 1 because it is interpreted as an unsigned value,255:
-1-> 0b11111111 (two's complement) -> 2551-> 0b00000001 -> 1
| Returns | |
|---|---|
| Type | Description |
Comparator<ByteString> | |
Methods
asReadOnlyByteBuffer()
publicabstractByteBufferasReadOnlyByteBuffer()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.
| Returns | |
|---|---|
| Type | Description |
ByteBuffer | wrapped bytes |
asReadOnlyByteBufferList()
publicabstractList<ByteBuffer>asReadOnlyByteBufferList()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.
| Returns | |
|---|---|
| Type | Description |
List<ByteBuffer> | a list of wrapped bytes |
byteAt(int index)
publicabstractbytebyteAt(intindex)Gets the byte at the given index. This method should be used only for random access to individual bytes. To access bytes sequentially, use theByteIterator returned by#iterator(), and call#substring(int, int) first if necessary.
| Parameter | |
|---|---|
| Name | Description |
index | intindex of byte |
| Returns | |
|---|---|
| Type | Description |
byte | the value |
concat(ByteString other)
publicfinalByteStringconcat(ByteStringother)Concatenate the givenByteString to this one. Short concatenations, of total size smaller thanByteString#CONCATENATE_BY_COPY_SIZE, are produced by copying the underlying bytes (as per Rope.java, BAP95. In general, the concatenate involves no copying.
| Parameter | |
|---|---|
| Name | Description |
other | ByteStringstring to concatenate |
| Returns | |
|---|---|
| Type | Description |
ByteString | a new |
copyTo(byte[] target, int offset)
publicvoidcopyTo(byte[]target,intoffset)Copies bytes into a buffer at the given offset.
To copy a subset of bytes, you call this method on the return value of#substring(int, int). Example:byteString.substring(start, end).copyTo(target, offset)
| Parameters | |
|---|---|
| Name | Description |
target | byte[]buffer to copy into |
offset | intin the target buffer |
copyTo(byte[] target, int sourceOffset, int targetOffset, int numberToCopy) (deprecated)
publicfinalvoidcopyTo(byte[]target,intsourceOffset,inttargetOffset,intnumberToCopy)Deprecated.Instead, callbyteString.substring(sourceOffset, sourceOffset + numberToCopy).copyTo(target, targetOffset)
Copies bytes into a buffer.
| Parameters | |
|---|---|
| Name | Description |
target | byte[]buffer to copy into |
sourceOffset | intoffset within these bytes |
targetOffset | intoffset within the target buffer |
numberToCopy | intnumber of bytes to copy |
copyTo(ByteBuffer target)
publicabstractvoidcopyTo(ByteBuffertarget)Copies bytes into a ByteBuffer.
To copy a subset of bytes, you call this method on the return value of#substring(int, int). Example:byteString.substring(start, end).copyTo(target)
| Parameter | |
|---|---|
| Name | Description |
target | ByteBufferByteBuffer to copy into. |
copyToInternal(byte[] target, int sourceOffset, int targetOffset, int numberToCopy)
protectedabstractvoidcopyToInternal(byte[]target,intsourceOffset,inttargetOffset,intnumberToCopy)Internal (package private) implementation of#copyTo(byte[],int,int,int). It assumes that all error checking has already been performed and thatnumberToCopy > 0.
| Parameters | |
|---|---|
| Name | Description |
target | byte[] |
sourceOffset | int |
targetOffset | int |
numberToCopy | int |
endsWith(ByteString suffix)
publicfinalbooleanendsWith(ByteStringsuffix)Tests if this bytestring ends with the specified suffix. Similar toString#endsWith(String)
| Parameter | |
|---|---|
| Name | Description |
suffix | ByteStringthe suffix. |
| Returns | |
|---|---|
| Type | Description |
boolean |
|
equals(Object o)
publicabstractbooleanequals(Objecto)| Parameter | |
|---|---|
| Name | Description |
o | Object |
| Returns | |
|---|---|
| Type | Description |
boolean | |
getTreeDepth()
protectedabstractintgetTreeDepth()Return the depth of the tree representing thisByteString, if any, whose root is this node. If this is a leaf node, return 0.
| Returns | |
|---|---|
| Type | Description |
int | tree depth or zero |
hashCode()
publicfinalinthashCode()Compute the hashCode using the traditional algorithm fromByteString.
| Returns | |
|---|---|
| Type | Description |
int | hashCode value |
isBalanced()
protectedabstractbooleanisBalanced()Returntrue if this ByteString is literal (a leaf node) or a flat-enough tree in the sense ofRopeByteString.
| Returns | |
|---|---|
| Type | Description |
boolean | true if the tree is flat enough |
isEmpty()
publicfinalbooleanisEmpty()Returnstrue if the size is0,false otherwise.
| Returns | |
|---|---|
| Type | Description |
boolean | true if this is zero bytes long |
isValidUtf8()
publicabstractbooleanisValidUtf8()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, returnstrue whenever:
Arrays.equals(byteString.toByteArray(),newString(byteString.toByteArray(),"UTF-8").getBytes("UTF-8"))This method returnsfalse 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.
| Returns | |
|---|---|
| Type | Description |
boolean | whether the bytes in this |
iterator()
publicByteString.ByteIteratoriterator()Return aByteString.ByteIterator over the bytes in the ByteString. To avoid auto-boxing, you may get the iterator manually and callByteIterator#nextByte().
| Returns | |
|---|---|
| Type | Description |
ByteString.ByteIterator | the iterator |
newCodedInput()
publicabstractCodedInputStreamnewCodedInput()Creates aCodedInputStream which can be used to read the bytes. Using this is often more efficient than creating aCodedInputStream that wraps the result of#newInput().
| Returns | |
|---|---|
| Type | Description |
CodedInputStream | stream based on wrapped data |
newInput()
publicabstractInputStreamnewInput()Creates anInputStream which can be used to read the bytes.
TheInputStream 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 returnedInputStream mightnot be thread safe.
| Returns | |
|---|---|
| Type | Description |
InputStream | an input stream that returns the bytes of this byte string. |
partialHash(int h, int offset, int length)
protectedabstractintpartialHash(inth,intoffset,intlength)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.
| Parameters | |
|---|---|
| Name | Description |
h | intstarting hash value |
offset | intoffset into this value to start looking at data values |
length | intnumber of data values to include in the hash computation |
| Returns | |
|---|---|
| Type | Description |
int | ending hash value |
partialIsValidUtf8(int state, int offset, int length)
protectedabstractintpartialIsValidUtf8(intstate,intoffset,intlength)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.
| Parameters | |
|---|---|
| Name | Description |
state | inteither |
offset | intoffset of the first byte to check |
length | intnumber of bytes to check |
| Returns | |
|---|---|
| Type | Description |
int |
|
peekCachedHashCode()
protectedfinalintpeekCachedHashCode()Return the cached hash code if available.
| Returns | |
|---|---|
| Type | Description |
int | value of cached hash code or 0 if not computed yet |
size()
publicabstractintsize()Gets the number of bytes.
| Returns | |
|---|---|
| Type | Description |
int | size in bytes |
startsWith(ByteString prefix)
publicfinalbooleanstartsWith(ByteStringprefix)Tests if this bytestring starts with the specified prefix. Similar toString#startsWith(String)
| Parameter | |
|---|---|
| Name | Description |
prefix | ByteStringthe prefix. |
| Returns | |
|---|---|
| Type | Description |
boolean |
|
substring(int beginIndex)
publicfinalByteStringsubstring(intbeginIndex)Return the substring frombeginIndex, inclusive, to the end of the string.
| Parameter | |
|---|---|
| Name | Description |
beginIndex | intstart at this index |
| Returns | |
|---|---|
| Type | Description |
ByteString | substring sharing underlying data |
substring(int beginIndex, int endIndex)
publicabstractByteStringsubstring(intbeginIndex,intendIndex)Return the substring frombeginIndex, inclusive, toendIndex, exclusive.
| Parameters | |
|---|---|
| Name | Description |
beginIndex | intstart at this index |
endIndex | intthe last character is the one before this index |
| Returns | |
|---|---|
| Type | Description |
ByteString | substring sharing underlying data |
toByteArray()
publicfinalbyte[]toByteArray()Copies bytes to abyte[].
| Returns | |
|---|---|
| Type | Description |
byte[] | copied bytes |
toString()
publicfinalStringtoString()| Returns | |
|---|---|
| Type | Description |
String | |
toString(String charsetName)
publicfinalStringtoString(StringcharsetName)Constructs a newString by decoding the bytes using the specified charset.
| Parameter | |
|---|---|
| Name | Description |
charsetName | Stringencode using this charset |
| Returns | |
|---|---|
| Type | Description |
String | new string |
| Exceptions | |
|---|---|
| Type | Description |
UnsupportedEncodingException | if charset isn't recognized |
toString(Charset charset)
publicfinalStringtoString(Charsetcharset)Constructs a newString by decoding the bytes using the specified charset. Returns the same empty String if empty.
| Parameter | |
|---|---|
| Name | Description |
charset | Charsetencode using this charset |
| Returns | |
|---|---|
| Type | Description |
String | new string |
toStringInternal(Charset charset)
protectedabstractStringtoStringInternal(Charsetcharset)Constructs a newString by decoding the bytes using the specified charset.
| Parameter | |
|---|---|
| Name | Description |
charset | Charsetencode using this charset |
| Returns | |
|---|---|
| Type | Description |
String | new string |
toStringUtf8()
publicfinalStringtoStringUtf8()Constructs a newString by decoding the bytes as UTF-8.
| Returns | |
|---|---|
| Type | Description |
String | new string using UTF-8 encoding |
writeTo(OutputStream out)
publicabstractvoidwriteTo(OutputStreamout)Writes a copy of the contents of this byte string to the specified output stream argument.
| Parameter | |
|---|---|
| Name | Description |
out | OutputStreamthe output stream to which to write the data. |
| Exceptions | |
|---|---|
| Type | Description |
IOException | if an I/O error occurs. |
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-10-30 UTC.