📜  Node.js Stream readable.readableLength 属性

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

Node.js Stream readable.readableLength 属性

可读 Stream 中的readable.readableLength 属性,用于检查队列中准备好读取的字节数。

句法:

readable.readableLength

返回值:它返回队列中准备好读取的字节数。

下面的示例说明了 Node.js 中readable.readableLength 属性的使用:

示例 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
read: 13

参考: https://nodejs.org/api/stream.html#stream_readable_readablelength。