[apps/tidep0084.git] / example / iot-gateway / node_modules / bytebuffer / src / methods / static / concat.js
1 /**
2 * Concatenates multiple ByteBuffers into one.
3 //? if (NODE) {
4 * @param {!Array.<!ByteBuffer|!Buffer|!ArrayBuffer|!Uint8Array|string>} buffers Buffers to concatenate
5 //? } else {
6 * @param {!Array.<!ByteBuffer|!ArrayBuffer|!Uint8Array|string>} buffers Buffers to concatenate
7 //? }
8 * @param {(string|boolean)=} encoding String encoding if `buffers` contains a string ("base64", "hex", "binary",
9 * defaults to "utf8")
10 * @param {boolean=} littleEndian Whether to use little or big endian byte order for the resulting ByteBuffer. Defaults
11 * to {@link ByteBuffer.DEFAULT_ENDIAN}.
12 * @param {boolean=} noAssert Whether to skip assertions of offsets and values for the resulting ByteBuffer. Defaults to
13 * {@link ByteBuffer.DEFAULT_NOASSERT}.
14 * @returns {!ByteBuffer} Concatenated ByteBuffer
15 * @expose
16 */
17 ByteBuffer.concat = function(buffers, encoding, littleEndian, noAssert) {
18 if (typeof encoding === 'boolean' || typeof encoding !== 'string') {
19 noAssert = littleEndian;
20 littleEndian = encoding;
21 encoding = undefined;
22 }
23 var capacity = 0;
24 for (var i=0, k=buffers.length, length; i<k; ++i) {
25 if (!ByteBuffer.isByteBuffer(buffers[i]))
26 buffers[i] = ByteBuffer.wrap(buffers[i], encoding);
27 length = buffers[i].limit - buffers[i].offset;
28 if (length > 0) capacity += length;
29 }
30 if (capacity === 0)
31 return new ByteBuffer(0, littleEndian, noAssert);
32 var bb = new ByteBuffer(capacity, littleEndian, noAssert),
33 bi;
34 //? if (!NODE && DATAVIEW)
35 var view = new Uint8Array(bb.buffer);
36 i=0; while (i<k) {
37 bi = buffers[i++];
38 length = bi.limit - bi.offset;
39 if (length <= 0) continue;
40 //? if (NODE) {
41 bi.buffer.copy(bb.buffer, bb.offset, bi.offset, bi.limit);
42 bb.offset += length;
43 //? } else {
44 //? if (DATAVIEW)
45 view.set(new Uint8Array(bi.buffer).subarray(bi.offset, bi.limit), bb.offset);
46 //? else
47 bb.view.set(bi.view.subarray(bi.offset, bi.limit), bb.offset);
48 bb.offset += length;
49 //? }
50 }
51 bb.limit = bb.offset;
52 bb.offset = 0;
53 return bb;
54 };