DEV Community

Posted on

How to Join Arraybuffer sliced into one ArrayBuffer?

Suppose I have ArrayBuffer(90) of one-file, Now I sliced ArrayBuffer into array according to offset of (30).

It will look like this at (Client-1)
[ArrayBuffer(30), ArrayBuffer(30), ArrayBuffer(30)]

I sent this Small chunks to another client2(One by one) where I kept this chunks into Array. ==> var buffer_area=[];

So buffer_area will denote like this at client2 :
(3) [ArrayBuffer(30),ArrayBuffer(30),ArrayBuffer(30)]

My dilemma is how to join/merge/append this array of ArrayBuffer
into one whole ArrayBuffer so it will look like ArrayBuffer(90).

Is there any way to do that : Join Array of ArrayBuffer into one whole ArrayBuffer?

Top comments (1)

jswalker_in profile image

It was so simple:

var array_of_arraybuffer = [ArrayBuffer(30),[ArrayBuffer(30)]; // Contain sliced ArrayBuffer

Step 1:
Loop Through : Chunk of ArrayBuffer : array_of_arraybuffer (i)

Step 2:
var merged_ab = new Uint16Array(); //In Every ith-iteration

Step 3:
Pull ith element from array of arraybuffer

var j=0,len=array_of_arraybuffer[i].length;

var tmp = new Uint8Array(merged_ab.byteLength + array_of_arraybuffer[i].byteLength);
tmp.set(new Uint8Array(merged_ab), 0);
tmp.set(new Uint8Array(array_of_arraybuffer[i]), merged_ab.byteLength);

Step 4:
/Check merged_ab.byteLength with array_of_arraybuffer[i] 's total size if it matched then it worked perfectly/