Kaedah
writable.cork() digunakan untuk memaksa semua data bertulis disimpan dalam ingatan. Data buffer dialih keluar daripada memori penimbal hanya selepas memanggil kaedah stream.uncork() atau stream.end().
cork()
writeable.cork()
#🎜🎜🎜##🎜🎜🎜#🎜🎜🎜#cork 🎜#
writeable.uncork()
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
Hanya data yang ditulis antara kaedah cork() akan dicetak, manakala data yang selebihnya akan disumbat ke dalam memori penimbal. Contoh di bawah menunjukkan cara untuk membuka kunci data di atas daripada memori penimbal.
Mari kita lihat contoh lain tentang cara membuka gabus() - 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()
Selepas mengepam memori penimbal menggunakan kaedah uncork(), data lengkap dalam contoh di atas akan dipaparkan.
Atas ialah kandungan terperinci Strim kaedah writeable.cork() dan uncork() dalam Node.js. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!