node.js – “可读”事件发生两次

前端之家收集整理的这篇文章主要介绍了node.js – “可读”事件发生两次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
var fs = require('fs');

var file = fs.createReadStream('./zeros.txt');
var dataSize = 0;

file.on('readable',function () {
    var data = file.read(10);
    console.log('readable size = ',data.length);
    console.log(data.toString());
});

文件“zeros.txt”包含700个字符“0”

据我所知,在调用read(10)之后,stream必须停止并等待新的read()调用.但是,调用的结果是:

readable size =  10
0000000000
readable size =  10
0000000000

解决方法

在Node.js将文件(整个或只是一部分,取决于文件本身的大小)加载到缓冲区(使用push()方法)之后,它将发出可读事件以指示某些数据已被读入缓冲并准备使用.然后在调用read(10)之后,释放缓冲区,然后Node.js将再次自动填充缓冲区并再次发出可读事件,以指示仍有一些数据要从缓冲区中读取.如果您将调用read(700),则不会再次发出下一个可读事件.

流动和非流动模式

与监听数据事件不同,流将保持在所谓的非流动模式.这意味着开发人员将负责释放流(从流中读取).另一方面,当监听数据事件时,流将自动进入所谓的流动模式,这意味着流本身将负责释放自身,即流将填充并清空自己直到他的底层系统(在这种情况下,zero.txt将被完全读取).请注意,缓冲区将在任一模式下自动填充数据.

流动模式

非流动模式的一个示例,我们必须手动清空缓冲区(使用read()方法):

var fs = require('fs'),util = require('util');

// I have increased the file size to 19 MB (about 19 mln characters);
// Cause of the buffer capicity.
var file = fs.createReadStream('./zeros.txt'); 
var dataSize = 0;

// Readable will be called when the buffer has been filled with data.
// Initially Node.js will fill the buffer with data automatically,// so this event will be called automatically aswell of course.
// Once the buffer will be free again after the first fill,Node.js
// will fill the buffer automatically again. Node.js just watches this stream
// and makes sure to fill it,when there is still some unread data in the zero.txt file.
file.on('readable',function() {
var i = 0; // we will count how many times did while loop,for fun

// If the buffer will be empty Node will write data to the buffer
// automatically,we don't have to care about that. However
// you can specify the buffer capicty manually if you want.
console.log('loading more data from the underlying system');

// This will make the stream read 1000 bytes
// it will also return a value NULL if there is not enough 
// data to read from the buffer (meaning buffer has been fully read 
// or there is still some data but you are trying to read 1000 bytes 
// and there is less than 1000 bytes left)
while(file.read(1000) !== null) {
    i++;
}
// At this moment while loop has read everything from the buffer.
// The buffer is now empty. After this comment console.log will execute
// Node.js will fill the buffer again with new data automatically.
// And then the 'readable' event will fire again.
console.log("had to loop: " + i + " times before the buffer was empty");
})

控制台的最后几个结果:

loading more data from the underlying system
had to loop: 66 times before the buffer was empty
loading more data from the underlying system
had to loop: 65 times before the buffer was empty
loading more data from the underlying system
had to loop: 66 times before the buffer was empty
loading more data from the underlying system
had to loop: 46 times before the buffer was empty
loading more data from the underlying system
had to loop: 1 times before the buffer was empty

非流动模式

这是非流动模式,因为我们必须手动释放缓冲区.现在我们将进入流动模式.在可读流上设置数据事件侦听器会将流从初始非流动模式切换到流动模式.这意味着缓冲区将自动清空. Node.js会将数据作为参数传递给数据事件监听器,一旦该函数执行缓冲区将再次清空,如果底层源缓冲区中仍有一些数据将自动填充新数据,然后数据事件将再次发出.注意:如果您正在侦听数据事件和可读事件,则两者都将触发,但数据事件侦听器将首先清空缓冲区,然后可读事件将触发,因此read()将始终返回NULL.

var fs = require('fs'),util = require('util');

var file = fs.createReadStream('./zeros.txt');
var dataSize = 0;

file.on('data',function() {
    // Once this listener will stop executing new data will be read
    // into the buffer and then the 'data' event will be emitted
    // again.
    console.log('data has been loaded and emptied!')
})

file.on('readable',function () {
    // Notice we want to try to read 1 byte from the buffer
    // but in the console we see that the read() method
    // resulted in NULL,which means that the buffer is empty.
    // That's of course because we enterd the flowing mode
    // by setting up the 'data' event. (In flowing mode)
    // after the execution of the 'data' event all data
    // from the buffer will be read,but the execution
    // of listeners will continue. After all the event listeners
    // attached to this stream will execute,Node.js will fill
    // the buffer automatically again.
    console.log('readable ' + file.read(1))
});

控制台的最后几个结果:

data has been loaded and emptied!
readable null
data has been loaded and emptied!
readable null
data has been loaded and emptied!
readable null
data has been loaded and emptied!
readable null
data has been loaded and emptied!
readable null

猜你在找的Node.js相关文章