Is it possible to know the amount of data a chunk (buffer) carries in ReadStreaming from a file (a text file) in node js ???
Yes, in your example, chunk.length
will be the amount of data in the chunk.
You cannot control what the chunk length will be. You have to code to whatever it ends up being when the data
event occurs. This is how streams work. The size of a given chunk depends upon how much data was sent, depends upon the transport (slow transports are more likely to give you smaller chunks) and the max buffer size in the stream.
You can control the max buffer size in the stream at the time you create the stream with the highWaterMark
option as shown in the doc.
var myReadStream = fs.createReadStream( __dirname + '/readme.txt', 'utf8', { highWaterMark: 60000 });
But setting this to a larger value only makes it possible to get larger chunks. It does not guarantee it.
I created a ReadStream and displayed the data of a text file in chunks, but is it possible to find the amount of data a buffer could have ?
If you set the highWaterMark
when you create the stream, then that will be the max size chunk you can get. If you want to see what the current highWaterMark
is set to, you can look in the stream instance data at:
myReadStream._readableState.highWaterMark
That is not a documented API so it is subject to change. If you need to know, you should probably just set it to a desired value when you create the stream.