* Concatenate an array of data of the given type. * @param {String} type the type of the data in the given array. * @param {Array} dataArray the array containing the data chunks to concatenate * @return {String|Uint8Array|Buffer} the concatenated data * @throws Error if the asked type is unsuppo
(type, dataArray)
| 2810 | * @throws Error if the asked type is unsupported |
| 2811 | */ |
| 2812 | function concat (type, dataArray) { |
| 2813 | var i, index = 0, res = null, totalLength = 0; |
| 2814 | for(i = 0; i < dataArray.length; i++) { |
| 2815 | totalLength += dataArray[i].length; |
| 2816 | } |
| 2817 | switch(type) { |
| 2818 | case "string": |
| 2819 | return dataArray.join(""); |
| 2820 | case "array": |
| 2821 | return Array.prototype.concat.apply([], dataArray); |
| 2822 | case "uint8array": |
| 2823 | res = new Uint8Array(totalLength); |
| 2824 | for(i = 0; i < dataArray.length; i++) { |
| 2825 | res.set(dataArray[i], index); |
| 2826 | index += dataArray[i].length; |
| 2827 | } |
| 2828 | return res; |
| 2829 | case "nodebuffer": |
| 2830 | return Buffer.concat(dataArray); |
| 2831 | default: |
| 2832 | throw new Error("concat : unsupported type '" + type + "'"); |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | /** |
| 2837 | * Listen a StreamHelper, accumulate its content and concatenate it into a |