我有一个fs.ReadStream对象指向一个非常大的文件。现在我想从ReadStream中读取8000个字节,但内部缓冲区只有6000个字节。所以我的方法是读取那6000个字节并等待内部缓冲区再次填满,使用while循环检查内部缓冲区长度是否不再为0。
这样的事情:
BinaryObject.prototype.read = function(length) {
var value;
// Check whether we have enough data in the internal buffer
if (this.stream._readableState.length < length) {
// Not enough data - read the full internal buffer to
// force the ReadStream to fill it again.
value = this.read(this.stream._readableState.length);
while (this.stream._readableState.length === 0) {
// Wait...?
}
// We should have some more data in the internal buffer
// here... Read the rest and add it to our `value` buffer
// ... something like this:
//
// value.push(this.stream.read(length - value.length))
// return value
} else {
value = this.stream.read(length);
this.stream.position += length;
return value;
}
};
问题是,缓冲区不再被填充 - 脚本将在while循环中空闲。
这样做的最佳方法是什么?
答案 0 :(得分:2)
这很简单。你不需要做任何缓冲:
var fs = require('fs'),
rs = fs.createReadStream('/path/to/file');
var CHUNK_SIZE = 8192;
rs.on('readable', function () {
var chunk;
while (null !== (chunk = rs.read(CHUNK_SIZE))) {
console.log('got %d bytes of data', chunk.length);
}
});
rs.on('end', function () {
console.log('end');
});
如果CHUNK_SIZE
大于内部缓冲区,则节点将返回null并缓冲一些,然后再次发出readable
。您甚至可以通过传递:
var rs = fs.createReadStream('/path/to/file', {highWatermark: CHUNK_SIZE});
答案 1 :(得分:0)
以下是在流中读取文件的示例。
var fs = require('fs'),
readStream = fs.createReadStream(srcPath);
readStream.on('data', function (chunk) {
console.log('got %d bytes of data', chunk.length);
});
readStream.on('readable', function () {
var chunk;
while (null !== (chunk = readStream.read())) {
console.log('got %d bytes of data', chunk.length);
}
});
readStream.on('end', function () {
console.log('got all bytes of data');
});