Class ByteString.BoundedByteString
- All Implemented Interfaces:
Serializable, Iterable<Byte>
- Enclosing class:
ByteString
ByteString over a single byte array.
In terms of the public API of ByteString, you end up here by calling ByteString.copyFrom(byte[]) followed by ByteString.substring(int, int).
This class contains most of the overhead involved in creating a substring from a ByteString.LiteralByteString. The overhead involves some range-checking and two extra fields.
-
Nested Class Summary
Nested classes/interfaces inherited from class ByteString
ByteString.AbstractByteIterator, ByteString.ByteIterator, ByteString.CodedBuilder, ByteString.LeafByteString, ByteString.Output -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]private final intprivate final intprivate static final longFields inherited from class ByteString
CONCATENATE_BY_COPY_SIZE, EMPTY, MAX_READ_FROM_CHUNK_SIZE, MIN_READ_FROM_CHUNK_SIZE -
Constructor Summary
ConstructorsConstructorDescriptionBoundedByteString(byte[] bytes, int offset, int length) Creates aBoundedByteStringbacked by the sub-range of given array, without copying. -
Method Summary
Modifier and TypeMethodDescriptionConstructs a read-onlyjava.nio.ByteBufferwhose content is equal to the contents of this byte string.Constructs a list of read-onlyjava.nio.ByteBufferobjects such that the concatenation of their contents is equal to the contents of this byte string.bytebyteAt(int index) Gets the byte at the given index.voidcopyTo(ByteBuffer target) Copies bytes into a ByteBuffer.protected voidcopyToInternal(byte[] target, int sourceOffset, int targetOffset, int numberToCopy) Internal (package private) implementation ofByteString.copyTo(byte[],int,int,int).protected booleanequalsInternal(ByteString other) Internal portion of the equals check: as a precondition the caller has already checked most fast properties that are common (including reference identity, null, size, cached hash codes if available) are the same.(package private) booleanequalsRange(ByteString other, int offset, int length) Check equality of the substring of given length of this object starting at zero with anotherByteStringsubstring starting at offset.(package private) byteinternalByteAt(int index) Gets the byte at the given index, assumes bounds checking has already been performed.booleanTells whether thisByteStringrepresents 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 aCodedInputStreamwhich can be used to read the bytes.newInput()Creates anInputStreamwhich can be used to read the bytes.protected intpartialHash(int h, int offset, int length) Compute the hash across the value bytes starting with the given hash, and return the result.private voidintsize()Gets the number of bytes.substring(int beginIndex, int endIndex) Return the substring frombeginIndex, inclusive, toendIndex, exclusive.protected StringtoStringInternal(Charset charset) Constructs a newStringby decoding the bytes using the specified charset.(package private) Object(package private) voidwriteTo(ByteOutput out) Writes thisByteStringto the providedByteOutput.voidwriteTo(OutputStream out) Writes a copy of the contents of this byte string to the specified output stream argument.(package private) voidwriteToInternal(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 ByteString.LeafByteString
getTreeDepth, isBalanced, writeToReverseMethods inherited from class ByteString
checkIndex, checkRange, concat, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, copyFromUtf8, copyTo, copyTo, empty, endsWith, equals, fromHex, hashCode, isEmpty, iterator, newCodedBuilder, newOutput, newOutput, peekCachedHashCode, readFrom, readFrom, readFrom, startsWith, substring, toByteArray, toString, toString, toString, toStringUtf8, unsignedLexicographicalComparator, wrap, wrap, wrap, wrap, wrap, wrap, writeToMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
bytes
private final byte[] bytes -
offset
private final int offset -
length
private final int length -
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
BoundedByteString
BoundedByteString(byte[] bytes, int offset, int length) Creates aBoundedByteStringbacked by the sub-range of given array, without copying.- Parameters:
bytes- array to wrapoffset- index to first byte to use in byteslength- number of bytes to use from bytes- Throws:
IllegalArgumentException- ifoffset < 0,length < 0, or ifoffset + length > bytes.length.
-
-
Method Details
-
byteAt
public byte byteAt(int index) Gets the byte at the given index. ThrowsArrayIndexOutOfBoundsExceptionfor backwards-compatibility reasons although it would more properly beIndexOutOfBoundsException.- Specified by:
byteAtin classByteString- Parameters:
index- index of byte- Returns:
- the value
- Throws:
ArrayIndexOutOfBoundsException-indexis invalid input: '<' 0 or >= size
-
internalByteAt
byte internalByteAt(int index) Description copied from class:ByteStringGets the byte at the given index, assumes bounds checking has already been performed.- Specified by:
internalByteAtin classByteString- Parameters:
index- index of byte- Returns:
- the value
-
size
public int size()Description copied from class:ByteStringGets the number of bytes.- Specified by:
sizein classByteString- Returns:
- size in bytes
-
substring
Description copied from class:ByteStringReturn the substring frombeginIndex, inclusive, toendIndex, exclusive.- Specified by:
substringin 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:ByteStringInternal (package private) implementation ofByteString.copyTo(byte[],int,int,int). It assumes that all error checking has already been performed and thatnumberToCopy > 0.- Specified by:
copyToInternalin classByteString
-
copyTo
Description copied from class:ByteStringCopies 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:
copyToin classByteString- Parameters:
target- ByteBuffer to copy into.
-
asReadOnlyByteBuffer
Description copied from class:ByteStringConstructs a read-onlyjava.nio.ByteBufferwhose 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:
asReadOnlyByteBufferin classByteString- Returns:
- wrapped bytes
-
asReadOnlyByteBufferList
Description copied from class:ByteStringConstructs a list of read-onlyjava.nio.ByteBufferobjects 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:
asReadOnlyByteBufferListin classByteString- Returns:
- a list of wrapped bytes
-
writeTo
Description copied from class:ByteStringWrites a copy of the contents of this byte string to the specified output stream argument.- Specified by:
writeToin classByteString- Parameters:
out- the output stream to which to write the data.- Throws:
IOException- if an I/O error occurs.
-
writeTo
Description copied from class:ByteStringWrites thisByteStringto the providedByteOutput. Calling this method may result in multiple operations on the targetByteOutput.This method may expose internal backing buffers of the
ByteStringto theByteOutputin order to avoid additional copying overhead. It would be possible for a maliciousByteOutputto corrupt theByteString. Use with caution!- Specified by:
writeToin classByteString- Parameters:
out- the output target to receive the bytes- Throws:
IOException- if an I/O error occurs- See Also:
-
writeToInternal
Description copied from class:ByteStringInternal version ofByteString.writeTo(OutputStream,int,int)that assumes all error checking has already been done.- Specified by:
writeToInternalin classByteString- Throws:
IOException
-
toStringInternal
Description copied from class:ByteStringConstructs a newStringby decoding the bytes using the specified charset.- Specified by:
toStringInternalin classByteString- Parameters:
charset- encode using this charset- Returns:
- new string
-
isValidUtf8
public boolean isValidUtf8()Description copied from class:ByteStringTells whether thisByteStringrepresents 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
truewhenever:Arrays.equals(byteString.toByteArray(), new String(byteString.toByteArray(), UTF_8).getBytes(UTF_8))This method returns
falsefor "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, and Table 3-7 Well Formed UTF-8 Byte Sequences.
- Specified by:
isValidUtf8in classByteString- Returns:
- whether the bytes in this
ByteStringare a well-formed UTF-8 byte sequence
-
equalsInternal
Description copied from class:ByteStringInternal portion of the equals check: as a precondition the caller has already checked most fast properties that are common (including reference identity, null, size, cached hash codes if available) are the same. This method is when we need to check the actual string contents from there.- Specified by:
equalsInternalin classByteString
-
equalsRange
Description copied from class:ByteString.LeafByteStringCheck equality of the substring of given length of this object starting at zero with anotherByteStringsubstring starting at offset.- Specified by:
equalsRangein 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.
-
partialHash
protected int partialHash(int h, int offset, int length) Description copied from class:ByteStringCompute 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:
partialHashin 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:ByteStringCreates anInputStreamwhich can be used to read the bytes.The
InputStreamreturned 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
InputStreammight not be thread safe.- Specified by:
newInputin classByteString- Returns:
- an input stream that returns the bytes of this byte string.
-
newCodedInput
Description copied from class:ByteStringCreates aCodedInputStreamwhich can be used to read the bytes. Using this is often more efficient than creating aCodedInputStreamthat wraps the result ofByteString.newInput().- Specified by:
newCodedInputin classByteString- Returns:
- stream based on wrapped data
-
writeReplace
Object writeReplace() -
readObject
- Throws:
IOException
-