Hi,

In a recent change[1], the Buffer._charsWritten expected from following 
code is changed from 6 to 4, this make me wonder what is the definition and 
proper value for Buffer._charsWritten.

buf = new Buffer(9);
buf.write('123456', 'base64');
assert.equal(Buffer._charsWritten, 4);
buf.write('00010203040506070809', 'hex');
assert.equal(Buffer._charsWritten, 18);


According to the doc, "The number of characters written (which may be 
different than the number of bytes written) is set in Buffer._charsWrittenand 
will be overwritten the next time 
buf.write() is called."
My understanding to "The number of characters written" is the number of 
characters used in the input stream to be decoded into the buffer, and in 
this case, 6 seems to be reasonable as used to decode 4 octets.
Theoretically, the proper base64 encoded input should have multiple of 4 
characters, in this case, 2 paddings is probably needed. If we consider 
multiple of 4 to be "non partial character", then the decoded octets would 
be 3 instead of 4. But in this case, Buffer.write do return 4 and write 4 
octets into the buffer.

So, is this a bug or there is a better explanation? Perhaps my 
understanding is just wrong. Would you please shed the light on what should 
be expected here? 

Cheers,
Henry

[1] 
https://github.com/joyent/node/commit/f101f7c9babb31f077c78b52de7cc45ad687f57e#test/simple/test-buffer.js

Reply via email to