📜  Node.js Buffer.kMaxLength 属性

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

Node.js Buffer.kMaxLength 属性

Buffer.kMaxLength属性是缓冲区模块中Buffer类的内置应用程序编程接口,用于设置和获取单个缓冲区实例允许的最大长度。

句法:

const Buffer.kMaxLength

参数:此属性用作 getter 和 setter,因此有时将整数值作为参数。

返回值:此属性返回单个缓冲区实例允许的最大长度。

示例 1:文件名:index.js

Javascript
// Node.js program to demonstrate the
// Buffer.kMaxLength property
 
// Creating and initializing arraybuffer object
const arrbuff = new ArrayBuffer(16);
 
// Getting buffer object form existing
// arraybuffer object
const buffer = Buffer.from(arrbuff);
 
// Setting the the maximum value
// into the buffer
buffer.kMaxLength = 23;
 
// Getting the maximum length by using
// kMaxLength property
const value = buffer.kMaxLength;
 
// Display the result
console.log("kMaxLength is: " + value);


Javascript
// Node.js program to demonstrate the
// Buffer.kMaxLength property
 
// If kMaxLengthis is not initialized
 
// Creating and initializing arraybuffer
// object
const arrbuff = new ArrayBuffer(16);
 
// Getting buffer object form existing
// arraybuffer object
const buffer = Buffer.from(arrbuff);
 
// Getting the maximum length by using
// kMaxLength property
const value = buffer.kMaxLength;
 
// Display the result
console.log("kMaxLength is: " + value);


输出:

kMaxLength is: 23

示例 2:文件名:index.js

如果 kMaxLength 未初始化

Javascript

// Node.js program to demonstrate the
// Buffer.kMaxLength property
 
// If kMaxLengthis is not initialized
 
// Creating and initializing arraybuffer
// object
const arrbuff = new ArrayBuffer(16);
 
// Getting buffer object form existing
// arraybuffer object
const buffer = Buffer.from(arrbuff);
 
// Getting the maximum length by using
// kMaxLength property
const value = buffer.kMaxLength;
 
// Display the result
console.log("kMaxLength is: " + value);

输出:

kMaxLength is: undefined

使用以下命令运行 index.js 文件:

node index.js

参考: https://nodejs.org/dist/latest-v12.x/docs/api/buffer.html#buffer_buffer_kmaxlength