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

WBOY
リリース: 2023-09-16 22:53:08
転載
1333 人が閲覧しました

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 までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!