📜  Node.js Buffer.subarray() 方法

📅  最后修改于: 2022-05-13 01:56:44.810000             🧑  作者: Mango

Node.js Buffer.subarray() 方法

buffer.subarray() 方法是缓冲区模块的内置应用程序编程接口,用于裁剪数组的一部分,即从数组创建子数组。
句法:

Buffer.subarray( starting_index, ending_index )

参数:此方法有两个参数,如前所述,如下所述:

  • start_index:该参数指定一个整数值,表示缓冲区的起始地址,新缓冲区从该地址开始。它的默认值为 0。
  • ending_index:它指定一个整数值,表示要创建新缓冲区的缓冲区的结束地址。默认值是缓冲区的长度。

返回值:此方法返回裁剪后的数组。此缓冲区指向相同的内存,但具有裁剪的开始和结束索引。如果参数中的ending_index 大于缓冲区长度,则将缓冲区长度作为结束索引。
注意:如果新缓冲区被修改,则相同的内容将反映在原始缓冲区中,因为它们指向相同的内存。
以下示例说明了 Node.js 中buffer.subarray() 方法的使用:
示例 1:

javascript
// Node.js program to demonstrate the  
// Buffer.subarray() method
 
// Allocating buffer
const buf = Buffer.from('GeeksforGeeks', 'ascii');
 
// Printing original buffer
console.log("Original buffer is: " + buf);
 
// Cropping buffer, here starting index
// is 5 and ending index is 10
cropped_buf = buf.subarray(5, 10);
 
// Printing cropped buffer
console.log("Cropped buffer is: " + cropped_buf);
  
// Modifying cropped buffer
cropped_buf[0] = 70;  // F
cropped_buf[1] = 79;  // O
cropped_buf[2] = 82;  // R
 
// Printing cropped buffer
console.log("Cropped buffer after modification is: " + cropped_buf);
 
// Printing original buffer
console.log("Original buffer after modification is: " + buf);


javascript
// Node.js program to demonstrate the  
// Buffer.subarray() method
 
// Allocating buffer
const buf = Buffer.from('GeeksforGeeks', 'ascii');
 
// Printing original buffer
console.log("Original buffer is: " + buf);
  
// Cropping buffer, here starting index
// is -10 and ending index is -1
cropped_buf = buf.subarray(-12, -1);
 
// Printing cropped buffer
console.log("Cropped buffer is:" + cropped_buf);
  
// Cropping buffer again, here starting
// index is -10 and ending index is -5
cropped_buf = buf.subarray(-10, -5);
 
// Printing cropped buffer
console.log("Cropped buffer is: " + cropped_buf);
  
// Cropping buffer again with no parameter
cropped_buf = buf.subarray();
 
// Printing cropped buffer
console.log("Cropped buffer is: " + cropped_buf);


输出:

Original buffer is: GeeksforGeeks
Cropped buffer is: forGe
Cropped buffer after modification is: FORGe
Original buffer after modification is: GeeksFORGeeks

示例 2:

javascript

// Node.js program to demonstrate the  
// Buffer.subarray() method
 
// Allocating buffer
const buf = Buffer.from('GeeksforGeeks', 'ascii');
 
// Printing original buffer
console.log("Original buffer is: " + buf);
  
// Cropping buffer, here starting index
// is -10 and ending index is -1
cropped_buf = buf.subarray(-12, -1);
 
// Printing cropped buffer
console.log("Cropped buffer is:" + cropped_buf);
  
// Cropping buffer again, here starting
// index is -10 and ending index is -5
cropped_buf = buf.subarray(-10, -5);
 
// Printing cropped buffer
console.log("Cropped buffer is: " + cropped_buf);
  
// Cropping buffer again with no parameter
cropped_buf = buf.subarray();
 
// Printing cropped buffer
console.log("Cropped buffer is: " + cropped_buf);

输出:

Original buffer is: GeeksforGeeks
Cropped buffer is:eeksforGeek
Cropped buffer is: ksfor
Cropped buffer is: GeeksforGeeks

注意:以上程序将使用 node index.js 命令编译运行。
参考: https://nodejs.org/api/buffer.html#buffer_buf_subarray_start_end