简体   繁体   English

如何在Node.js中追加到Buffer

[英]How to append to Buffer in Node.js

Say I have a Buffer: 说我有一个缓冲区:

let b = Buffer.from('');

how can I append to b ? 我如何附加到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? 在同一主题上,是否可以创建动态大小的缓冲区,还是应该使用数组?

To create a dynamic buffer use an array then concat the array similar to this: 要创建动态缓冲区,请使用数组,然后concat数组,如下所示:

let chunks = []

stream
  .on('data', chunk => chunks.push(chunk))
  .on('close', () => console.log(Buffer.concat(chunks)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM