* @param {Buffer[]} chunks * @param {number} length * @returns {Uint8Array}
(chunks, length)
| 494 | * @returns {Uint8Array} |
| 495 | */ |
| 496 | function chunksConcat (chunks, length) { |
| 497 | if (chunks.length === 0 || length === 0) { |
| 498 | return new Uint8Array(0) |
| 499 | } |
| 500 | if (chunks.length === 1) { |
| 501 | // fast-path |
| 502 | return new Uint8Array(chunks[0]) |
| 503 | } |
| 504 | const buffer = new Uint8Array(Buffer.allocUnsafeSlow(length).buffer) |
| 505 | |
| 506 | let offset = 0 |
| 507 | for (let i = 0; i < chunks.length; ++i) { |
| 508 | const chunk = chunks[i] |
| 509 | buffer.set(chunk, offset) |
| 510 | offset += chunk.length |
| 511 | } |
| 512 | |
| 513 | return buffer |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * @param {Consume} consume |