Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js/ read 100 first bytes of a file

I'm trying to read a file in parts: the first 100 bytes and then on.. I'm trying to read the first 100 bytes of the /npm file:

app.post('/random', function(req, res) {
    var start = req.body.start;
    var fileName = './npm';
    var contentLength = req.body.contentlength;
    var file = randomAccessFile(fileName + 'read');
    console.log("Start is: " + start);
    console.log("ContentLength is: " + contentLength);
    fs.open(fileName, 'r', function(status, fd) {
        if (status) {
            console.log(status.message);
            return;
        }
        var buffer = new Buffer(contentLength);
        fs.read(fd, buffer, start, contentLength, 0, function(err, num) {
            console.log(buffer.toString('utf-8', 0, num));
        });
    });

the output is:

Start is: 0
ContentLength is: 100

and the next error:

fs.js:457
  binding.read(fd, buffer, offset, length, position, wrapper);
          ^
Error: Length extends beyond buffer
    at Object.fs.read (fs.js:457:11)
    at C:\NodeInst\node\FileSys.js:132:12
    at Object.oncomplete (fs.js:107:15)

What can be the reason?

like image 672
Or Smith Avatar asked May 18 '14 08:05

Or Smith


1 Answers

You're confusing the offset and position argument. From the docs:

offset is the offset in the buffer to start writing at.

position 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.

You should change your code to this:

    fs.read(fd, buffer, 0, contentLength, start, function(err, num) {
        console.log(buffer.toString('utf-8', 0, num));
    });

Basically the offset is will be index that fs.read will write to the buffer. Let's say you have a buffer with length of 10 like this: <Buffer 01 02 03 04 05 06 07 08 09 0a> and you will read from /dev/zero which is basically only zeros, and set the offset to 3 and set the length to 4 then you will get this: <Buffer 01 02 03 00 00 00 00 08 09 0a>.

fs.open('/dev/zero', 'r', function(status, fd) {
    if (status) {
        console.log(status.message);
        return;
    }
    var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
    fs.read(fd, buffer, 3, 4, 0, function(err, num) {
        console.log(buffer);
    });
});

Also to make things you might wanna try using fs.createStream:

app.post('/random', function(req, res) {
    var start = req.body.start;
    var fileName = './npm';
    var contentLength = req.body.contentlength;
    fs.createReadStream(fileName, { start : start, end: contentLength - 1 })
        .pipe(res);
});
like image 99
Farid Nouri Neshat Avatar answered Oct 13 '22 00:10

Farid Nouri Neshat