stream在Unix系统中是个标准的概念。

In computer programmingstandard streams are preconnected input and output communication channels[1] between a computer program and its environment when it begins execution. The three I/O connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console (input via keyboard, output via monitor), but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection, e.g. via a pipeline. More generally, a child process will inherit the standard streams of its parent process.

以上摘自维基,说‘标准流’是在计算机程序开始执行时,已经预先在计算机程序及其运行环境间(个人觉得终端和文件系统间比较好理解)搭建好的输入输出通信管道。标准流抽象了原先的输入输出,更专注在数据的流通层面。

流的一个重要特征是,将数据的流通看成是一段一段的;也就是说一个文件,你不需要整个完全下载完才能操作,可以一部分一部分地读取,对已经读取的部分进行边操作。当然,不同语言对流都有各自的接口实现,不过抽象意义上是一样的。

在nodejs中,流Stream也是一个抽象的接口。在很多模块中都有体现,http、TCP socket、fs等;

而且流是以buffer的形式存在,非常方便我们对其操作;

另外,所有的流都是eventEmitter的实例,故而可以以事件驱动的方式执行异步。

下面是在慕课网学习时跟着编的一个例子:

 var readstream=fs.createReadStream('./xiaoyu.mp4');
var writestream=fs.createWriteStream('./xiaoyu2.mp4');
readstream.on('data',function(chunk){
if(writestream.write(chunk)===false){
console.log('still cached')
readstream.pause();
}
})
readstream.on('end',function(){
writestream.end();
})
writestream.on('drain',function(){
console.log('drain')
readstream.resume()
})
//直接pipe()
fs.createReadStream('/path/to/source').pipe(fs.createWriteStream('/path/to/dest'));

合着在网上看到的一些资料做个说明。fs.readFile是把整个文件读到内存中的,但是用.createReadFile()和.createWriteFile()则是读一部分写一部分,适合大文件按读写的情况。

代码第4行:writestream.write(chunk):true代表chunk已经写完,可以继续读入,false表示不应该继续读入,要.pause()。

--The return value is true if the internal buffer is less than the highWaterMark configured when the stream was created after admitting chunk. If false is returned, further attempts to write data to the stream should stop until the 'drain' event is emitted.

stream有4中类型:Readble、Writable、Duplex、Transform

Readble:可读流,提供数据 .pause() .resume()

Writable:可写流,消费data,从Readble读取数据后对buffer处理

Duplex:实现了上面两个的接口,有TCP sockets 、zlib streams 、crypto streams

Transform:也继承了读写接口,但是不缓存数据,只是处理经过它的数据,有zlib streams 、crypto streams

node的stream的更多相关文章

  1. 9、Node.js Stream(流)

    #########################################################################介绍Node.js Stream(流)Stream 是 ...

  2. Node.js stream 流学习

    由于node.js 创建http 是这样的 http.createServer(function(request,response){}).listen(2000); 里面的request 就是rea ...

  3. Node.js Stream(流)

    Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请求的request 对象就是一个 Stream,还有stdout(标准输出). Node.js,Str ...

  4. node.js stream

    stream是一个接口,流是可以从一个读取或写入数据的目标对象 ,Node 中有很多对象实现了这个接口   一.nodejs stream类型 1. Readable - 可读操作. Writable ...

  5. [Node.js] Stream all things!

    Node.js come alone with many Stream API. Stream is useful when handling large trunck of data. For ex ...

  6. 24.Node.js Stream(流)

    转自:http://www.runoob.com/nodejs/nodejs-stream.html Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请 ...

  7. 每天学点node系列-stream

    在编写代码时,我们应该有一些方法将程序像连接水管一样连接起来 -- 当我们需要获取一些数据时,可以去通过"拧"其他的部分来达到目的.这也应该是IO应有的方式. -- Doug Mc ...

  8. Node.js Stream - 实战篇

    邹斌 ·2016-07-22 11:04 背景 前面两篇(基础篇和进阶篇)主要介绍流的基本用法和原理,本篇从应用的角度,介绍如何使用管道进行程序设计,主要内容包括: 管道的概念 Browserify的 ...

  9. node.js Stream Buffer FsPromise

    Stream: 类似这样:a.pipe(b).pipe(c); 我想写一个b.所以: var rs=new (require('stream').Readable)(); var ws=new (re ...

  10. Node.js——Stream

    介绍 文件流:我们一般对大一点的文件实现stream的方式进行操作 http:显然http.createServer创建过程中的IncomingMessage实现了可读流的接口,ServerRespo ...

随机推荐

  1. PHP 获取前两页的url地址

    通过隐藏表单控件 <input type="hidden" name="prevurl" value="<?php echo $_SERV ...

  2. 安装宝塔检测到系统已存在Apache,请使用纯净安装

    执行命令 停止服务 net stop Apache2.4 删除服务 sc delete apache

  3. Ubuntu syslog 太多的 named[1195]: error (network unreachable) resolving './DNSKEY/IN': 2001:7fd::1#53

    Edit file /etc/default/bind9: # run resolvconf? RESOLVCONF=yes # startup options for the server OPTI ...

  4. 一篇文章带你编写10种语言HelloWorld

    0,编程语言排行榜 计算机编程语言众多,世界上大概有600 多种编程语言,但是流行的也就几十种.我们来看下编程语言排行榜,下面介绍两种语言排行榜. Ⅰ TIOBE 指数 该指数每月更新一次,它监控了近 ...

  5. Spring Boot @EnableAutoConfiguration和 @Configuration的区别

    Spring Boot @EnableAutoConfiguration和 @Configuration的区别 在Spring Boot中,我们会使用@SpringBootApplication来开启 ...

  6. Spring5参考指南:Bean的生命周期管理

    文章目录 Spring Bean 的生命周期回调 总结生命周期机制 startup和Shutdown回调 优雅的关闭Spring IoC容器 Spring Bean 的生命周期回调 Spring中的B ...

  7. windows下安装Pycham2020软件

    1.在pycham官网下载安装软件https://www.jetbrains.com/pycharm/download/#section=windows 2.我下载的是64位的安装包,现在开始安装 3 ...

  8. POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  9. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

  10. POJ Building a Space Station 最小生成树

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15664   Accepted: 6865 Description You ...