stream在Unix系统中是个标准的概念。
In computer programming, standard 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的实例,故而可以以事件驱动的方式执行异步。
下面是在慕课网学习时跟着编的一个例子:
1 var readstream=fs.createReadStream(‘./xiaoyu.mp4‘); 2 var writestream=fs.createWriteStream(‘./xiaoyu2.mp4‘); 3 readstream.on(‘data‘,function(chunk){ 4 if(writestream.write(chunk)===false){ 5 console.log(‘still cached‘) 6 readstream.pause(); 7 } 8 }) 9 readstream.on(‘end‘,function(){ 10 writestream.end(); 11 }) 12 writestream.on(‘drain‘,function(){ 13 console.log(‘drain‘) 14 readstream.resume() 15 }) 16 //直接pipe() 17 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