Home >Web Front-end >JS Tutorial >Stream writable.cork() and uncork() methods in Node.js

Stream writable.cork() and uncork() methods in Node.js

WBOY
WBOYforward
2023-09-16 22:53:081380browse

Node.js 中的 Stream writable.cork() 和 uncork() 方法

writable.cork() method is used to force all written data to be buffered in memory. Buffered data is removed from buffer memory only after calling the stream.uncork() or stream.end() method.

Syntax

cork()

writeable.cork()

Cork()

writeable.uncork()

Parameters

Because it buffers the written data. The only required parameter will be writable data.

Example

Create a file called cork.js and copy the following code snippet. After creating the file, run this code using the following command as shown in the example below -

node cork.js

cork.js

Live Demo

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

Output

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

Only the data written between cork() methods will be printed, while the remaining data will be stuffed into the buffer memory. The example below shows how to unlock the above data from buffer memory.

Example

Let's look at another example on how to uncork() - uncork.js

Live Demonstration

// 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()

Output

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

After using the uncork() method to refresh the buffer memory, the complete data in the above example will be displayed.

The above is the detailed content of Stream writable.cork() and uncork() methods in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete