ホームページ  >  記事  >  ウェブフロントエンド  >  Node.js の writable.cork() メソッドと uncork() メソッドをストリームする

Node.js の writable.cork() メソッドと uncork() メソッドをストリームする

WBOY
WBOY転載
2023-09-16 22:53:081305ブラウズ

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

writable.cork() メソッドは、書き込まれたすべてのデータを強制的にメモリにバッファリングするために使用されます。バッファされたデータは、stream.uncork() メソッドまたは stream.end() メソッドを呼び出した後にのみバッファ メモリから削除されます。

構文

cork()

writeable.cork()

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

Output

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 の writable.cork() メソッドと uncork() メソッドをストリームするの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。