Skip to main content

blob.resize(newSize)

Changes the size of a blob

Availability

Device + Agent

Parameters

Name Type Description
newSize Integer The new memory allocation of the blob in bytes

Returns

Nothing

Description

This method increases or decreases the amount of contiguous memory allocated to the target blob. If the new size is smaller than the original size, bytes outside the size of the new blob will be lost.

It’s important to bear in mind when working with blobs that they are mutable. When you create a blob, whether with a set size — as in the example code below — or no size, at that point the object holds only the specified number of bytes. If you simply use, for instance,

local aBlob = blob();

then your blob’s size is initially zero and will stay so until you write data to it.

The method len() will show the blob’s current size: the amount of data it contains, which is the initial size plus any data beyond that that has been written to it.

The resize() method sets the blob’s memory allocation, not its size. If the new allocation is smaller than the current one, size and allocation will inevitably coincide. But this is not the case if the new size is larger: Squirrel simply grants the blob enough contiguous memory space to grow into. It does not fill that extra space, so the blob’s size remains unchanged.

The blob can grow beyond the new allocation; resize() does not impose a limit, only the amount of memory available to Squirrel does. However, growing a blob beyond its allocated size — as you inevitably do by calling blob() with no size parameter and writing one or more bytes to it, for instance — will cause Squirrel to reallocate the blob to where it has sufficient contiguous memory to hold all of the blob’s bytes, if enough free memory is available.

To prevent Squirrel doing work moving the blob every time at the blob increases in size, it’s good practice to pre-allocate the blob with sufficient memory when it is instantiated using blob() and passing a non-zero size.

Example Code