公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

writable.cork()方法用于强制所有写入的数据缓冲在内存中。只有在调用stream.uncork()或stream.end()方法后,缓冲数据才会从缓冲存储器中删除。

语法

cork()

writeable.cork()

登录后复制

开塞()

writeable.uncork()

登录后复制

参数

因为它缓冲写入的数据。唯一需要的参数将是可写数据。

示例

创建一个名为 cork.js 的文件并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示 –

node cork.js

登录后复制

cork.js

 现场演示

// 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
writable.write('Hi - This data is printed');

// 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');

登录后复制

输出

C:\homeode>> node cork.js
Hi - This data is printed

登录后复制

只有在 cork() 方法之间写入的数据才会被打印,而其余数据将被塞入缓冲存储器中。下面的示例展示了如何从缓冲区内存中解锁上述数据。

示例

让我们再看一个有关如何 uncork() 的示例 – uncork.js

 现场演示

// 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
writable.write('Hi - This data is printed');

// 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');

// Flushing the data from buffered memory
writable.uncork()

登录后复制

输出

C:\homeode>> node uncork.js
Hi - This data is printed
Welcome to TutorialsPoint !
SIMPLY LEARNING
This data will be corked in the memory

登录后复制

使用 uncork() 方法刷新缓冲内存后,就会显示上面示例中的完整数据。

以上就是Node.js 中的 Stream writable.cork() 和 uncork() 方法的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:JS Node Stream uncork 方法
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定