Node.js Buffer.concat() 方法
Buffer.concat()方法用于将给定数组中的所有缓冲区对象连接到一个缓冲区对象中。该方法的返回值也是一个缓冲区。如果未提供缓冲区长度,则从列表中的缓冲区实例计算。
句法:
Buffer.concat( list, length )
参数:此方法接受上面提到的两个参数,如下所述:
- list:包含要连接的缓冲区列表。
- 长度:它定义了连接缓冲区的长度。此参数是可选的。
示例 1:
// Returns a new buffer with the
// copy of the passed string
var buf1 = Buffer.from("Geeks");
// Returns another buffer with
// copy of the passed string
var buf2 = Buffer.from("for");
var buf3 = Buffer.from("Geeks");
// Creates an array of buffers
var list = [buf1, buf2, buf3];
// Concatenates all buffer objects into one buffer
var newbuff = Buffer.concat(list);
console.log("The concatenated buffer:");
// Displays the concatenated buffer
console.log(newbuff);
输出:
The concatenated buffer:
示例 2:
// Returns a new buffer with the
// copy of the passed string
var buf1 = Buffer.from("Good");
// Returns another buffer with
// copy of the passed string
var buf2 = Buffer.from("morning");
var buf3 = Buffer.from("everyone");
// Creates an array of buffers
var list = [buf1, buf2, buf3];
// Concatenates all buffer objects
// into one buffer
var newbuff = Buffer.concat(list);
console.log("The concatenated buffer:");
// Displays the concatenated buffer
console.log(newbuff);
输出:
The concatenated buffer:
注意:以上程序将使用node index.js
命令编译运行。
参考:
https://nodejs.org/dist/latest-v13.x/docs/api/buffer.html#buffer_class_method_buffer_concat_list_totallength