Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Buffer.write() appending 0x0 #1361

Closed
sixtus opened this issue Jul 19, 2011 · 3 comments
Closed

Buffer.write() appending 0x0 #1361

sixtus opened this issue Jul 19, 2011 · 3 comments

Comments

@sixtus
Copy link

sixtus commented Jul 19, 2011

It's currently not possible to use Buffer.write() for strings that do not use the 0x0 termination pattern.

b
(Buffer aa aa aa aa)
b.write("A", 0, "utf8")
1
b
(Buffer 41 00 aa aa)

I would like a mechanism for the Buffer to result in (Buffer 41 aa aa aa)

Side note: Buffer._charsWritten seems to always be set to 2, regardless of the last write()

All output based on v0.5.1

@koichik
Copy link

koichik commented Jul 19, 2011

Yes, V8 adds null terminator if buffer has room enough.
We can avoid this behavior, but it is inefficient (~40%, see #394).

I think that #243 will solve your problem.
example:

> b = new Buffer(4); b.fill(0xAA); b;
<Buffer aa aa aa aa>
> b.write(s, 0, Buffer.byteLength(s), 'utf8');
<Buffer 41 aa aa aaa>

How about this?

Side note: Buffer._charsWritten seems to always be set to 2, regardless of the last write()

Hmm... you should confirm it as follows:

> b.write('a', 0, 'utf8'); Buffer._charsWritten
1
> b.write('aa', 0, 'utf8'); Buffer._charsWritten
2
> b.write('aaa', 0, 'utf8'); Buffer._charsWritten
3

@sixtus
Copy link
Author

sixtus commented Jul 19, 2011

Yes, this entry can be seen as the missing use case for #243

Regarding Buffer._charsWritten: it must be a repl bug, doing it in one go with ; works, doing it in two lines doesn't. I guess it's the ">\n" prompt?

@koichik
Copy link

koichik commented Jul 23, 2011

There is workaround for v0.4.

> b = new Buffer([0xAA, 0xAA, 0xAA, 0xAA])
<Buffer aa aa aa aa>
> s = 'a'
'a'
> b.slice(0, Buffer.byteLength(s)).write(s)
1
> b
<Buffer 61 aa aa aa>

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants