Node.js 中 Stream writable.writableLength 屬性
writable.writableLength 屬性用於顯示排隊中準備寫入的位元組或物件數。這用於根據 highWaterMark 的狀態檢查資料。
語法
writeable.writableLength
示例 1
建立一個名為 writableLength.js 的檔案並複製下面的程式碼段。建立檔案後,使用以下命令執行此程式碼,如下例所示 −
node writableLength.js
// Program to demonstrate writable.writableLength method
const stream = require('stream');
// Creating a data stream with writable
const writable = new stream.Writable({
// Writing the data from stream
write: function(chunk, encoding, next) {
// Converting the data chunk to be displayed
console.log(chunk.toString());
next();
}
});
// Writing data - Not in the buffer queue
writable.write('Hi - This data will not be counted');
// Calling the cork() function
writable.cork();
// Again writing some data
writable.write('Welcome to TutorialsPoint !');
writable.write('SIMPLY LEARNING ');
writable.write('This data will be corked in the memory');
// Printing the length of the queue data
console.log(writable.writableLength);輸出
C:\home
ode>> node writableLength.js Hi - This data will not be counted 81
將堵塞在緩衝佇列中的資料進行計數並列印到控制檯。
示例
我們再看一個示例。
// Program to demonstrate writable.cork() method
const stream = require('stream');
// Creating a data stream with writable
const writable = new stream.Writable({
// Writing the data from stream
write: function(chunk, encoding, next) {
// Converting the data chunk to be displayed
console.log(chunk.toString());
next();
}
});
// Writing data - Not in the buffer queue
writable.write('Hi - This data will not be counted');
// Calling the cork() function
writable.cork();
// Again writing some data
writable.write('Welcome to TutorialsPoint !');
writable.write('SIMPLY LEARNING ');
writable.write('This data will be corked in the memory');
// Printing the length of the queue data
console.log(writable.writableLength);
// Flushing the data from buffered memory
writable.uncork()
console.log(writable.writableLength);輸出
C:\home
ode>> node writableLength.js Hi - This data will not be counted 81 Welcome to TutorialsPoint ! SIMPLY LEARNING This data will be corked in the memory 0
由於 uncork() 之後資料已經重新整理。該佇列將不會儲存任何資料,這就是返回的長度為 0 的原因。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP