Appending ArrayBuffers

arraybuffer, javascript, typed-arrays

Solution

You could always use `DataView` (http://www.khronos.org/registry/typedarray/specs/latest/#8) rather than a specific typed array, but as has been mentioned in the comments to your question, you can't actually do much with `ArrayBuffer` on its own.

Problem

What is the preferable way of appending/combining ArrayBuffers? I'm receiving and parsing network packets with a variety of data structures. Incoming messages are read into ArrayBuffers. If a partial packet arrives I need to store it and wait for the next message before re-attempting to parse it. Currently I'm doing something like this: ``` function appendBuffer( buffer1, buffer2 ) { var tmp = new Uint8Array( buffer1.byteLength + buffer2.byteLength ); tmp.set( new Uint8Array( buffer1 ), 0 ); tmp.set( new Uint8Array( buffer2 ), buffer1.byteLength ); return tmp.buffer; } ``` Obviously you can't get around having to create a new buffer as ArrayBuffers are of a fixed length, but is it necessary to initialize typed arrays? Upon arrival I just want is to be able to treat the buffers as buffers; types and structures are of no concern.

Original source

Related problems