don kaka don kaka - 1 month ago 18
Javascript Question

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.

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

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

buf = buff;

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.

Comments