How to find out the actual size of the contents of a byte buffer in nodejs?

I receive files as byte buffers and cannot use the fs.stat () method. Therefore, I am trying to use buf.length, but this length refers to the amount of memory allocated for the buffer object, and not the actual size of the content. For example, I have a file of size 22,449 bytes. buf.length returns 39804 for it.

+7
buffer
source share
1 answer

You need byteLength :

 var buff = fs.readFileSync(__dirname + '/test.txt'); console.log( buff.byteLength ); 

For node 0.10.21 you can try the following:

console.log (buff.toString (). length);

+16
source share

All Articles