周汉成
周汉成

Reputation: 1445

Is there a definition of nodejs buffer content size?

First I initialize a buffer object:

var buffer = new Buffer(10); // allocate it with 10 bytes
buffer.fill(0); // avoid sensitive data
buffer.write("abc")
console.log(buffer)

Maybe I can recognize its content length is 3, I conclude a buffer's content length equals to the index of a 00 byte after which every byte must be 00 :

<Buffer 61 62 63 00 00 00 00 00 00 00>

But if I do:

var buffer = new Buffer(10); // allocate it with 10 bytes
buffer.fill(0); // avoid sensitive data
for (var i=0;i<3;i++) {
  buffer[i] = 0
}
console.log(buffer);

In this case, the buffer content length is whether 3 or 0?

<Buffer 00 00 00 00 00 00 00 00 00 00>

Thx

Upvotes: 0

Views: 437

Answers (1)

Yuri Zarubin
Yuri Zarubin

Reputation: 11677

Not sure what you mean by "content length", but the buffer's length, in both cases, is 10. Check this by doing console.log(buffer.length)

Upvotes: 2

Related Questions