读取流:内部缓冲区不再填满



我有一个fs。指向一个相当大的文件的 ReadStream 对象。现在我想从 ReadStream 读取 8000 字节,但内部缓冲区只有 6000 字节。所以我的方法是读取这 6000 个字节,然后通过使用 while-loop 来检查内部缓冲区长度是否不再为 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 循环中空闲。

最好的方法是什么?

这很简单。您无需在自己这边进行任何缓冲:

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});

下面是在流中读取文件的示例。

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');
});

相关内容

  • 没有找到相关文章

最新更新