The Data class represents binary data in memory and provides a wide range of methods for manipulating, converting, compressing, decompressing, and transforming that data. It is useful for working with raw byte buffers, encoded files, images, and more.
Specifies which compression algorithm to use when compressing or decompressing a Data instance:
| Value | Description |
|---|---|
lzfse |
Fast and efficient compression using the LZFSE algorithm. |
lz4 |
Very fast compression with moderate compression ratio. |
lzma |
High compression ratio, slower performance. |
zlib |
Standard and widely-used compression format. |
size: numberThe length of the data in bytes (read-only).
resetBytes(startIndex: number, endIndex: number): voidResets a range of bytes to zero.
startIndex: Starting index (inclusive)endIndex: Ending index (exclusive)Throws an error if the indices are out of bounds.
advanced(amount: number): DataReturns a new Data instance by removing the first amount bytes from the buffer.
replaceSubrange(startIndex, endIndex, data): voidReplaces a specified byte range with the content of another Data instance.
compressed(algorithm: CompressionAlgorithm): DataCompresses the current data using the specified algorithm and returns a new Data instance.
Throws an error if the data is empty or cannot be compressed.
decompressed(algorithm: CompressionAlgorithm): DataDecompresses the current data using the specified algorithm and returns a new Data instance.
Must use the same algorithm that was used to compress the data.
slice(start?: number, end?: number): DataReturns a new Data instance representing a slice of the original.
start: Starting index (default is 0)end: Ending index (default is the end of the data)append(other: Data): voidAppends another Data instance to the end of the current one.
getBytes(): Uint8Array | null (Deprecated)Use toUint8Array() instead.
toUint8Array(): Uint8Array | nullConverts the data into a Uint8Array.
toArrayBuffer(): ArrayBufferConverts the data into an ArrayBuffer.
toBase64String(): stringReturns a Base64-encoded string representation of the data.
toHexString(): stringReturns a hexadecimal string representation of the data.
toRawString(encoding?: string): string | nullConverts the data into a string using the specified encoding (default: "utf-8").
toIntArray(): number[]Returns an array of integers representing the bytes of the data.
Data.fromIntArray(array: number[]): DataCreates a Data instance from an array of integers.
Data.fromString(str: string, encoding?: string): Data | null (Deprecated)Use Data.fromRawString() instead.
Data.fromRawString(str: string, encoding?: string): Data | nullCreates a Data instance from a raw string, using the specified encoding (default: "utf-8").
Data.fromFile(filePath: string): Data | nullReads binary data from a file path and returns a Data instance.
Data.fromArrayBuffer(buffer: ArrayBuffer): Data | nullCreates a Data instance from an ArrayBuffer.
Data.fromUint8Array(bytes: Uint8Array): Data | nullCreates a Data instance from a Uint8Array.
Data.fromBase64String(base64: string): Data | nullCreates a Data instance from a Base64-encoded string.
Data.fromHexString(hex: string): Data | nullCreates a Data instance from a hexadecimal string.
Data.fromJPEG(image: UIImage, compressionQuality?: number): Data | nullConverts a UIImage to JPEG format data.
compressionQuality: Compression quality between 0.0 and 1.0 (default: 1.0)Data.fromPNG(image: UIImage): Data | nullConverts a UIImage to PNG format data.
Data.combine(dataList: Data[]): DataCombines multiple Data instances into one.
Returns null if the list is empty or all items are empty.