node.js使用stream模塊實(shí)現(xiàn)自定義流示例
本文實(shí)例講述了node.js使用stream模塊實(shí)現(xiàn)自定義流。分享給大家供大家參考,具體如下:
有些時(shí)候我們需要自定義一些流,來(lái)操作特殊對(duì)象,node.js中為我們提供了一些基本流類(lèi)。
我們新創(chuàng)建的流類(lèi)需要繼承四個(gè)基本流類(lèi)之一(stream.Writeable,stream.Readable,stream.Duplex,stream.Transform),并確保調(diào)用了父類(lèi)構(gòu)造函數(shù)。
一、實(shí)現(xiàn)自定義的可讀流
實(shí)現(xiàn)可讀流需繼承 stream.Readable,并實(shí)現(xiàn) readable._read() 方法。
下面的代碼我們實(shí)現(xiàn)了一個(gè)從數(shù)組中讀取數(shù)據(jù)的流
const {Readable} = require('stream');
//這里我們自定義了一個(gè)用來(lái)讀取數(shù)組的流
class ArrRead extends Readable {
constructor(arr, opt) {
//注意這里,需調(diào)用父類(lèi)的構(gòu)造函數(shù)
super(opt);
this.arr = arr;
this.index = 0;
}
//實(shí)現(xiàn) _read() 方法
_read(size) {
//如果當(dāng)前下標(biāo)等于數(shù)組長(zhǎng)度,說(shuō)明數(shù)據(jù)已經(jīng)讀完
if (this.index == this.arr.length) {
this.push(null);
} else {
this.arr.slice(this.index, this.index + size).forEach((value) => {
this.push(value.toString());
});
this.index += size;
}
}
}
let arr = new ArrRead([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], {
highWaterMark: 2
});
//這樣當(dāng)我們監(jiān)聽(tīng) 'data' 事件時(shí),流會(huì)調(diào)用我們實(shí)現(xiàn)的 _read() 方法往緩沖區(qū)中讀取數(shù)據(jù)
//然后提供給消費(fèi)者
arr.on('data', function (data) {
console.log(data.toString());
});
二、實(shí)現(xiàn)自定義的可寫(xiě)流
實(shí)現(xiàn)可寫(xiě)流必須繼承 stream.Writeable ,并實(shí)現(xiàn) writeable._write() 方法。writable._writev() 方法是可選的。
const {Writable} = require('stream');
//這里我們自定義了一個(gè)用來(lái)寫(xiě)入數(shù)組的流
class ArrWrite extends Writable {
constructor(arr, opt) {
super(opt);
this.arr = arr;
}
//實(shí)現(xiàn) _write() 方法
_write(chunk, encoding, callback) {
this.arr.push(chunk.toString());
callback();
}
}
let data = [];
let arr = new ArrWrite(data, {
highWaterMark: 3
});
arr.write('1');
arr.write('2');
arr.write('3');
console.log(data);
三、實(shí)現(xiàn)自定義的可讀可寫(xiě)流
可讀可寫(xiě)流必須繼承 stream.Duplex,并實(shí)現(xiàn) readable._read() 和 writable._write() 方法。
const {Duplex} = require('stream');
//這里我們自定義了一個(gè)用來(lái)寫(xiě)讀可寫(xiě)數(shù)組的流
class ArrReadWrite extends Duplex {
constructor(arr, opt) {
super(opt);
this.arr = arr;
this.index = 0;
}
//實(shí)現(xiàn) _write() 方法
_write(chunk, encoding, callback) {
this.arr.push(chunk.toString());
callback();
}
//實(shí)現(xiàn) _read() 方法
_read(size) {
//如果當(dāng)前下標(biāo)等于數(shù)組長(zhǎng)度,說(shuō)明數(shù)據(jù)已經(jīng)讀完
if (this.index == this.arr.length) {
this.push(null);
} else {
this.arr.slice(this.index, this.index + size).forEach((value) => {
this.push(value.toString());
});
this.index += size;
}
}
}
let data = [];
let arrWR = new ArrReadWrite(data, {
highWaterMark: 3
});
//往流中寫(xiě)入數(shù)據(jù)
arrWR.write('1');
arrWR.write('2');
arrWR.write('3');
console.log(data);
//往流中讀取數(shù)據(jù)
console.log(arrWR.read(2).toString());
console.log(arrWR.read(2).toString());
四、自定義的轉(zhuǎn)換流
轉(zhuǎn)換流必須繼承 stream.Transform,需實(shí)現(xiàn) transform._transform() 方法。
const {Transform} = require('stream');
//這里我們自定義了一個(gè)用來(lái)轉(zhuǎn)換數(shù)組的流
class Trans extends Transform {
constructor(opt) {
super(opt);
}
_transform(chunk, encoding, callback) {
//將轉(zhuǎn)換后的數(shù)據(jù)輸出到可讀流
this.push(chunk.toString().toUpperCase());
//參數(shù)一是Error對(duì)象
//參數(shù)二如果傳入,會(huì)被轉(zhuǎn)發(fā)到 readable.push()
callback();
}
}
let t = new Trans({
highWaterMark: 3
});
t.on('data', function (data) {
console.log(data.toString());
});
t.write('a');
t.write('b');
t.write('c');
轉(zhuǎn)換流就是將讀取到的數(shù)據(jù)做些計(jì)算然后輸出。轉(zhuǎn)換流既可以作為可讀流,又可以作為可寫(xiě)流。
const {Transform} = require('stream');
//這里我們自定義了一個(gè)用來(lái)轉(zhuǎn)換數(shù)組的流
class Trans extends Transform {
constructor(opt) {
super(opt);
}
_transform(chunk, encoding, callback) {
//將轉(zhuǎn)換后的數(shù)據(jù)輸出到可讀流
this.push(chunk.toString().toUpperCase());
//參數(shù)一是Error對(duì)象
//參數(shù)二如果傳入,會(huì)被轉(zhuǎn)發(fā)到 readable.push()
callback();
}
}
let t = new Trans({
highWaterMark: 3
});
t.on('data', function (data) {
console.log('data', data.toString());
});
//stdin.pipe(t) 表示將我們的標(biāo)準(zhǔn)輸入寫(xiě)入到我的轉(zhuǎn)換流 t 中,此時(shí) t 是可寫(xiě)流。
//pipe(process.stdout) 表示將轉(zhuǎn)換流 t 中的數(shù)據(jù)讀取到標(biāo)準(zhǔn)輸出中,此時(shí) t 是可讀流。
process.stdin.pipe(t).pipe(process.stdout);
希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。
- Node.js中的流(Stream)的作用詳解
- node.js同步/異步文件讀寫(xiě)-fs,Stream文件流操作實(shí)例詳解
- Node.js數(shù)據(jù)流Stream之Duplex流和Transform流用法
- Node.js數(shù)據(jù)流Stream之Readable流和Writable流用法
- node.js中stream流中可讀流和可寫(xiě)流的實(shí)現(xiàn)與使用方法實(shí)例分析
- Node.js中你不可不精的Stream(流)
- Node.js中流(stream)的使用方法示例
- Node.js中的流(Stream)介紹
- Node.js 中的流Stream模塊簡(jiǎn)介及如何使用流進(jìn)行數(shù)據(jù)處理
相關(guān)文章
node.js從數(shù)據(jù)庫(kù)獲取數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了node.js從數(shù)據(jù)庫(kù)獲取數(shù)據(jù)的具體代碼,nodejs可以獲取具體某張數(shù)據(jù)表信息,感興趣的朋友可以參考一下2016-05-05
node.js中的path.basename方法使用說(shuō)明
這篇文章主要介紹了node.js中的path.basename方法使用說(shuō)明,本文介紹了path.basename的方法說(shuō)明、語(yǔ)法、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12

