1 | 'use strict';
|
---|
2 |
|
---|
3 |
|
---|
4 | var TYPED_OK = (typeof Uint8Array !== 'undefined') &&
|
---|
5 | (typeof Uint16Array !== 'undefined') &&
|
---|
6 | (typeof Int32Array !== 'undefined');
|
---|
7 |
|
---|
8 | function _has(obj, key) {
|
---|
9 | return Object.prototype.hasOwnProperty.call(obj, key);
|
---|
10 | }
|
---|
11 |
|
---|
12 | exports.assign = function (obj /*from1, from2, from3, ...*/) {
|
---|
13 | var sources = Array.prototype.slice.call(arguments, 1);
|
---|
14 | while (sources.length) {
|
---|
15 | var source = sources.shift();
|
---|
16 | if (!source) { continue; }
|
---|
17 |
|
---|
18 | if (typeof source !== 'object') {
|
---|
19 | throw new TypeError(source + 'must be non-object');
|
---|
20 | }
|
---|
21 |
|
---|
22 | for (var p in source) {
|
---|
23 | if (_has(source, p)) {
|
---|
24 | obj[p] = source[p];
|
---|
25 | }
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | return obj;
|
---|
30 | };
|
---|
31 |
|
---|
32 |
|
---|
33 | // reduce buffer size, avoiding mem copy
|
---|
34 | exports.shrinkBuf = function (buf, size) {
|
---|
35 | if (buf.length === size) { return buf; }
|
---|
36 | if (buf.subarray) { return buf.subarray(0, size); }
|
---|
37 | buf.length = size;
|
---|
38 | return buf;
|
---|
39 | };
|
---|
40 |
|
---|
41 |
|
---|
42 | var fnTyped = {
|
---|
43 | arraySet: function (dest, src, src_offs, len, dest_offs) {
|
---|
44 | if (src.subarray && dest.subarray) {
|
---|
45 | dest.set(src.subarray(src_offs, src_offs + len), dest_offs);
|
---|
46 | return;
|
---|
47 | }
|
---|
48 | // Fallback to ordinary array
|
---|
49 | for (var i = 0; i < len; i++) {
|
---|
50 | dest[dest_offs + i] = src[src_offs + i];
|
---|
51 | }
|
---|
52 | },
|
---|
53 | // Join array of chunks to single array.
|
---|
54 | flattenChunks: function (chunks) {
|
---|
55 | var i, l, len, pos, chunk, result;
|
---|
56 |
|
---|
57 | // calculate data length
|
---|
58 | len = 0;
|
---|
59 | for (i = 0, l = chunks.length; i < l; i++) {
|
---|
60 | len += chunks[i].length;
|
---|
61 | }
|
---|
62 |
|
---|
63 | // join chunks
|
---|
64 | result = new Uint8Array(len);
|
---|
65 | pos = 0;
|
---|
66 | for (i = 0, l = chunks.length; i < l; i++) {
|
---|
67 | chunk = chunks[i];
|
---|
68 | result.set(chunk, pos);
|
---|
69 | pos += chunk.length;
|
---|
70 | }
|
---|
71 |
|
---|
72 | return result;
|
---|
73 | }
|
---|
74 | };
|
---|
75 |
|
---|
76 | var fnUntyped = {
|
---|
77 | arraySet: function (dest, src, src_offs, len, dest_offs) {
|
---|
78 | for (var i = 0; i < len; i++) {
|
---|
79 | dest[dest_offs + i] = src[src_offs + i];
|
---|
80 | }
|
---|
81 | },
|
---|
82 | // Join array of chunks to single array.
|
---|
83 | flattenChunks: function (chunks) {
|
---|
84 | return [].concat.apply([], chunks);
|
---|
85 | }
|
---|
86 | };
|
---|
87 |
|
---|
88 |
|
---|
89 | // Enable/Disable typed arrays use, for testing
|
---|
90 | //
|
---|
91 | exports.setTyped = function (on) {
|
---|
92 | if (on) {
|
---|
93 | exports.Buf8 = Uint8Array;
|
---|
94 | exports.Buf16 = Uint16Array;
|
---|
95 | exports.Buf32 = Int32Array;
|
---|
96 | exports.assign(exports, fnTyped);
|
---|
97 | } else {
|
---|
98 | exports.Buf8 = Array;
|
---|
99 | exports.Buf16 = Array;
|
---|
100 | exports.Buf32 = Array;
|
---|
101 | exports.assign(exports, fnUntyped);
|
---|
102 | }
|
---|
103 | };
|
---|
104 |
|
---|
105 | exports.setTyped(TYPED_OK);
|
---|