18
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ArrayBufferの分割/結合方法

Posted at

あらまし

ArrayBufferに格納された比較的サイズの大きいデータ(バイナリファイルなど)をリモートへ送信や受信したり、ウェブワーカーに渡す際に、UIのブロックを防ぐために小さな単位に分割したい場合がある。ここではArrayBufferを指定のサイズのセグメントに分割する方法と、複数のArrayBufferを単一のArrayBufferに結合する方法を紹介する。

分割

分割はArrayBufferのslice関数で行う。slice関数は、指定の開始から終了位置までのデータを保持した新しいArrayBufferオブジェクトを返す関数である。sliceされたデータは、もとのArrayBufferとは共有されず、新たにコピーされる。

function segmentation(arrayBuffer, segmentSize)
{
	var segments= [];
	var fi = 0;
	while(fi*segmentSize< arrayBuffer.byteLength){
		segments.push( arrayBuffer.slice(fi*segmentSize, (fi+1)*segmentSize) );
		++fi;
	}
	return segments;
}

実行例

サイズ100のArrayBufferをサイズ30のセグメントに分割。30, 30, 30, 10のサイズの4つのArrayBufferに分割される。

image

結合

一旦Uint8Arrayで結合後のサイズ分の領域確保してから、set関数を使って各セグメントを該当位置に格納していゆく。

function concatenation(segments)
{
	var sumLength = 0;
	for(var i = 0; i < segments.length; ++i){
		sumLength += segments[i].byteLength;
	}
	var whole = new Uint8Array(sumLength);
	var pos = 0;
	for(var i = 0; i < segments.length; ++i){
		whole.set(new Uint8Array(segments[i]),pos);
		pos += segments[i].byteLength;
	}
	return whole.buffer;
}

実行例

サイズ100のArrayBufferをサイズ30のセグメントに分割し、さらにそれらを結合して元に戻す。

image

18
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?