How to read a file by setting the correct offset and position and write back to Nodejs with manual buffering?

I want to read a file with an interval of 64 bytes. And I also do not want to use any functionality that interanlly implements buffering. I wanted to do the buffering manually. So I started using fs.read (). I tried, but I really don’t know how to set the position , which says where to read in the file and offset in the buffer to start writing.
So I found some resources and started to implement it myself. But what I did seems completely wrong. Please find my code below.

app.get('/manualBufferAnother', function (req, res, next) {
   var filePath = path.join(__dirname, 'Koala.jpg');
   console.log("FilePath is: "+filePath);
   var fileName = path.basename(filePath);
   var mimeType = mime.lookup(filePath);
   var stat = fs.statSync(filePath);

   res.writeHead(200, {
         "Content-Type": mimeType,
         "Content-Disposition" : "attachment; filename=" + fileName,
         'connection': 'keep-alive',
         "Content-Length": stat.size,
         "Transfer-Encoding": "chunked"
   });

   fs.open(filePath, 'r', function(err, fd) {
       var completeBufferSize = stat.size;
       var offset = 0;  //is the offset in the buffer to start writing at
       var length = 511; //is an integer specifying the number of bytes to read
       var position = 0;  //is an integer specifying where to begin reading 
       from in the file. If position is null, data will be read from the current file position
       var buffer = new Buffer(completeBufferSize);
       buf(res,fd,offset,position,length,buffer,stat);        
   });
 });

var buf = function(res,fd,offset,position,length,buffer,stat) {
if(position+buffer.length < length) {
    fs.read(fd,buffer,offset,length,position,function(error,bytesRead,bufferr {
        res.write(bufferr.slice(0,bytesRead));
        console.log("Bytes Read: "+bytesRead);
        position=position+bufferr.length;
        buf(res,fd,offset,position,length,bufferr,stat);
    })
} else {
    fs.read(fd,buffer,offset,length,position,function(error,bytesRead,bufferr) {
        console.log("Bytes Read in else: "+bytesRead);
        res.end(bufferr.slice(0,bytesRead));
        fs.close(fd)
    })
}
}

, . . - ? , ?

+4
1

:

res.writeHead(...);
var SIZE = 64; // 64 byte intervals
fs.open(filepath, 'r', function(err, fd) {
  fs.fstat(fd, function(err, stats) {
    var bufferSize = stats.size;
    var buffer = new Buffer(bufferSize),
    var bytesRead = 0;

    while (bytesRead < bufferSize) {
      var size = Math.min(SIZE, bufferSize - bytesRead);
      var read = fs.readSync(fd, buffer, bytesRead, size, bytesRead);
      bytesRead += read;
    }
    res.write(buffer);
  });
});

?

, . Node.js (). , , , . , Node.js , callback. read . readSync. () C read() ( ).

, , , , .

readSync , .

//                        /----- where to start writing at in `buffer`    
fs.readSync(fd, buffer, offset, length, position)
//                                           \------- where to read from in the 
//                                                    file given by `fd`

. C, Javascript - . - , , Javascript (aka ").
Express.js:

, . , . . Node , .

.pipe() Streams ( ), , , . , : / - C, Node.js.

+2

Source: https://habr.com/ru/post/1674366/


All Articles