Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know actual size of byte buffer`s content in nodejs?

Tags:

node.js

buffer

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

like image 972
lor1an Avatar asked Aug 18 '16 09:08

lor1an


People also ask

How do you find the buffer size?

To check the buffer window, multiply the bit rate (bits per second) by the buffer window (in seconds) and divide by 1000 to get the size, in bits, of the buffer for the stream.

How do I decode a buffer in Node JS?

In Node. js, the Buffer. toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type. Converting a buffer to a string is known as encoding, and converting a string to a buffer is known as decoding.


1 Answers

You need byteLength:

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

For node 0.10.21 you can try this:

console.log(buff.toString().length); 
like image 189
stdob-- Avatar answered Sep 30 '22 19:09

stdob--