Node.js | Stream readable.readableLength Свойство

Опубликовано: 3 Августа, 2021

Свойство readable.readableLength в читаемом Stream, которое используется для проверки количества байтов в очереди, готовых к чтению.

Синтаксис:

 readable.readableLength

Возвращаемые значения: возвращает количество байтов в очереди, готовых к чтению.

Примеры ниже иллюстрируют использование свойства readable.readableLength в Node.js:

Пример 1:




// Node.js program to demonstrate the     
// readable.readableLength Property
   
// Accessing stream module
const stream = require("stream");
   
// Creating a Readable stream 
const readable = new stream.Readable("input.txt");
   
// Calling readable.readableLength 
// Property
readable.readableLength;

Выход:

 0

Пример 2:




// Node.js program to demonstrate the
// readable.readableLength property
// Include fs module
const fs = require( "fs" );
// Constructing readable stream
const readable = fs.createReadStream( "input.txt" );
// Instructions for reading data
readable.on( 'readable' , () => {
let chunk;
// Using while loop and calling
// read method
while ( null !== (chunk = readable.read())) {
// Displaying the chunk length
console.log(`read: ${chunk.length}`);
}
});
// Calling readableLength property
readable.readableLength;

Выход:

 0
читать: 13

Ссылка: https://nodejs.org/api/stream.html#stream_readable_readablelength.