如何知道Node.js中字节缓冲区内容的实际大小? [英] How to know actual size of byte buffer`s content in nodejs?

查看:147
本文介绍了如何知道Node.js中字节缓冲区内容的实际大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将文件作为字节缓冲区获取,不能使用fs.stat()方法。
因此,我尝试使用buf.length,但是此长度是指为缓冲区对象分配的内存量,而不是内容大小。
例如,我的文件大小为22,449字节。
buf.length返回39804。

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.

推荐答案

您需要 byteLength

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

对于节点0.10.21,您可以尝试以下操作:

For node 0.10.21 you can try this:

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

这篇关于如何知道Node.js中字节缓冲区内容的实际大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆