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
相關用法
- node.js Stream writable.destroyed用法及代碼示例
- node.js Stream writable.writableEnded用法及代碼示例
- node.js Stream readable.destroyed用法及代碼示例
- node.js Stream writable.writableCorked用法及代碼示例
- node.js Stream writable.writableHighWaterMark用法及代碼示例
- node.js Stream writable.writableFinished用法及代碼示例
- node.js Stream writable.writableObjectMode用法及代碼示例
- node.js Stream readable.readable用法及代碼示例
- node.js Stream writable.writable用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream writable.writableLength Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。