Class (ArrayBuffer)

Underlying data structure of the ArkTS TypedArray (Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Uint8ClampedArray, and Float32Array).

  • The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • This module can be imported only to ArkTS files (with the file name extension .ets).

Decorator: @Sendable

Modules to Import

import { collections } from '@kit.ArkTS';

Properties

System capability: SystemCapability.Utils.Lang

Atomic service API: This API can be used in atomic services since API version 12.

Name Type Read Only Optional Description
byteLength number Yes No Number of bytes occupied by the buffer.

constructor

constructor(byteLength: number)

A constructor used to create an ArkTS ArrayBuffer of a given length.

System capability: SystemCapability.Utils.Lang

Atomic service API: This API can be used in atomic services since API version 12.

Parameters

Name Type Mandatory Description
byteLength number Yes The number of bytes occupied by the buffer. The value ranges from 0 to 2147483647. Otherwise, an exception is thrown. 0 indicates that the length of the constructed ArkTS ArrayBuffer is 0, and 2147483647 indicates that the length of the constructed ArkTS ArrayBuffer is 2147483647.

Error codes

For details about the following error codes, see Language Basics Library Error Codes.

ID Error Message
10200012 The ArrayBuffer's constructor cannot be directly invoked.

Example

let arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(10);
console.info("byteLength: " + arrayBuffer.byteLength); // byteLength: 10

slice

slice(begin: number, end?: number): ArrayBuffer

Selects a range of elements in this ArkTS ArrayBuffer to create an ArkTS ArrayBuffer.

System capability: SystemCapability.Utils.Lang

Atomic service API: This API can be used in atomic services since API version 12.

Parameters

Name Type Mandatory Description
begin number Yes Start index. If begin < 0, the start position is begin + arrayBuffer.byteLength. If begin < -arrayBuffer.byteLength, the start position is 0.
end number No End index (excluding the element at this index). If end < 0, the end position is end + arrayBuffer.byteLength. If end > arrayBuffer.byteLength, the slice ends at arrayBuffer.byteLength. The default value is the length of the original ArkTS ArrayBuffer.

Return value

Type Description
ArrayBuffer collections.ArrayBuffer, a new ArkTS ArrayBuffer object containing the content of the specified range of the original ArkTS ArrayBuffer

Error codes

For details about the following error codes, see Language Basics Library Error Codes.

ID Error Message
10200011 The slice method cannot be bound.
10200201 Concurrent modification error.

Example

let arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(10);
let slicedBuffer: collections.ArrayBuffer = arrayBuffer.slice(0, 4);
console.info("byteLength: " + slicedBuffer.byteLength); // byteLength: 4