GUIDES Binary Data Resize an ArrayBuffer
About
Binary Data
Index
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
Callbacks
Index
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
What About setTimeout?
Optimizing Embedded JavaScript
Index
When to Optimize
Know Where to Optimize
Looping through an Array
Iterating Over a String
Building a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Appending to an Array
Operating on Bits
Defining Class Methods
Reducing Stack Use
Time
Index
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Set Real-Time Clock Time
Get Time and Date from Network
Get Time and Date from Real-Time Clock
Sleep

Resize an ArrayBuffer

An ArrayBuffer created with with the maxByteLength option may be resized.

const buffer = new ArrayBuffer(0, {maxByteLength: 1024});
// => buffer.byteLength == 0

buffer.resize(48);
// => buffer.byteLength == 48

maxByteLength is the largest number of bytes that the ArrayBuffer may contain.

The XS JavaScript engine does not allocate maxByteLength bytes when the ArrayBuffer is created. It only allocates as many bytes are used.

const buffer = new ArrayBuffer(64, {maxByteLength: 1024});

buffer.resize(1024);
// => ok

buffer.resize(1025);
// => throws exception

Sometimes you don't know the maximum size an ArrayBuffer will need, but you must provides a value for maxBytesLength. You can safely use a very large number because XS doesn't pre-allocate maxByteLength bytes.

Attempting to resize() beyond available memory will fail.

const buffer = new ArrayBuffer(64, {maxByteLength: 1024 * 1024 * 1024});

When a resizable ArrayBuffer is the backing store for Uint8Array or any other TypedArray, the array's length automatically adjusts based on the ArrayBuffer.

const buffer = new ArrayBuffer(512, {maxByteLength: 1024});

const bytes = new Uint8Array(buffer);
const words = new Uint16Array(buffer);
// => bytes.length == 512
// => words.length = 256

buffer.resize(1024);
// => bytes.length == 1024
// => words.length = 512

buffer.resize(9);
// => bytes.length == 9
// => words.length = 4

You can determine if an ArrayBuffer can be resized by checking its resizable property. Its maxByteLength property indicates the largest possible size of the buffer.

const resizableBuffer = new ArrayBuffer(512, {maxByteLength: 1024});
// => resizableBuffer.resizable == true
// => resizableBuffer.maxByteLength == 1024
// => resizableBuffer.byteLength == 512

const buffer = new ArrayBuffer(512);
// => buffer.resizable == false
// => buffer.maxByteLength == 0
// => buffer.byteLength == 512