Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to Buffer in Node.js

Say I have a Buffer:

let b = Buffer.from('');

how can I append to b? Is the only way to create a new Buffer?

b = Buffer.concat([b, z]);

on the same subject, is there a way to create a dynamic sized buffer, or should I use Array instead?

like image 608
Alexander Mills Avatar asked May 17 '26 13:05

Alexander Mills


1 Answers

To create a dynamic buffer use an array then concat the array similar to this:

let chunks = []

stream
  .on('data', chunk => chunks.push(chunk))
  .on('close', () => console.log(Buffer.concat(chunks)))
like image 64
Get Off My Lawn Avatar answered May 19 '26 03:05

Get Off My Lawn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!