0

我有一个 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 循环中闲置。

做到这一点的最佳方法是什么?

4

2 回答 2

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});
于 2013-10-17T12:14:42.690 回答
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');
});
于 2013-10-17T12:53:57.010 回答