Concatenating Buffers
You can concatenate two or more Buffer objects together to form a new buffer. The concat(list, [totalLength]) method accepts an array of Buffer objects as the first parameter and totalLength, defining the maximum bytes in the buffer, as an optional second argument. The Buffer objects are concatenated in the order in which they appear in the list, and a new Buffer object is returned, containing the contents of the original buffers up to totalLength bytes.
If you do not provide a totalLength parameter, concat() figures out the total length for you. However, it has to iterate through the list, so providing a totalLength value is a bit faster.
The code in Listing 5.5 illustrates concatenation by concatenating a base Buffer
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access