final class SegmentedByteString extends ByteString
The first half of the directory is the cumulative byte count covered by each segment. The
element at directory[0] contains the number of bytes held in segments[0]; the
element at directory[1] contains the number of bytes held in segments[0] +
segments[1], and so on. The element at directory[segments.length - 1] contains the total
size of this byte string. The first half of the directory is always monotonically increasing.
The second half of the directory is the offset in segments of the first content byte.
Bytes preceding this offset are unused, as are bytes beyond the segment's effective size.
Suppose we have a byte string, [A, B, C, D, E, F, G, H, I, J, K, L, M] that is stored
across three byte arrays: [x, x, x, x, A, B, C, D, E, x, x, x], [x, F, G], and
[H, I, J, K, L, M, x, x, x, x, x, x]. The three byte arrays would be stored in segments in order. Since the arrays contribute 5, 2, and 6 elements respectively, the directory
starts with [5, 7, 13 to hold the cumulative total at each position. Since the offsets
into the arrays are 4, 1, and 0 respectively, the directory ends with 4, 1, 0].
Concatenating these two halves, the complete directory is [5, 7, 13, 4, 1, 0].
This structure is chosen so that the segment holding a particular offset can be found by binary search. We use one array rather than two for the directory as a micro-optimization.
| Modifier and Type | Field and Description |
|---|---|
(package private) int[] |
directory |
(package private) byte[][] |
segments |
data, EMPTY, hashCode, HEX_DIGITS, utf8| Constructor and Description |
|---|
SegmentedByteString(Buffer buffer,
int byteCount) |
| Modifier and Type | Method and Description |
|---|---|
java.nio.ByteBuffer |
asByteBuffer()
Returns a
ByteBuffer view of the bytes in this ByteString. |
java.lang.String |
base64()
Returns this byte string encoded as Base64.
|
java.lang.String |
base64Url()
Returns this byte string encoded as URL-safe
Base64.
|
boolean |
equals(java.lang.Object o) |
byte |
getByte(int pos)
Returns the byte at
pos. |
int |
hashCode() |
java.lang.String |
hex()
Returns this byte string encoded in hexadecimal.
|
ByteString |
hmacSha1(ByteString key)
Returns the 160-bit SHA-1 HMAC of this byte string.
|
ByteString |
hmacSha256(ByteString key)
Returns the 256-bit SHA-256 HMAC of this byte string.
|
int |
indexOf(byte[] other,
int fromIndex) |
(package private) byte[] |
internalArray()
Returns the bytes of this string without a defensive copy.
|
int |
lastIndexOf(byte[] other,
int fromIndex) |
ByteString |
md5()
Returns the 128-bit MD5 hash of this byte string.
|
boolean |
rangeEquals(int offset,
byte[] other,
int otherOffset,
int byteCount)
Returns true if the bytes of this in
[offset..offset+byteCount) equal the bytes of
other in [otherOffset..otherOffset+byteCount). |
boolean |
rangeEquals(int offset,
ByteString other,
int otherOffset,
int byteCount)
Returns true if the bytes of this in
[offset..offset+byteCount) equal the bytes of
other in [otherOffset..otherOffset+byteCount). |
private int |
segment(int pos)
Returns the index of the segment that contains the byte at
pos. |
ByteString |
sha1()
Returns the 160-bit SHA-1 hash of this byte string.
|
ByteString |
sha256()
Returns the 256-bit SHA-256 hash of this byte string.
|
int |
size()
Returns the number of bytes in this ByteString.
|
java.lang.String |
string(java.nio.charset.Charset charset)
Constructs a new
String by decoding the bytes using charset. |
ByteString |
substring(int beginIndex)
Returns a byte string that is a substring of this byte string, beginning at the specified
index until the end of this string.
|
ByteString |
substring(int beginIndex,
int endIndex)
Returns a byte string that is a substring of this byte string, beginning at the specified
beginIndex and ends at the specified endIndex. |
ByteString |
toAsciiLowercase()
Returns a byte string equal to this byte string, but with the bytes 'A'
through 'Z' replaced with the corresponding byte in 'a' through 'z'.
|
ByteString |
toAsciiUppercase()
Returns a byte string equal to this byte string, but with the bytes 'a'
through 'z' replaced with the corresponding byte in 'A' through 'Z'.
|
byte[] |
toByteArray()
Returns a byte array containing a copy of the bytes in this
ByteString. |
private ByteString |
toByteString()
Returns a copy as a non-segmented byte string.
|
java.lang.String |
toString()
Returns a human-readable string that describes the contents of this byte string.
|
java.lang.String |
utf8()
Constructs a new
String by decoding the bytes as UTF-8. |
(package private) void |
write(Buffer buffer)
Writes the contents of this byte string to
buffer. |
void |
write(java.io.OutputStream out)
Writes the contents of this byte string to
out. |
private java.lang.Object |
writeReplace() |
codePointIndexToCharIndex, compareTo, decodeBase64, decodeHex, encodeString, encodeUtf8, endsWith, endsWith, hmacSha512, indexOf, indexOf, indexOf, lastIndexOf, lastIndexOf, lastIndexOf, of, of, of, read, sha512, startsWith, startsWithSegmentedByteString(Buffer buffer, int byteCount)
public java.lang.String utf8()
ByteStringString by decoding the bytes as UTF-8.utf8 in class ByteStringpublic java.lang.String string(java.nio.charset.Charset charset)
ByteStringString by decoding the bytes using charset.string in class ByteStringpublic java.lang.String base64()
ByteStringbase64 in class ByteStringpublic java.lang.String hex()
ByteStringhex in class ByteStringpublic ByteString toAsciiLowercase()
ByteStringtoAsciiLowercase in class ByteStringpublic ByteString toAsciiUppercase()
ByteStringtoAsciiUppercase in class ByteStringpublic ByteString md5()
ByteStringmd5 in class ByteStringpublic ByteString sha1()
ByteStringsha1 in class ByteStringpublic ByteString sha256()
ByteStringsha256 in class ByteStringpublic ByteString hmacSha1(ByteString key)
ByteStringhmacSha1 in class ByteStringpublic ByteString hmacSha256(ByteString key)
ByteStringhmacSha256 in class ByteStringpublic java.lang.String base64Url()
ByteStringbase64Url in class ByteStringpublic ByteString substring(int beginIndex)
ByteStringbeginIndex is 0.substring in class ByteStringpublic ByteString substring(int beginIndex, int endIndex)
ByteStringbeginIndex and ends at the specified endIndex. Returns this byte string if
beginIndex is 0 and endIndex is the length of this byte string.substring in class ByteStringpublic byte getByte(int pos)
ByteStringpos.getByte in class ByteStringprivate int segment(int pos)
pos.public int size()
ByteStringsize in class ByteStringpublic byte[] toByteArray()
ByteStringByteString.toByteArray in class ByteStringpublic java.nio.ByteBuffer asByteBuffer()
ByteStringByteBuffer view of the bytes in this ByteString.asByteBuffer in class ByteStringpublic void write(java.io.OutputStream out)
throws java.io.IOException
ByteStringout.write in class ByteStringjava.io.IOExceptionvoid write(Buffer buffer)
ByteStringbuffer.write in class ByteStringpublic boolean rangeEquals(int offset,
ByteString other,
int otherOffset,
int byteCount)
ByteString[offset..offset+byteCount) equal the bytes of
other in [otherOffset..otherOffset+byteCount). Returns false if either range is
out of bounds.rangeEquals in class ByteStringpublic boolean rangeEquals(int offset,
byte[] other,
int otherOffset,
int byteCount)
ByteString[offset..offset+byteCount) equal the bytes of
other in [otherOffset..otherOffset+byteCount). Returns false if either range is
out of bounds.rangeEquals in class ByteStringpublic int indexOf(byte[] other,
int fromIndex)
indexOf in class ByteStringpublic int lastIndexOf(byte[] other,
int fromIndex)
lastIndexOf in class ByteStringprivate ByteString toByteString()
byte[] internalArray()
ByteStringinternalArray in class ByteStringpublic boolean equals(java.lang.Object o)
equals in class ByteStringpublic int hashCode()
hashCode in class ByteStringpublic java.lang.String toString()
ByteString[text=Hello] or [hex=0000ffff].toString in class ByteStringprivate java.lang.Object writeReplace()