Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2023-09-16 22:53:08
forward
1332 people have browsed it

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()
Copy after login

Cork()

writeable.uncork()
Copy after login

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
Copy after login

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');
Copy after login

Output

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

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()
Copy after login

Output

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!