ByteString

An immutable wrapper around a byte sequence providing String like functionality.

ByteString allows treating binary data as a value and passing it to other functions without worrying about data modification. The class facilitates various operations on binary data, like comparison or testing for subsequence inclusion.

ByteString is a good fit for untyped binary data that could not be represented as String, like hashes, payload of network packets, encrypted data, etc.

ByteString copies data on creation as well as on conversion back to ByteArray, thus guaranteeing that subsequent modification of source data or data returned from toByteArray won't mutate the string itself.

Constructors

Link copied to clipboard
constructor(data: ByteArray, startIndex: Int = 0, endIndex: Int = data.size)

Wraps a copy of data subarray starting at startIndex and ending at endIndex into a byte string.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Returns the range of valid byte indices for this byte string.

Link copied to clipboard
val size: Int

Returns size of this ByteString.

Functions

Link copied to clipboard
open operator override fun compareTo(other: ByteString): Int

Compares a byte sequence wrapped by this byte string to a byte sequence wrapped by other in lexicographical order. Byte values are compared as unsigned integers.

Link copied to clipboard

Returns true if the content of this byte string equals to the array.

Link copied to clipboard
fun copyInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size)

Copies a subsequence starting at startIndex and ending at endIndex of a byte sequence wrapped by this byte string and writes it into destination array starting at destinationOffset offset.

Link copied to clipboard

Decodes content of a byte string into a string using UTF-8 encoding.

Decodes the content of a byte string to a string using given charset.

Link copied to clipboard

Returns true if this byte string ends with the suffix specified by the byteArray.

Returns true if this byte string ends with the suffix specified by the byteString.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Returns true if other is a byte string containing exactly the same byte sequence.

Link copied to clipboard
operator fun get(index: Int): Byte

Returns a byte at the given index in this byte string.

Link copied to clipboard
open override fun hashCode(): Int

Returns a hash code based on the content of this byte string.

Link copied to clipboard
fun ByteString.indexOf(byte: Byte, startIndex: Int = 0): Int

Returns the index within this byte string of the first occurrence of the specified byte, starting from the specified startIndex. If the byte not found, -1 is returned.

fun ByteString.indexOf(byteArray: ByteArray, startIndex: Int = 0): Int

Returns the index within this byte string of the first occurrence of the specified byteArray, starting from the specified startIndex. If the byteArray not found, -1 is returned.

fun ByteString.indexOf(byteString: ByteString, startIndex: Int = 0): Int

Returns the index within this byte string of the first occurrence of the specified byteString, starting from the specified startIndex. If the byteString not found, -1 is returned.

Link copied to clipboard

Returns true if this byte string is empty.

Link copied to clipboard

Returns true if this byte string is not empty.

Link copied to clipboard
fun ByteString.lastIndexOf(byte: Byte, startIndex: Int = 0): Int

Returns the index within this char sequence of the last occurrence of the specified byte, starting from the specified startIndex. If the byte not found, -1 is returned.

fun ByteString.lastIndexOf(byteArray: ByteArray, startIndex: Int = 0): Int

Returns the index within this char sequence of the last occurrence of the specified byteArray, starting from the specified startIndex. If the byteArray not found, -1 is returned.

fun ByteString.lastIndexOf(byteString: ByteString, startIndex: Int = 0): Int

Returns the index within this char sequence of the last occurrence of the specified byteString, starting from the specified startIndex. If the byteString not found, -1 is returned.

Link copied to clipboard

Returns true if this byte string starts with the prefix specified by the byteArray.

Returns true if this byte string starts with the prefix specified by the byteString.

Link copied to clipboard
fun substring(startIndex: Int, endIndex: Int = size): ByteString

Returns a new byte string wrapping a subsequence of bytes wrapped by this byte string starting from startIndex and ending at endIndex.

Link copied to clipboard
fun toByteArray(startIndex: Int = 0, endIndex: Int = size): ByteArray

Returns a copy of subsequence starting at startIndex and ending at endIndex of a byte sequence wrapped by this byte string.

Link copied to clipboard
fun ByteString.toHexString(format: HexFormat = HexFormat.Default): String

Formats bytes in this byte string using the specified format.

fun ByteString.toHexString(startIndex: Int = 0, endIndex: Int = size, format: HexFormat = HexFormat.Default): String

Formats bytes in this byte string using the specified HexFormat.

Link copied to clipboard
open override fun toString(): String

Returns a string representation of this byte string. A string representation consists of size and a hexadecimal-encoded string of a byte sequence wrapped by this byte string.