Last active
July 11, 2023 16:31
-
-
Save gogromat/446e962bde1122747cfe to your computer and use it in GitHub Desktop.
Concatenate n-many ArrayBuffers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Concatenates n-many ArrayBuffers | |
* Based on the https://gist.github.com/72lions/4528834 | |
* | |
* @param {...ArrayBuffer} ArrayBuffer(s) to concatenate | |
* @return {ArrayBuffer} The new ArrayBuffer created out of n buffers. | |
*/ | |
var concatArrayBuffers = function () { | |
var buffers = Array.prototype.slice.call(arguments), | |
buffersLengths = buffers.map(function(b) { return b.byteLength; }), | |
totalBufferlength = buffersLengths.reduce(function(p, c) { return p+c; }, 0), | |
unit8Arr = new Uint8Array(totalBufferlength); | |
buffersLengths.reduce(function (p, c, i) { | |
unit8Arr.set(new Uint8Array(buffers[i]), p); | |
return p+c; | |
}, 0); | |
return unit8Arr.buffer; | |
}; | |
// To call it: | |
// concatArrayBuffers.apply(this/context, [ArrayBuffers]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
respectfully, i offer this slight modification, to allow mixed type arrays or buffers to be concatenated on arbritrary byte boundaries