1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      怎么在node.js中使用stream模塊

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

      成都創(chuàng)新互聯(lián)2013年開創(chuàng)至今,先為北川羌族等服務(wù)建站,北川羌族等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為北川羌族企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

      一、實現(xiàn)自定義的可讀流

      實現(xiàn)可讀流需繼承 stream.Readable,并實現(xiàn) readable._read() 方法。

      下面的代碼我們實現(xiàn)了一個從數(shù)組中讀取數(shù)據(jù)的流

      const {Readable} = require('stream');
      //這里我們自定義了一個用來讀取數(shù)組的流
      class ArrRead extends Readable {
        constructor(arr, opt) {
          //注意這里,需調(diào)用父類的構(gòu)造函數(shù)
          super(opt);
          this.arr = arr;
          this.index = 0;
        }
        //實現(xiàn) _read() 方法
        _read(size) {
          //如果當(dāng)前下標(biāo)等于數(shù)組長度,說明數(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)聽 'data' 事件時,流會調(diào)用我們實現(xiàn)的 _read() 方法往緩沖區(qū)中讀取數(shù)據(jù)
      //然后提供給消費者
      arr.on('data', function (data) {
        console.log(data.toString());
      });

      二、實現(xiàn)自定義的可寫流

      實現(xiàn)可寫流必須繼承 stream.Writeable ,并實現(xiàn) writeable._write() 方法。writable._writev() 方法是可選的。

      const {Writable} = require('stream');
      //這里我們自定義了一個用來寫入數(shù)組的流
      class ArrWrite extends Writable {
        constructor(arr, opt) {
          super(opt);
          this.arr = arr;
        }
        //實現(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);

      三、實現(xiàn)自定義的可讀可寫流

      可讀可寫流必須繼承 stream.Duplex,并實現(xiàn) readable._read() 和 writable._write() 方法。

      const {Duplex} = require('stream');
      //這里我們自定義了一個用來寫讀可寫數(shù)組的流
      class ArrReadWrite extends Duplex {
        constructor(arr, opt) {
          super(opt);
          this.arr = arr;
          this.index = 0;
        }
        //實現(xiàn) _write() 方法
        _write(chunk, encoding, callback) {
          this.arr.push(chunk.toString());
          callback();
        }
        //實現(xiàn) _read() 方法
        _read(size) {
          //如果當(dāng)前下標(biāo)等于數(shù)組長度,說明數(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
      });
      //往流中寫入數(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,需實現(xiàn) transform._transform() 方法。

      const {Transform} = require('stream');
      //這里我們自定義了一個用來轉(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對象
          //參數(shù)二如果傳入,會被轉(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ù)做些計算然后輸出。轉(zhuǎn)換流既可以作為可讀流,又可以作為可寫流。

      const {Transform} = require('stream');
      //這里我們自定義了一個用來轉(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對象
          //參數(shù)二如果傳入,會被轉(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)輸入寫入到我的轉(zhuǎn)換流 t 中,此時 t 是可寫流。
      //pipe(process.stdout) 表示將轉(zhuǎn)換流 t 中的數(shù)據(jù)讀取到標(biāo)準(zhǔn)輸出中,此時 t 是可讀流。
      process.stdin.pipe(t).pipe(process.stdout);

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


      分享名稱:怎么在node.js中使用stream模塊
      標(biāo)題鏈接:http://ef60e0e.cn/article/giieoe.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        连山| 青阳县| 团风县| 扶绥县| 阜平县| 刚察县| 奇台县| 鹤壁市| 孝昌县| 稷山县| 眉山市| 吴旗县| 武城县| 府谷县| 昌图县| 天全县| 琼中| 石台县| 阿鲁科尔沁旗| 新河县| 昌平区| 镇康县| 定襄县| 孝昌县| 色达县| 铜鼓县| 南部县| 温泉县| 咸阳市| 潮州市| 满洲里市| 镇赉县| 三亚市| 南平市| 江源县| 峨边| 子长县| 北票市| 泽库县| 姜堰市| 长武县|