Class ByteString
- java.lang.Object
-
- okio.ByteString
-
- All Implemented Interfaces:
Serializable,Comparable<ByteString>
public class ByteString extends Object implements Serializable, 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 Stringbase64()Returns this byte string encoded as Base64.Stringbase64Url()Returns this byte string encoded as URL-safe Base64.intcompareTo(ByteString byteString)Compares this object to the specified object to determine their relative order.static ByteStringdecodeBase64(String base64)Decodes the Base64-encoded bytes and returns their value as a byte string.static ByteStringdecodeHex(String hex)Decodes the hex-encoded bytes and returns their value a byte string.static ByteStringencodeUtf8(String s)Returns a new byte string containing theUTF-8bytes ofs.booleanequals(Object o)Compares this instance with the specified object and indicates if they are equal.bytegetByte(int pos)Returns the byte atpos.inthashCode()Returns an integer hash code for this object.Stringhex()Returns this byte string encoded in hexadecimal.ByteStringmd5()Returns the 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.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(InputStream in, int byteCount)Readscountbytes frominand returns the result.ByteStringsha256()Returns the SHA-256 hash of this byte string.intsize()Returns the number of bytes in this ByteString.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.StringtoString()Returns a string containing a concise, human-readable description of this object.Stringutf8()Constructs a newStringby decoding the bytes asUTF-8.voidwrite(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.
-
encodeUtf8
public static ByteString encodeUtf8(String s)
Returns a new byte string containing theUTF-8bytes ofs.
-
utf8
public String utf8()
Constructs a newStringby decoding the bytes asUTF-8.
-
base64
public 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 MD5 hash of this byte string.
-
sha256
public ByteString sha256()
Returns the SHA-256 hash of this byte string.
-
base64Url
public String base64Url()
Returns this byte string encoded as URL-safe Base64.
-
decodeBase64
public static ByteString decodeBase64(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 String hex()
Returns this byte string encoded in hexadecimal.
-
decodeHex
public static ByteString decodeHex(String hex)
Decodes the hex-encoded bytes and returns their value a byte string.
-
read
public static ByteString read(InputStream in, int byteCount) throws IOException
Readscountbytes frominand returns the result.- Throws:
EOFException- ifinhas fewer thancountbytes to read.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.
-
write
public void write(OutputStream out) throws IOException
Writes the contents of this byte string toout.- Throws:
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.
-
equals
public boolean equals(Object o)
Description copied from class:ObjectCompares this instance with the specified object and indicates if they are equal. In order to be equal,omust represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.The default implementation returns
trueonly ifthis == o. See Writing a correctequalsmethod if you intend implementing your ownequalsmethod.The general contract for the
equalsandObject.hashCode()methods is that ifequalsreturnstruefor any two objects, thenhashCode()must return the same value for these objects. This means that subclasses ofObjectusually override either both methods or neither of them.- Overrides:
equalsin classObject- Parameters:
o- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
hashCode
public int hashCode()
Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
compareTo
public int compareTo(ByteString byteString)
Description copied from interface:ComparableCompares this object to the specified object to determine their relative order.- Specified by:
compareToin interfaceComparable<ByteString>- Parameters:
byteString- the object to compare to this instance.- Returns:
- a negative integer if this instance is less than
another; a positive integer if this instance is greater thananother; 0 if this instance has the same order asanother.
-
toString
public String toString()
Description copied from class:ObjectReturns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod.
-
-