2018-08-14 12:11:49 +00:00
|
|
|
// reduce buffer size, avoiding mem copy
|
2018-08-27 12:30:50 +00:00
|
|
|
export function shrinkBuf (buf, size) {
|
|
|
|
if (buf.length === size) { return buf; }
|
|
|
|
if (buf.subarray) { return buf.subarray(0, size); }
|
2018-08-14 12:11:49 +00:00
|
|
|
buf.length = size;
|
|
|
|
return buf;
|
2020-03-16 13:59:45 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
|
|
|
|
export function arraySet (dest, src, src_offs, len, dest_offs) {
|
2018-08-14 12:11:49 +00:00
|
|
|
if (src.subarray && dest.subarray) {
|
|
|
|
dest.set(src.subarray(src_offs, src_offs + len), dest_offs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Fallback to ordinary array
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
dest[dest_offs + i] = src[src_offs + i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join array of chunks to single array.
|
2018-08-27 12:30:50 +00:00
|
|
|
export function flattenChunks (chunks) {
|
2018-08-14 12:11:49 +00:00
|
|
|
var i, l, len, pos, chunk, result;
|
|
|
|
|
|
|
|
// calculate data length
|
|
|
|
len = 0;
|
|
|
|
for (i = 0, l = chunks.length; i < l; i++) {
|
|
|
|
len += chunks[i].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// join chunks
|
|
|
|
result = new Uint8Array(len);
|
|
|
|
pos = 0;
|
|
|
|
for (i = 0, l = chunks.length; i < l; i++) {
|
|
|
|
chunk = chunks[i];
|
|
|
|
result.set(chunk, pos);
|
|
|
|
pos += chunk.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
export var Buf8 = Uint8Array;
|
|
|
|
export var Buf16 = Uint16Array;
|
|
|
|
export var Buf32 = Int32Array;
|