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, startsWith
SegmentedByteString(Buffer buffer, int byteCount)
public java.lang.String utf8()
ByteString
String
by decoding the bytes as UTF-8
.utf8
in class ByteString
public java.lang.String string(java.nio.charset.Charset charset)
ByteString
String
by decoding the bytes using charset
.string
in class ByteString
public java.lang.String base64()
ByteString
base64
in class ByteString
public java.lang.String hex()
ByteString
hex
in class ByteString
public ByteString toAsciiLowercase()
ByteString
toAsciiLowercase
in class ByteString
public ByteString toAsciiUppercase()
ByteString
toAsciiUppercase
in class ByteString
public ByteString md5()
ByteString
md5
in class ByteString
public ByteString sha1()
ByteString
sha1
in class ByteString
public ByteString sha256()
ByteString
sha256
in class ByteString
public ByteString hmacSha1(ByteString key)
ByteString
hmacSha1
in class ByteString
public ByteString hmacSha256(ByteString key)
ByteString
hmacSha256
in class ByteString
public java.lang.String base64Url()
ByteString
base64Url
in class ByteString
public ByteString substring(int beginIndex)
ByteString
beginIndex
is 0.substring
in class ByteString
public ByteString substring(int beginIndex, int endIndex)
ByteString
beginIndex
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 ByteString
public byte getByte(int pos)
ByteString
pos
.getByte
in class ByteString
private int segment(int pos)
pos
.public int size()
ByteString
size
in class ByteString
public byte[] toByteArray()
ByteString
ByteString
.toByteArray
in class ByteString
public java.nio.ByteBuffer asByteBuffer()
ByteString
ByteBuffer
view of the bytes in this ByteString
.asByteBuffer
in class ByteString
public void write(java.io.OutputStream out) throws java.io.IOException
ByteString
out
.write
in class ByteString
java.io.IOException
void write(Buffer buffer)
ByteString
buffer
.write
in class ByteString
public 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 ByteString
public 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 ByteString
public int indexOf(byte[] other, int fromIndex)
indexOf
in class ByteString
public int lastIndexOf(byte[] other, int fromIndex)
lastIndexOf
in class ByteString
private ByteString toByteString()
byte[] internalArray()
ByteString
internalArray
in class ByteString
public boolean equals(java.lang.Object o)
equals
in class ByteString
public int hashCode()
hashCode
in class ByteString
public java.lang.String toString()
ByteString
[text=Hello]
or [hex=0000ffff]
.toString
in class ByteString
private java.lang.Object writeReplace()