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.
cork()
writeable.cork()
Cork()
writeable.uncork()
Because it buffers the written data. The only required parameter will be writable data.
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');
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.
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()
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!