在同一个fd上多次创建createReadStream

时间:2021-04-09 13:29:11

In a previous question it seemed that the only way to do random-access reads from a file in node.js is to use fs.createReadStream() with its optional fd, start, and end fields.

在之前的一个问题中,似乎唯一的方法是在节点中进行随机访问。js将使用fs.createReadStream()及其可选的fd、start和end字段。

This worked fine in my simplest tests. But in my project I need to repeatedly read from different offsets of a binary file. This failed in a strange way so I came up with a minimal test case:

在我最简单的测试中,这个方法很有效。但是在我的项目中,我需要反复读取二进制文件的不同偏移量。这以一种奇怪的方式失败了,所以我想出了一个最小的测试用例:

var fs = require('fs');

fs.open('test.txt', 'r', function (err, fd) {
  if (err) {
    console.error('error opening file: ' + err);
  } else {
    fs.createReadStream(null, {fd: fd, start: 2, end: 5}).on('error', function (err) {
        throw e;
      }).on('close', function () {
        console.log('outer close');
      }).on('data', function (data) {
        console.log('outer data', data);
      }).on('end', function () {
        console.log('outer end');

        fs.createReadStream(null, {fd: fd, start: 0, end: 3}).on('error', function (err) {
            throw e;
          }).on('close', function () {
            console.log('inner close');
          }).on('data', function (data) {
            console.log('inner data', data);
          }).on('end', function () {
            console.log('inner end');

            // more code to execute after both reads
          });
      });
  }
});

The inner end event is never received. (The outer close is received inconsistently, but I don't need to attach code to it.)

内端事件永远不会被接收。(外部关闭接收不一致,但我不需要为它附加代码。)

I've implemented this project before in Perl and even in JavaScript as a Firefox extension, but it's proving difficult under node. This is also a test for whether I can start using node.js as a general purpose scripting language.

我以前在Perl中实现过这个项目,甚至在JavaScript中也实现了Firefox扩展,但是在node下面证明它很困难。这也是测试我是否可以开始使用node。js是一种通用的脚本语言。

1 个解决方案

#1


3  

The issue is that the outer ReadStream will close the fd after it is used, so reusing it on the second ReadStream will fail. The newest Node unstable actually has an autoClose options for ReadStreams but that is not part of stable yet.

问题是,外部ReadStream在使用后将关闭fd,所以在第二个ReadStream上重用它将会失败。最新的不稳定节点实际上有一个ReadStreams的自动关闭选项,但这还不是稳定的一部分。

The real answer that is that the information given to you in your previous question is incorrect. createReadStream is implemented using all public APIs, so there is nothing that it can do that you can't do too. In this case, you can just use fs.read with its position argument.

真正的答案是,你在上一个问题中得到的信息是不正确的。createReadStream是使用所有公共api实现的,所以它所能做的事情你也可以做。在这种情况下,您可以使用fs。用它的位置参数来阅读。

var fs = require('fs');                                                         

fs.open('test.txt', 'r', function (err, fd) {                                   
  if (err) {                                                                    
    console.error('error opening file: ' + err);                                
  } else {                                                                      
    fs.read(fd, new Buffer(4), 0, 4, 2, function(err, bytesRead, data){        
      if (err) throw err;                                                       
      console.log('outer data', data);                                          

      fs.read(fd, new Buffer(3), 0, 3, 0, function(err, bytesRead, data2){   
        if (err) throw err;                                                     
        console.log('inner data', data2);                                       
        fs.close(fd);                                                           

        // more code to execute after both reads                                
      });                                                                       
    });                                                                         
  }                                                                             
});   

#1


3  

The issue is that the outer ReadStream will close the fd after it is used, so reusing it on the second ReadStream will fail. The newest Node unstable actually has an autoClose options for ReadStreams but that is not part of stable yet.

问题是,外部ReadStream在使用后将关闭fd,所以在第二个ReadStream上重用它将会失败。最新的不稳定节点实际上有一个ReadStreams的自动关闭选项,但这还不是稳定的一部分。

The real answer that is that the information given to you in your previous question is incorrect. createReadStream is implemented using all public APIs, so there is nothing that it can do that you can't do too. In this case, you can just use fs.read with its position argument.

真正的答案是,你在上一个问题中得到的信息是不正确的。createReadStream是使用所有公共api实现的,所以它所能做的事情你也可以做。在这种情况下,您可以使用fs。用它的位置参数来阅读。

var fs = require('fs');                                                         

fs.open('test.txt', 'r', function (err, fd) {                                   
  if (err) {                                                                    
    console.error('error opening file: ' + err);                                
  } else {                                                                      
    fs.read(fd, new Buffer(4), 0, 4, 2, function(err, bytesRead, data){        
      if (err) throw err;                                                       
      console.log('outer data', data);                                          

      fs.read(fd, new Buffer(3), 0, 3, 0, function(err, bytesRead, data2){   
        if (err) throw err;                                                     
        console.log('inner data', data2);                                       
        fs.close(fd);                                                           

        // more code to execute after both reads                                
      });                                                                       
    });                                                                         
  }                                                                             
});