欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Node.js數(shù)據(jù)流Stream之Readable流和Writable流用法

 更新時間:2022年07月06日 09:21:21   作者:社會主義接班人  
這篇文章介紹了Node.js數(shù)據(jù)流Stream之Readable流和Writable流的用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、前傳

Stream在很多語言都會有,當(dāng)然Node.js也不例外。數(shù)據(jù)流是可讀、可寫、或即可讀又可寫的內(nèi)存結(jié)構(gòu)。Node.js中主要包括Readable、Writable、Duplex(雙工)和Transform(變換)流。但是在學(xué)這些之前先學(xué)會util模塊中的一個從其他對象繼承的功能.

util模塊提供了util.inherits()方法來允許你創(chuàng)建一個繼承另一個對象的prototype(原形)方法的對象。當(dāng)創(chuàng)建一個新對象時,prototype方法自動被使用。

util.inherits(constructor,superconstructor)原形constructor被設(shè)定為原形superConstructor,并在一個新的對象被創(chuàng)建時執(zhí)行。可以通過使用constructor.super_屬性從自定義對象的構(gòu)造函數(shù)訪問supercontructor.

二、Readable流

有的前傳util模塊從其他對象繼承的功能的了解,Readable就很好理解了.主要它包含以下方法和事件。

1.事件:

readable:在數(shù)據(jù)塊可以從流中讀取的時候發(fā)出。

data:類似readable,不同之處在于,當(dāng)數(shù)據(jù)的事件處理程序被連接時,流被轉(zhuǎn)變成流動的模式,并且數(shù)據(jù)處理程序被連續(xù)的調(diào)用,直到所有數(shù)據(jù)都被用盡

end:當(dāng)數(shù)據(jù)不再被提供時由流發(fā)出

close:當(dāng)?shù)讓淤Y源,如文件,已關(guān)閉時發(fā)出。

error:在接收數(shù)據(jù)中出錯是發(fā)出。

2.方法:

read([size]):從流中讀數(shù)據(jù).數(shù)據(jù)可以是String、Buffer、null(下面代碼會有),當(dāng)指定size,那么只讀僅限于那個字節(jié)數(shù)

setEncoding(encoding):設(shè)置read()請求讀取返回String時使用的編碼

pause():暫停從該對象發(fā)出的data事件

resume():恢復(fù)從該對象發(fā)出的data事件

pipe(destination,[options]):把這個流的輸出傳輸?shù)揭粋€由deatination(目的地)指定的Writable流對象。options是一個js對象.例如:{end:true}當(dāng)Readable結(jié)束時就結(jié)束Writable目的地。

unpipe([destination]):從Writale目的地斷開這一對象。

3.demo:

var stream = require('stream');
var util = require('util');
util.inherits(Answers, stream.Readable);
function Answers(opt) {
  stream.Readable.call(this, opt);
  this.quotes = ["yes", "no", "maybe"];
  this._index = 0;
}
Answers.prototype._read = function() {
  if (this._index > this.quotes.length){
    this.push(null);
  } else {
    this.push(this.quotes[this._index]);
    this._index += 1;
  }
};
var r = new Answers();
console.log("Direct read: " + r.read().toString());
r.on('data', function(data){
  console.log("Callback read: " + data.toString());
});
r.on('end', function(data){
  console.log("No more answers.");
});
r.on('readable',function(data)
{
   console.log('readable');
});

輸出結(jié)果:

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe stream_read.js
Direct read: yes
readable
Callback read: no
Callback read: maybe
readable
readable
readable
No more answers.

Process finished with exit code 0

上面定義了一個通過util.inherits()繼承Readable流的對象,從輸出結(jié)果可以看到輸出了3個字符串,但readable事件確執(zhí)行了4次,其實前面也有寫,read()可以是null,最后是push(null)了。

三、Writable流

有讀就會有寫,畢竟是可逆的,它和readable一樣也有一些事件和方法

1.方法

write(chunk,[encoding],[callback]):將數(shù)據(jù)寫入流。chunk(數(shù)據(jù)塊)中包含要寫入的數(shù)據(jù),encoding指定字符串的編碼,callback指定當(dāng)數(shù)據(jù)已經(jīng)完全刷新時執(zhí)行的一個回調(diào)函數(shù)。如果成功寫入,write()返回true.

end([chunk],[encoding],[callback]):與write()相同,它把Writable對象設(shè)為不再接受數(shù)據(jù)的狀態(tài),并發(fā)送finish事件。

2.事件

drain:在write()調(diào)用返回false后,當(dāng)準(zhǔn)備好開始寫更多數(shù)據(jù)時,發(fā)出此事件通知監(jiān)視器。

finish:當(dāng)end()在Writable對象上調(diào)用,所以數(shù)據(jù)被刷新,并不會有更多的數(shù)據(jù)被接受時觸發(fā)

pipe:當(dāng)pipe()方法在Readable流上調(diào)用,已添加此writable為目的地時發(fā)出

unpipe:當(dāng)unpipe()方法被調(diào)用,以刪除Writable為目的地時發(fā)出。

3.demo

var stream = require('stream');
var util = require('util');
util.inherits(Writer, stream.Writable);
function Writer(opt) {
  stream.Writable.call(this, opt);
  this.data = new Array();
}
Writer.prototype._write = function(data, encoding, callback) {
  this.data.push(data.toString('utf8'));
  console.log("Adding: " + data);
  callback();
};
var w = new Writer();
for (var i=1; i<=5; i++){  
  w.write("Item" + i, 'utf8');
}
w.end("ItemLast");
console.log(w.data);

輸出結(jié)果:

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe stream_write.js
Adding: Item1
Adding: Item2
Adding: Item3
Adding: Item4
Adding: Item5
Adding: ItemLast
[ 'Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'ItemLast' ]

Process finished with exit code 0

四、把Readable流用管道輸送到Writable流

上面也介紹了Readable流pipe()方法,這個主要是來測試

var stream = require('stream');
var util = require('util');
util.inherits(Reader, stream.Readable);
util.inherits(Writer, stream.Writable);
function Reader(opt) {
  stream.Readable.call(this, opt);
  this._index = 1;
}
Reader.prototype._read = function(size) {
  var i = this._index++;
  if (i > 10){
    this.push(null);
  } else {
    this.push("Item " + i.toString());
  }
};
function Writer(opt) {
  stream.Writable.call(this, opt);
  this._index = 1;
}
Writer.prototype._write = function(data, encoding, callback) {
  console.log(data.toString());
  callback();
};

var r = new Reader();
var w = new Writer();
w.on('pipe',function(){
  console.log('pipe');
});
r.pipe(w);

輸出結(jié)果:

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe stream_piped.js
pipe
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10

Process finished with exit code 0

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論