You can create a new TypedArray with a new ArrayBuffer, but you cannot resize an existing buffer
function concatTypedArrays(a, b) {
Can now do
var a = new Uint8Array(2), b = new Uint8Array(3); a[0] = 1; a[1] = 2; b[0] = 3; b[1] = 4; concatTypedArrays(a, b);
If you want to use different types, go through Uint8Array , since the smallest unit is a byte, i.e.
function concatBuffers(a, b) { return concatTypedArrays( new Uint8Array(a.buffer || a), new Uint8Array(b.buffer || b) ).buffer; }
This means that .length will work as expected, now you can convert it to your typed array (make sure it is the type that will accept .byteLength in the buffer, though)
Now you can implement any method that you like to concatenate your data, for example.
function concatBytes(ui8a, byte) { var b = new Uint8Array(1); b[0] = byte; return concatTypedArrays(ui8a, b); } var u8 = new Uint8Array(0); u8 = concatBytes(u8, 0x80);
Paul S.
source share