Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read large binary files in node js without a blocking loop?

I am trying to learn some basics of event driven programming. So for an exercise I am trying to write a program that reads a large binary file and does something with it but without ever making a blocking call. I have come up with the following:

var fs = require('fs');
var BUFFER_SIZE = 1024;
var path_of_file = "somefile"

fs.open(path_of_file, 'r', (error_opening_file, fd) =>
{
    if (error_opening_file)
    {
        console.log(error_opening_file.message);
        return;
    }

    var buffer = new Buffer(BUFFER_SIZE);
    fs.read(fd, buffer, 0, BUFFER_SIZE, 0, (error_reading_file, bytesRead, buffer) =>
    {
        if (error_reading_file)
        {
            console.log(error_reading_file.message);
            return;
        }

        // do something e.g. print or write to another file
    })
})

I know I need to put a while loop in order to read complete file but in the above code I am reading just the first 1024 bytes of the file and cannot formulate how to continue reading the file without using a blocking loop. How could we do it?

like image 657
Muhammad Ali Avatar asked Mar 21 '16 20:03

Muhammad Ali


People also ask

How would you handle doing large processing in node JS?

To do that we create a request of type stream using axios. In case the requested file has the . zip extension, we are going to pipe the response through unzip, otherwise we write the stream to the disk using the node. js native fs.

Is heavily used in Node JS to deal with streams of binary data?

The Buffer class in Node. js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data.

How do I fix a process out of memory exception in node JS?

This exception can be solved by increasing the default memory allocated to our program to the required memory by using the following command. Parameters: SPACE_REQD: Pass the increased memory space (in Megabytes).


1 Answers

Use fs.createReadStream instead. This will call your callback over and over again until it has finished reading the file, so you don't have to block.

var fs = require('fs');

var readStream = fs.createReadStream('./test.exe');
readStream.on('data', function (chunk) {
  console.log(chunk.length);
})
like image 149
rgvassar Avatar answered Oct 16 '22 15:10

rgvassar