Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
};
@vhmth

This comment has been minimized.

Copy link

@vhmth vhmth commented Jun 23, 2015

Good πŸ‘

@khalilovcmd

This comment has been minimized.

Copy link

@khalilovcmd khalilovcmd commented Jul 27, 2016

πŸ‘

@mapaiva

This comment has been minimized.

Copy link

@mapaiva mapaiva commented Aug 19, 2016

πŸ‘

@rico345100

This comment has been minimized.

Copy link

@rico345100 rico345100 commented Jan 31, 2017

Awesome.

@amolpshinde07

This comment has been minimized.

Copy link

@amolpshinde07 amolpshinde07 commented Apr 5, 2017

πŸ‘

@Ladvien

This comment has been minimized.

Copy link

@Ladvien Ladvien commented Jun 12, 2017

πŸ‘

@deanslamajr

This comment has been minimized.

Copy link

@deanslamajr deanslamajr commented Jul 7, 2017

πŸ‘

@samccone

This comment has been minimized.

Copy link

@samccone samccone commented Dec 17, 2017

πŸ‘

@apowers313

This comment has been minimized.

Copy link

@apowers313 apowers313 commented Mar 29, 2018

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
@tchakabam

This comment has been minimized.

Copy link

@tchakabam tchakabam commented Jul 24, 2018

Nullable version with correct jsdoc annotation. Allows to start-off from a null value accumulator and add up from there. Or to be able to concat with null which results in returning the same ArrayBuffer as input. Another option would be to create a plain copy here if you want to be "safe".

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;
};
@danakt

This comment has been minimized.

Copy link

@danakt danakt commented Nov 9, 2018

@apowers313 Unfortunately, your option will work slowly when high performance is needed. And typed arrays are used in such cases.

@javiercbgr

This comment has been minimized.

Copy link

@javiercbgr javiercbgr commented Mar 29, 2019

πŸ‘

@saikishored

This comment has been minimized.

Copy link

@saikishored saikishored commented Jan 10, 2020

Thank you. You saved my day :)

@jonathan-annett

This comment has been minimized.

Copy link

@jonathan-annett jonathan-annett commented Feb 24, 2020

 function concatArrayBuffers (bufs) {
                        var offset = 0;
                        var bytes = 0;
                        var bufs2=bufs.map(function(buf,total){
                            bytes += buf.byteLength;
                            return buf;
                        });
                        var buffer = new ArrayBuffer(bytes);
                        var store = new Uint8Array(buffer);
                        bufs2.forEach(function(buf){
                            store.set(new Uint8Array(buf.buffer||buf,buf.byteOffset),offset);
                            offset += buf.byteLength;
                        });
                        return buffer
                         
                    }
 var all_buffers = concatArrayBuffers([buffer1,buffer2,buffer3]);
@11zouzouzou

This comment has been minimized.

Copy link

@11zouzouzou 11zouzouzou commented Mar 24, 2020

good

@dtruffaut

This comment has been minimized.

Copy link

@dtruffaut dtruffaut commented Jan 13, 2021

Great ! πŸ‘πŸ‘πŸ‘

@zholmes1

This comment has been minimized.

Copy link

@zholmes1 zholmes1 commented Jan 26, 2021

πŸ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment