分类

nodejs 的 stream 有四种:

  • Readable:可读流
  • Writable: 可写流
  • Duplex:双工流
  • Transform:转换流

Readable

// _read方法是从底层系统读取具体数据的逻辑,即生产数据的逻辑。
// 在_read方法中,通过调用push(data)将数据放入可读流中供下游消耗。
// 在_read方法中,可以同步调用push(data),也可以异步调用。
// 当全部数据都生产出来后,必须调用push(null)来结束可读流。
// 流一旦结束,便不能再调用push(data)添加数据。
// 创建一个可以读的流,通过这个流对象监听数据
const stream = require('stream');
const Readable = stream.Readable; class MyFileReader extends Readable {
constructor(options) {
super(options);
} attachDataSource(dataSource) {
this.dataSource = dataSource;
} // 实现 _read 方法
_read() {
// 通过 DataSource 传入数据
// 当 DataSource get 方法返回 null 会触发 Readable 的 end 事件,表示会结束
this.push(this.dataSource.get());
}
} class DataSource {
constructor(size = 10) {
this.size = size;
this.data = []; this.fill();
} // 生产数据
fill() {
for (let index = 0; index < this.size; index++) {
this.data.push(index.toString());
}
} // 获取数据
get() {
if (this.data.length === 0) {
return null;
} return this.data.pop();
}
} const dataSource = new DataSource();
const myFileReader = new MyFileReader(); myFileReader.attachDataSource(dataSource);
myFileReader.on('data', (data) => console.log('data: ' + data));
myFileReader.on('end', () => console.log('end'));

在 _read 方法里,可以调用 push 方法往缓冲池里放入数据,这里写的this.push(this.dataSource.get())方法是一个同步方法,获取完数据后立马再次执行 _read 方法获取下一条数据,如果你在_read 方法里写一个异步push 的数据

 setTimeout(() => {
this.push(this.dataSource.get());
});

那么本次 _read 方法执行后,没有数据则不会执行下一次 _read 方法,直到 state 发生了变化,具体的细节可以参看:https://blog.csdn.net/shasharoman/article/details/80251512 这篇文章

Writable

// 上游通过调用writable.write(data)将数据写入可写流中。write()方法会调用_write()将data写入底层。
// 在_write中,当数据成功写入底层后,必须调用next(err)告诉流开始处理下一个数据。
// next的调用既可以是同步的,也可以是异步的。
// 上游必须调用writable.end(data)来结束可写流,data是可选的。此后,不能再调用write新增数据。
// 在end方法调用后,当所有底层的写操作均完成时,会触发finish事件。 const Writable = require('stream').Writable class ToWritable extends Writable{
constructor(){
super();
} _write(data, enc, next) {
// 将流中的数据写入底层
process.stdout.write(data.toString().toUpperCase());
// 写入完成时,调用`next()`方法通知流传入下一个数据
process.nextTick(next);
}
} const writable = new ToWritable(); writable.on('finish', () => process.stdout.write('DONE')); // 将一个数据写入流中
writable.write(`a\n`);
writable.write(`b\n`);
writable.write(`c\n`); // 再无数据写入流时,需要调用`end`方法
writable.end();

Duplex

// 代码中实现了_read方法,所以可以监听data事件来消耗Duplex产生的数据。
// 同时,又实现了_write方法,可作为下游去消耗数据。 const Duplex = require('stream').Duplex class ToDuplexable extends Duplex{
constructor(){
super();
this.limit = 0;
} _write(buf, enc, next) {
this.limit ++ ;
next();
} _read() {
if(this.limit--){
this.push('data');
}
// this.push(this.index++)
}
} const toDuplexable = new ToDuplexable(); toDuplexable.on('data', data => process.stdout.write(data));
toDuplexable.on('end', () => process.stdout.write('DONE')); toDuplexable.write(''); setTimeout(() => {
toDuplexable.write('');
toDuplexable.end();
}, 1000);

Transform

// Duplex 可读流中的数据(0, 1)与可写流中的数据('a', 'b')是隔离开的,但在Transform中可写端写入的数据经变换后会自动添加到可读端
//Tranform继承自Duplex,并已经实现了_read和_write方法,同时要求用户实现一个_transform方法。 const Transform = require('stream').Transform class MyTransform extends Transform{
constructor() {
super()
} _transform(buf, enc, next){
let res = buf.toString('utf8'); // 调用push方法将变换后的数据添加到可读端
this.push(`${res}_transform\n`);
// 调用next方法准备处理下一个
next();
}
} var myTransform = new MyTransform(); myTransform.on('data', data => process.stdout.write(data)); myTransform.write('abc');
myTransform.write('def');
myTransform.end();

参考:

nodejs stream基础知识的更多相关文章

  1. 【NodeJS】基础知识

    nodejs基础 nodejs允许自己封装模块,使得编写程序可以模块化,便于维护整理.在一个js文件中写完封装的函数或对象后,可以使用exports或module.exports来将模块中的函数暴露给 ...

  2. Stream基础知识

    Stream API Stream是Java8中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,但是将执行操作的时间交给具体实现来决定.例如,如果你希望计算某个方法的平均值,你可以在每个元素 ...

  3. 前端知识体系-NodeJS相关】NodeJS基础知识全面总结

    NodeJS基础知识 1. Node的全局对象和全局变量 1.1 全局对象:所有模块都可以调用的 global:表示Node所在的全局环境,类似于浏览器的window对象. process:该对象表示 ...

  4. 1.nodejs权威指南--基础知识

    1. 基础知识 1.1 全局作用域及函数 1.1.1 全局作用域 在nodejs中,定义了一个global对象,代表nodejs中的全局命名空间,任何全局变量.函数或对象都是该对象的一个属性值 1.1 ...

  5. JAVA基础知识|lambda与stream

    lambda与stream是java8中比较重要两个新特性,lambda表达式采用一种简洁的语法定义代码块,允许我们将行为传递到函数中.之前我们想将行为传递到函数中,仅有的选择是使用匿名内部类,现在我 ...

  6. nodejs+gulp+webpack基础知识

    nodejs+gulp+webpack基础知识 2019年08月22日 11:49:40 天府云创 阅读数 22   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文 ...

  7. NodeJs>------->>第三章:Node.js基础知识

    第三章:Node.js基础知识 一:Node.js中的控制台 1:console.log.console.info  方法 console.log(" node app1.js 1> ...

  8. C#网络编程基础知识

    C#网络编程基础知识一 1.IPAddress类 用于表示一个IP地址.IPAddress默认构造函数 public IPAddress(long address);一般不用 其中Parse()方法最 ...

  9. JAVA基础知识之网络编程——-网络基础(Java的http get和post请求,多线程下载)

    本文主要介绍java.net下为网络编程提供的一些基础包,InetAddress代表一个IP协议对象,可以用来获取IP地址,Host name之类的信息.URL和URLConnect可以用来访问web ...

随机推荐

  1. rebound是facebook的开源动画库

    网址:http://www.jcodecraeer.com/a/opensource/2015/0121/2338.html 介绍: rebound是facebook的开源动画库.可以认为这个动画库是 ...

  2. [转]maven2中snapshot快照库和release发布库的应用

    [转载声明] 转载时必须标注:本文来源于铁木箱子的博客http://www.mzone.cc [原文地址] 原文永久地址是:http://www.mzone.cc/article/279.html 在 ...

  3. 查看apache,nginx,mysql,linux,php版本

    查看apache版本 /usr/sbin/apachectl -v httpd -v 安装目录,使用apachectl -v mysql版本查看 mysql -V 查看linux版本 1.cat /e ...

  4. 微信小程序之如何注册微信小程序

    所有文章均是CSDN博客所看,已按照作者要求,注明出处了,感谢作者的整理! 博客文章地址:http://blog.csdn.net/michael_ouyang/article/details/546 ...

  5. python 可变参数函数定义* args和**kwargs的用法

    python函数可变参数 (Variable Argument) 的方法:使用*args和**kwargs语法.其中,*args是可变的positional arguments列表,**kwargs是 ...

  6. [CMD]重启电脑

    https://zhidao.baidu.com/question/686086701903450132.html bat是批处理,可以调用关机命令关机. 制作方法如下: 打开记事本程序: 输入如下内 ...

  7. WinForm开发----关闭window窗体最好的办法

    最近有一人问道,如何切换窗体.一想到这,我就想,不就是new一个form,然后就show么? 可是我发现,当你控制某个属性的时候,不是不能控制,只是很麻烦而已.有没有好的办法?当然有,咋办? 最简单最 ...

  8. java 基于tomcat的数据源案例

    1.在context中定义数据源 <?xml version="1.0" encoding="UTF-8"?> <Context path=& ...

  9. images have the “stationarity” property, which implies that features that are useful in one region are also likely to be useful for other regions.

    Convolutional networks may include local or global pooling layers[clarification needed], which combi ...

  10. python元组和列表区别

    元组可以简单认为是一个只读的列表 tuper = const list