'How to know actual size of byte buffer`s content in nodejs?
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.
Solution 1:[1]
You need byteLength
:
var buff = fs.readFileSync(__dirname + '/test.txt');
console.log(Buffer.byteLength(buff));
For node 0.10.21 you can try this:
Update
buffer.toSTring()
is unsafe, since buffers are meant to store binary data andtoString()
will attempt to do character encoding translation which will corrupt the binary data.
console.log(buff.toString().length);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lissy93 |