Package okio
Class ByteString
- java.lang.Object
-
- okio.ByteString
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<ByteString>
public class ByteString extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<ByteString>
An immutable sequence of bytes.Byte strings compare lexicographically as a sequence of unsigned bytes. That is, the byte string
ffsorts after00. This is counter to the sort order of the corresponding bytes, where-1sorts before0.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.
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static ByteStringEMPTYA singleton emptyByteString.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.nio.ByteBufferasByteBuffer()Returns aByteBufferview of the bytes in thisByteString.java.lang.Stringbase64()Returns this byte string encoded as Base64.java.lang.Stringbase64Url()Returns this byte string encoded as URL-safe Base64.intcompareTo(ByteString byteString)static ByteStringdecodeBase64(java.lang.String base64)Decodes the Base64-encoded bytes and returns their value as a byte string.static ByteStringdecodeHex(java.lang.String hex)Decodes the hex-encoded bytes and returns their value a byte string.static ByteStringencodeString(java.lang.String s, java.nio.charset.Charset charset)Returns a new byte string containing thecharset-encoded bytes ofs.static ByteStringencodeUtf8(java.lang.String s)Returns a new byte string containing theUTF-8bytes ofs.booleanendsWith(byte[] suffix)booleanendsWith(ByteString suffix)booleanequals(java.lang.Object o)bytegetByte(int pos)Returns the byte atpos.inthashCode()java.lang.Stringhex()Returns this byte string encoded in hexadecimal.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.intindexOf(byte[] other)intindexOf(byte[] other, int fromIndex)intindexOf(ByteString other)intindexOf(ByteString other, int fromIndex)intlastIndexOf(byte[] other)intlastIndexOf(byte[] other, int fromIndex)intlastIndexOf(ByteString other)intlastIndexOf(ByteString other, int fromIndex)ByteStringmd5()Returns the 128-bit MD5 hash of this byte string.static ByteStringof(byte... data)Returns a new byte string containing a clone of the bytes ofdata.static ByteStringof(byte[] data, int offset, int byteCount)Returns a new byte string containing a copy ofbyteCountbytes ofdatastarting atoffset.static ByteStringof(java.nio.ByteBuffer data)booleanrangeEquals(int offset, byte[] other, int otherOffset, int byteCount)Returns true if the bytes of this in[offset..offset+byteCount)equal the bytes ofotherin[otherOffset..otherOffset+byteCount).booleanrangeEquals(int offset, ByteString other, int otherOffset, int byteCount)Returns true if the bytes of this in[offset..offset+byteCount)equal the bytes ofotherin[otherOffset..otherOffset+byteCount).static ByteStringread(java.io.InputStream in, int byteCount)Readscountbytes frominand returns the result.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.intsize()Returns the number of bytes in this ByteString.booleanstartsWith(byte[] prefix)booleanstartsWith(ByteString prefix)java.lang.Stringstring(java.nio.charset.Charset charset)Constructs a newStringby decoding the bytes usingcharset.ByteStringsubstring(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.ByteStringsubstring(int beginIndex, int endIndex)Returns a byte string that is a substring of this byte string, beginning at the specifiedbeginIndexand ends at the specifiedendIndex.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'.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'.byte[]toByteArray()Returns a byte array containing a copy of the bytes in thisByteString.java.lang.StringtoString()Returns a human-readable string that describes the contents of this byte string.java.lang.Stringutf8()Constructs a newStringby decoding the bytes asUTF-8.voidwrite(java.io.OutputStream out)Writes the contents of this byte string toout.
-
-
-
Field Detail
-
EMPTY
public static final ByteString EMPTY
A singleton emptyByteString.
-
-
Method Detail
-
of
public static ByteString of(byte... data)
Returns a new byte string containing a clone of the bytes ofdata.
-
of
public static ByteString of(byte[] data, int offset, int byteCount)
Returns a new byte string containing a copy ofbyteCountbytes ofdatastarting atoffset.
-
of
public static ByteString of(java.nio.ByteBuffer data)
-
encodeUtf8
public static ByteString encodeUtf8(java.lang.String s)
Returns a new byte string containing theUTF-8bytes ofs.
-
encodeString
public static ByteString encodeString(java.lang.String s, java.nio.charset.Charset charset)
Returns a new byte string containing thecharset-encoded bytes ofs.
-
utf8
public java.lang.String utf8()
Constructs a newStringby decoding the bytes asUTF-8.
-
string
public java.lang.String string(java.nio.charset.Charset charset)
Constructs a newStringby decoding the bytes usingcharset.
-
base64
public java.lang.String base64()
Returns this byte string encoded as Base64. In violation of the RFC, the returned string does not wrap lines at 76 columns.
-
md5
public ByteString md5()
Returns the 128-bit MD5 hash of this byte string.
-
sha1
public ByteString sha1()
Returns the 160-bit SHA-1 hash of this byte string.
-
sha256
public ByteString sha256()
Returns the 256-bit SHA-256 hash of this byte string.
-
sha512
public ByteString sha512()
Returns the 512-bit SHA-512 hash of this byte string.
-
hmacSha1
public ByteString hmacSha1(ByteString key)
Returns the 160-bit SHA-1 HMAC of this byte string.
-
hmacSha256
public ByteString hmacSha256(ByteString key)
Returns the 256-bit SHA-256 HMAC of this byte string.
-
hmacSha512
public ByteString hmacSha512(ByteString key)
Returns the 512-bit SHA-512 HMAC of this byte string.
-
base64Url
public java.lang.String base64Url()
Returns this byte string encoded as URL-safe Base64.
-
decodeBase64
@Nullable public static ByteString decodeBase64(java.lang.String base64)
Decodes the Base64-encoded bytes and returns their value as a byte string. Returns null ifbase64is not a Base64-encoded sequence of bytes.
-
hex
public java.lang.String hex()
Returns this byte string encoded in hexadecimal.
-
decodeHex
public static ByteString decodeHex(java.lang.String hex)
Decodes the hex-encoded bytes and returns their value a byte string.
-
read
public static ByteString read(java.io.InputStream in, int byteCount) throws java.io.IOException
Readscountbytes frominand returns the result.- Throws:
java.io.EOFException- ifinhas fewer thancountbytes to read.java.io.IOException
-
toAsciiLowercase
public 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
public 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
public 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. Returns this byte string ifbeginIndexis 0.
-
substring
public ByteString substring(int beginIndex, int endIndex)
Returns a byte string that is a substring of this byte string, beginning at the specifiedbeginIndexand ends at the specifiedendIndex. Returns this byte string ifbeginIndexis 0 andendIndexis the length of this byte string.
-
getByte
public byte getByte(int pos)
Returns the byte atpos.
-
size
public int size()
Returns the number of bytes in this ByteString.
-
toByteArray
public byte[] toByteArray()
Returns a byte array containing a copy of the bytes in thisByteString.
-
asByteBuffer
public java.nio.ByteBuffer asByteBuffer()
Returns aByteBufferview of the bytes in thisByteString.
-
write
public void write(java.io.OutputStream out) throws java.io.IOExceptionWrites the contents of this byte string toout.- Throws:
java.io.IOException
-
rangeEquals
public boolean rangeEquals(int offset, ByteString other, int otherOffset, int byteCount)Returns true if the bytes of this in[offset..offset+byteCount)equal the bytes ofotherin[otherOffset..otherOffset+byteCount). Returns false if either range is out of bounds.
-
rangeEquals
public boolean rangeEquals(int offset, byte[] other, int otherOffset, int byteCount)Returns true if the bytes of this in[offset..offset+byteCount)equal the bytes ofotherin[otherOffset..otherOffset+byteCount). Returns false if either range is out of bounds.
-
startsWith
public final boolean startsWith(ByteString prefix)
-
startsWith
public final boolean startsWith(byte[] prefix)
-
endsWith
public final boolean endsWith(ByteString suffix)
-
endsWith
public final boolean endsWith(byte[] suffix)
-
indexOf
public final int indexOf(ByteString other)
-
indexOf
public final int indexOf(ByteString other, int fromIndex)
-
indexOf
public final int indexOf(byte[] other)
-
indexOf
public int indexOf(byte[] other, int fromIndex)
-
lastIndexOf
public final int lastIndexOf(ByteString other)
-
lastIndexOf
public final int lastIndexOf(ByteString other, int fromIndex)
-
lastIndexOf
public final int lastIndexOf(byte[] other)
-
lastIndexOf
public int lastIndexOf(byte[] other, int fromIndex)
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
compareTo
public int compareTo(ByteString byteString)
- Specified by:
compareToin interfacejava.lang.Comparable<ByteString>
-
toString
public java.lang.String toString()
Returns a human-readable string that describes the contents of this byte string. Typically this is a string like[text=Hello]or[hex=0000ffff].- Overrides:
toStringin classjava.lang.Object
-
-