Node.js Stream writable.writableLength 属性
writable.writableLength 属性是流模块的内置应用程序,用于检查队列中准备写入的字节数。
句法:
writable.writableLength
返回值:此属性返回准备写入队列的字节数。
下面的例子说明了 Node.js 中writable.writableLength 属性的使用:
示例 1:
// Node.js program to demonstrate the
// writable.writableLength Property
// Accessing stream module
const stream = require('stream');
// Creating a stream and creating
// a write function
const writable = new stream.Writable({
// Write function with its
// parameters
write: function(chunk, encoding, next) {
// Converting the chunk of
// data to string
console.log(chunk.toString());
next();
}
});
// Calling cork() function
writable.cork();
//Writing data
writable.write('hi');
// Again writing some data
writable.write('GFG');
// Calling writable.writableLength
// Property
writable.writableLength;
输出:
5
示例 2:
// Node.js program to demonstrate the
// writable.writableLength Property
// Accessing stream module
const stream = require('stream');
// Creating a stream and creating
// a write function
const writable = new stream.Writable({
// Write function with its
// parameters
write: function(chunk, encoding, next) {
// Converting the chunk of
// data to string
console.log(chunk.toString());
next();
}
});
// Calling cork() function
writable.cork();
//Writing data
writable.write('hi');
// Calling uncork() function
writable.uncork();
// Again calling cork function
writable.cork();
// Again writing some data
writable.write('GFG');
// Calling writable.writableLength
// Property
writable.writableLength;
输出:
hi
3
在这里,缓冲区中不再存在数据“hi”因此,缓冲区只有 3 个字节存在数据“GFG”占用 3 个字节。
参考: https://nodejs.org/api/stream.html#stream_writable_writablelength