成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

怎么在node.js中使用stream模塊-創(chuàng)新互聯(lián)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在node.js中使用stream模塊,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),寧海企業(yè)網(wǎng)站建設(shè),寧海品牌網(wǎng)站建設(shè),網(wǎng)站定制,寧海網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,寧海網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

一、實(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);

上述就是小編為大家分享的怎么在node.js中使用stream模塊了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

分享題目:怎么在node.js中使用stream模塊-創(chuàng)新互聯(lián)
URL分享:http://jinyejixie.com/article12/dcjpdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站網(wǎng)站營(yíng)銷(xiāo)、做網(wǎng)站定制開(kāi)發(fā)、網(wǎng)站制作外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
山阴县| 余干县| 颍上县| 邮箱| 石屏县| 刚察县| 革吉县| 安龙县| 名山县| 公安县| 浪卡子县| 塔城市| 舞阳县| 华安县| 陕西省| 修水县| 偏关县| 伊川县| 玛纳斯县| 丰城市| 沐川县| 视频| 英山县| 泽普县| 许昌市| 琼中| 高淳县| 内黄县| 洛宁县| 赤壁市| 武平县| 龙泉市| 葫芦岛市| 广州市| 潍坊市| 玉环县| 桦川县| 故城县| 银川市| 霍山县| 玛多县|