-
- All Implemented Interfaces:
-
java.io.Serializable,java.lang.Comparable
public class ByteString implements Serializable, Comparable<ByteString>
An immutable sequence of bytes.
Byte strings compare lexicographically as a sequence of unsigned bytes. That is, the byte string
{@code ff}sorts after{@code 00}. This is counter to the sort order of the corresponding bytes, where{@code -1}sorts before{@code 0}.Full disclosure: this class provides untrusted input and output streams with raw access to the underlying byte array. A hostile stream implementation could keep a reference to the mutable byte string, violating the immutable guarantee of this class. For this reason a byte string's immutability guarantee cannot be relied upon for security in applets and other environments that run both trusted and untrusted code in the same process.
-
-
Field Summary
Fields Modifier and Type Field Description public final static ByteStringEMPTY
-
Method Summary
Modifier and Type Method Description static ByteStringof(Array<byte> data)Returns a new byte string containing a clone of the bytes of {@code data}.static ByteStringof(Array<byte> data, int offset, int byteCount)Returns a new byte string containing a copy of {@code byteCount}bytes of{@code data}startingat{@code offset}.static ByteStringof(ByteBuffer data)static ByteStringencodeUtf8(String s)Returns a new byte string containing the {@code UTF-8}bytes of{@code s}.static ByteStringencodeString(String s, Charset charset)Returns a new byte string containing the {@code charset}-encoded bytes of{@code s}.Stringutf8()Constructs a new {@code String}by decoding the bytes as{@code UTF-8}.Stringstring(Charset charset)Constructs a new {@code String}by decoding the bytes using{@code charset}.Stringbase64()Returns this byte string encoded as Base64. ByteStringmd5()Returns the 128-bit MD5 hash of this byte string. ByteStringsha1()Returns the 160-bit SHA-1 hash of this byte string. ByteStringsha256()Returns the 256-bit SHA-256 hash of this byte string. ByteStringsha512()Returns the 512-bit SHA-512 hash of this byte string. ByteStringhmacSha1(ByteString key)Returns the 160-bit SHA-1 HMAC of this byte string. ByteStringhmacSha256(ByteString key)Returns the 256-bit SHA-256 HMAC of this byte string. ByteStringhmacSha512(ByteString key)Returns the 512-bit SHA-512 HMAC of this byte string. Stringbase64Url()Returns this byte string encoded as URL-safeBase64. static ByteStringdecodeBase64(String base64)Decodes the Base64-encoded bytes and returns their value as a byte string. Stringhex()Returns this byte string encoded in hexadecimal. static ByteStringdecodeHex(String hex)Decodes the hex-encoded bytes and returns their value a byte string. static ByteStringread(InputStream in, int byteCount)Reads {@code count}bytes from{@code in}and returns the result.ByteStringtoAsciiLowercase()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'.Returns this byte string if it contains no bytes in 'A' through 'Z'. ByteStringtoAsciiUppercase()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'.Returns this byte string if it contains no bytes in 'a' through 'z'. ByteStringsubstring(int beginIndex)Returns a byte string that is a substring of this byte string, beginning at the specifiedindex until the end of this string. ByteStringsubstring(int beginIndex, int endIndex)Returns a byte string that is a substring of this byte string, beginning at the specified {@code beginIndex}and ends at the specified{@code endIndex}.bytegetByte(int pos)Returns the byte at {@code pos}.intsize()Returns the number of bytes in this ByteString. Array<byte>toByteArray()Returns a byte array containing a copy of the bytes in this {@code ByteString}.ByteBufferasByteBuffer()Returns a {@code ByteBuffer}view of the bytes in this{@code ByteString}.voidwrite(OutputStream out)Writes the contents of this byte string to {@code out}.booleanrangeEquals(int offset, ByteString other, int otherOffset, int byteCount)Returns true if the bytes of this in {@code [offset..offset+byteCount)}equal the bytes of{@code other}in{@code [otherOffset..otherOffset+byteCount)}.booleanrangeEquals(int offset, Array<byte> other, int otherOffset, int byteCount)Returns true if the bytes of this in {@code [offset..offset+byteCount)}equal the bytes of{@code other}in{@code [otherOffset..otherOffset+byteCount)}.final booleanstartsWith(ByteString prefix)final booleanstartsWith(Array<byte> prefix)final booleanendsWith(ByteString suffix)final booleanendsWith(Array<byte> suffix)final intindexOf(ByteString other)final intindexOf(ByteString other, int fromIndex)final intindexOf(Array<byte> other)intindexOf(Array<byte> other, int fromIndex)final intlastIndexOf(ByteString other)final intlastIndexOf(ByteString other, int fromIndex)final intlastIndexOf(Array<byte> other)intlastIndexOf(Array<byte> other, int fromIndex)booleanequals(Object o)inthashCode()intcompareTo(ByteString byteString)StringtoString()Returns a human-readable string that describes the contents of this byte string. -
-
Method Detail
-
of
static ByteString of(Array<byte> data)
Returns a new byte string containing a clone of the bytes of
{@code data}.
-
of
static ByteString of(Array<byte> data, int offset, int byteCount)
Returns a new byte string containing a copy of
{@code byteCount}bytes of{@code data}startingat{@code offset}.
-
of
static ByteString of(ByteBuffer data)
-
encodeUtf8
static ByteString encodeUtf8(String s)
Returns a new byte string containing the
{@code UTF-8}bytes of{@code s}.
-
encodeString
static ByteString encodeString(String s, Charset charset)
Returns a new byte string containing the
{@code charset}-encoded bytes of{@code s}.
-
string
String string(Charset charset)
Constructs a new
{@code String}by decoding the bytes using{@code charset}.
-
base64
String base64()
Returns this byte string encoded as Base64. In violation of theRFC, the returned string does not wrap lines at 76 columns.
-
md5
ByteString md5()
Returns the 128-bit MD5 hash of this byte string.
-
sha1
ByteString sha1()
Returns the 160-bit SHA-1 hash of this byte string.
-
sha256
ByteString sha256()
Returns the 256-bit SHA-256 hash of this byte string.
-
sha512
ByteString sha512()
Returns the 512-bit SHA-512 hash of this byte string.
-
hmacSha1
ByteString hmacSha1(ByteString key)
Returns the 160-bit SHA-1 HMAC of this byte string.
-
hmacSha256
ByteString hmacSha256(ByteString key)
Returns the 256-bit SHA-256 HMAC of this byte string.
-
hmacSha512
ByteString hmacSha512(ByteString key)
Returns the 512-bit SHA-512 HMAC of this byte string.
-
base64Url
String base64Url()
Returns this byte string encoded as URL-safeBase64.
-
decodeBase64
@Nullable() static ByteString decodeBase64(String base64)
Decodes the Base64-encoded bytes and returns their value as a byte string.Returns null if
{@code base64}is not a Base64-encoded sequence of bytes.
-
decodeHex
static ByteString decodeHex(String hex)
Decodes the hex-encoded bytes and returns their value a byte string.
-
read
static ByteString read(InputStream in, int byteCount)
Reads
{@code count}bytes from{@code in}and returns the result.
-
toAsciiLowercase
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'.Returns this byte string if it contains no bytes in 'A' through 'Z'.
-
toAsciiUppercase
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'.Returns this byte string if it contains no bytes in 'a' through 'z'.
-
substring
ByteString substring(int beginIndex)
Returns a byte string that is a substring of this byte string, beginning at the specifiedindex until the end of this string. Returns this byte string if
{@code beginIndex}is 0.
-
substring
ByteString substring(int beginIndex, int endIndex)
Returns a byte string that is a substring of this byte string, beginning at the specified
{@code beginIndex}and ends at the specified{@code endIndex}. Returns this byte string if{@code beginIndex}is 0 and{@code endIndex}is the length of this byte string.
-
getByte
byte getByte(int pos)
Returns the byte at
{@code pos}.
-
size
int size()
Returns the number of bytes in this ByteString.
-
toByteArray
Array<byte> toByteArray()
Returns a byte array containing a copy of the bytes in this
{@code ByteString}.
-
asByteBuffer
ByteBuffer asByteBuffer()
Returns a
{@code ByteBuffer}view of the bytes in this{@code ByteString}.
-
write
void write(OutputStream out)
Writes the contents of this byte string to
{@code out}.
-
rangeEquals
boolean rangeEquals(int offset, ByteString other, int otherOffset, int byteCount)
Returns true if the bytes of this in
{@code [offset..offset+byteCount)}equal the bytes of{@code other}in{@code [otherOffset..otherOffset+byteCount)}. Returns false if either range isout of bounds.
-
rangeEquals
boolean rangeEquals(int offset, Array<byte> other, int otherOffset, int byteCount)
Returns true if the bytes of this in
{@code [offset..offset+byteCount)}equal the bytes of{@code other}in{@code [otherOffset..otherOffset+byteCount)}. Returns false if either range isout of bounds.
-
startsWith
final boolean startsWith(ByteString prefix)
-
startsWith
final boolean startsWith(Array<byte> prefix)
-
endsWith
final boolean endsWith(ByteString suffix)
-
indexOf
final int indexOf(ByteString other)
-
indexOf
final int indexOf(ByteString other, int fromIndex)
-
lastIndexOf
final int lastIndexOf(ByteString other)
-
lastIndexOf
final int lastIndexOf(ByteString other, int fromIndex)
-
lastIndexOf
final int lastIndexOf(Array<byte> other)
-
lastIndexOf
int lastIndexOf(Array<byte> other, int fromIndex)
-
hashCode
int hashCode()
-
compareTo
int compareTo(ByteString byteString)
-
-
-
-