当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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



相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | Stream writable.writableLength Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。