ReadStream:内部缓冲区不再占满 [英] ReadStream: Internal buffer does not fill up anymore

查看:62
本文介绍了ReadStream:内部缓冲区不再占满的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个fs.ReadStream对象,它指向一个很大的文件。现在,我想从ReadStream中读取8000个字节,但是内部缓冲区只有6000个字节。因此,我的方法是读取这6000个字节,然后使用while循环检查内部缓冲区的长度是否不再为0,以等待内部缓冲区再次填充。

I have a fs.ReadStream object that points to a pretty big file. Now I would like to read 8000 bytes from the ReadStream, but the internal buffer is only 6000 bytes. So my approach would be to read those 6000 bytes and wait for the internal buffer to fill up again by using a while-loop that checks whether the internal buffer length is not 0 anymore.

类似这样的东西:

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循环中空闲

The problem is, that the buffer is not filled anymore - the script will just idle in the while loop.

什么是最好的方法?

推荐答案

这很简单。您无需为自己做任何缓冲:

It's quite simple. You don't need to do any buffering on your side:

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并缓冲更多内容,然后再次发出可读。您甚至可以通过以下方式配置缓冲区的初始大小:

If CHUNK_SIZE is larger than the internal buffer, node will return null and buffer some more before emitting readable again. You can even configure the initial size of the buffer by passing:

var  rs = fs.createReadStream('/path/to/file', {highWatermark: CHUNK_SIZE});

这篇关于ReadStream:内部缓冲区不再占满的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆