模块/stream.readable.md
flowing mode + paused mode
默认:paused mode(刚创建时)
paused mode -> flowing mode 几种方式
var fs = require('fs');
var readstream = fs.createReadStream('./hello.txt');
console.log('1. isPaused: ' + readstream.isPaused());
setTimeout(function() {
readstream.on('data', function (content) {
console.log(`3. content is [ %s ]` + content);
});
console.log('2. isPaused: ' + readstream.isPaused());
}, 3000);
输出如下:
1. isPaused: false
2. isPaused: false
3. content is [ %s ]hello world
分别解释下:
readable.pause()、readable.unpipe(),会将状态置为false。需要注意的时,状态为false不代表不生产数据。有可能没有地方消费数据,但数据还在继续产生,并在internal buffer里缓存起来。var fs = require('fs');
var readstream = fs.createReadStream('./hello.txt');
readstream.on('data', function (chunk) {
console.log('on data: %s', chunk);
});
readstream.on('close', function () {
console.log('on close');
});
输出:
on data: hello world
on close
var fs = require('fs');
var readstream = fs.createReadStream('./hello.txt');
var readstream2 = fs.createReadStream('./hello.txt');
var readstream3 = fs.createReadStream('./hello.txt');
readstream.on('data', function (chunk) {
console.log('1. chunk type is Buffer ? %s', Buffer.isBuffer(chunk));
});
readstream2.setEncoding('utf8');
readstream2.on('data', function (chunk) {
console.log('2. chunk type is String ? %s', typeof chunk === 'string');
});
readstream3.setEncoding('utf8');
readstream3.on('data', function (chunk) {
console.log('3. data is: %s', chunk);
});
// 输出
// 1. chunk type is Buffer ? true
// 2. chunk type is String ? true
// 3. data is: hello world
Attaching a 'data' event listener to a stream that has not been explicitly paused will switch the stream into flowing mode. Data will then be passed as soon as it is available.
var fs = require('fs');
var readstream = fs.createReadStream('./hello.txt');
readstream.on('data', function (chunk) {
console.log('on data: %s', chunk);
});
readstream.on('end', function () {
console.log('on end');
});
readstream.on('close', function () {
console.log('on close');
});
// 输出:
// on data: hello world
// on end
// on close
var fs = require('fs');
var readstream = fs.createReadStream('./none-exists.txt');
readstream.on('error', function (error) {
console.log('on error: %s', error.message);
});
// 输出:
// on error: ENOENT: no such file or directory, open './none-exists.txt'
如果没有添加 error 事件监听,报错并退出
events.js:160
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open './none-exists.txt'
at Error (native)
var fs = require('fs');
var readstream = fs.createReadStream('./hello.txt');
readstream.on('readable', function() {
console.log('readable: %s', readstream.read());
});
readstream.on('end', function() {
console.log('end');
});
// 输出:
// readable: hello world
// readable: null
// end
stream.pipe(dest, {end: true})
stream.pipe(dest)返回dest,也就是说可以链式调用 stream.pipe(dest1).pipe(dest2)...
var fs = require('fs');
var r = fs.createReadStream('./hello.txt');
var z = require('zlib').createGzip();
var w = fs.createWriteStream('./hello.txt.gz');
r.pipe(z).pipe(w);
当end为true(默认)时,当数据读取结束,dest会自动关闭;否则dest不自动关闭。
例子:end事件触发时,dest已经被关闭,此时再往dest写数据,报错
var fs = require('fs');
var src = fs.createReadStream('./hello.txt');
var dest = fs.createWriteStream('./dest.txt');
src.pipe(dest);
src.on('end', function () {
try{
dest.end(' end');
}catch(error){
console.log('error! error.message is %s', error.message);
}
console.log('end');
});
// 输出:
// error! error.message is write after end
// end
显示声明end为true,成功写入。
var fs = require('fs');
var src = fs.createReadStream('./hello.txt');
var dest = fs.createWriteStream('./dest.txt');
src.pipe(dest, { end: false });
src.on('end', function () {
dest.end(' end');
console.log('end');
});
作用:读取internal buffer中的数据(在paused模式下使用) 参数说明:size,要读取的字节数 返回:Buffer 或者 String 或者 null
如果指定了size,且
The optional size argument specifies a specific number of bytes to read. If size bytes are not available to be read, null will be returned unless the stream has ended, in which case all of the data remaining in the internal buffer will be returned (even if it exceeds size bytes).
如果没有指定size,那么,internal buffer中的所有数据一次性返回。
If the size argument is not specified, all of the data contained in the internal buffer will be returned.
如果 readable.read() 返回了数据,那么 data 事件会被触发。
Note: If the readable.read() method returns a chunk of data, a 'data' event will also be emitted.
通过 fs.createReadStream(path, options) 创建的 stream,internal buffer 的大小为 64kb
Be aware that, unlike the default value set for highWaterMark on a readable stream (16 kb), the stream returned by this method has a default value of 64 kb for the same parameter.
看例子:
var fs = require('fs');
var readable = fs.createReadStream('./jquery-3.2.1.js');
readable.on('readable', function (chunk) {
var chunk;
while (null !== (chunk = readable.read())) {
console.log(`Received ${Math.ceil(chunk.length/1024)} kb of data.`);
}
});
// 输出
// Received 64 kb of data.
// Received 64 kb of data.
// Received 64 kb of data.
// Received 64 kb of data.
// Received 6 kb of data.
试下指定size
var fs = require('fs');
var readable = fs.createReadStream('./jquery-3.2.1.js');
var size = 1024 * 32; // 32k
readable.on('readable', function (chunk) {
var chunk;
while (null !== (chunk = readable.read(size))) {
console.log(`Received ${Math.ceil(chunk.length/1024)} kb of data.`);
}
});
// 输出
// Received 32 kb of data.
// Received 32 kb of data.
// Received 32 kb of data.
// Received 32 kb of data.
// Received 32 kb of data.
// Received 32 kb of data.
// Received 32 kb of data.
// Received 32 kb of data.
// Received 6 kb of data.
作用:停止向 destination 写入数据。 参数:destination 可选,如果没有指定,则对所有 dest 的写入都被停止。
例子:略
Note: The stream.unshift(chunk) method cannot be called after the 'end' event has been emitted or a runtime error will be thrown.
Developers using stream.unshift() often should consider switching to use of a Transform stream instead. See the API for Stream Implementers section for more information.