Concatenates two ArrayBuffers
/** | |
* Creates a new Uint8Array based on two different ArrayBuffers | |
* | |
* @private | |
* @param {ArrayBuffers} buffer1 The first buffer. | |
* @param {ArrayBuffers} buffer2 The second buffer. | |
* @return {ArrayBuffers} The new ArrayBuffer created out of the two. | |
*/ | |
var _appendBuffer = function(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; | |
}; |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Awesome. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
In ES6+ there's an easier way using the "iterator" property of TypedArrays and the spread operator: // create three example TypedArrays
// if you just have an ArrayBuffer called `ab` you can do `new Uint8Array(ab)`
var b1 = new Uint8Array([0x01, 0x02, 0x03]);
var b2 = new Uint8Array([0x04, 0x05, 0x06]);
var b3 = new Uint8Array([0x07, 0x08, 0x09]);
// combine all three arrays into a new array buffer
// if you need the ArrayBuffer instead of a TypedArray, it's at `combined.buffer
// NOTE: square brackets in the Uint8Array constructor -- Uint8Array([...])
var combined = new Uint8Array([
...b1,
...b2,
...b3
]);
// combined now contains:
// 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 |
This comment has been minimized.
This comment has been minimized.
Nullable version with correct jsdoc annotation. Allows to start-off from a Also, I don't see any state here, no point this being "private" or any member of a class. /**
* Creates a new ArrayBuffer from concatenating two existing ones
*
* @param {ArrayBuffer | null} buffer1 The first buffer.
* @param {ArrayBuffer | null} buffer2 The second buffer.
* @return {ArrayBuffer | null} The new ArrayBuffer created out of the two.
*/
var concatArrayBuffers = function(buffer1, buffer2) {
if (!buffer1) {
return buffer2;
} else if (!buffer2) {
return buffer1;
}
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
}; |
This comment has been minimized.
This comment has been minimized.
@apowers313 Unfortunately, your option will work slowly when high performance is needed. And typed arrays are used in such cases. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Thank you. You saved my day :) |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
good |
This comment has been minimized.
This comment has been minimized.
Great ! |
This comment has been minimized.
This comment has been minimized.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Good👍