Skip to content
Advertisement

Resize ArrayBuffer

If I want to create an arraybuffer, I write: var buff = new ArrayBuffer(size)

But how is it possible to resize an existing buffer? I mean, adding some more bytes at the end of the buffer.

Advertisement

Answer

var buff = new ArrayBuffer(32);
buff[31] = 43;
var newBuff = new ArrayBuffer(buff.byteLength*2);

for (var i=0;i<buff.byteLength;i++){
    newBuff[i] = buff[i];
}

buff = newBuff;

I’ve done it in C++ like this. Just made a bigger array and copy the contents over and then return the larger array and set it as the original.

Advertisement