There are a few basics to cover when it comes to putting data into a buffer such as encoding and buffer index values. So I thought I would write a quick post on the buffer write prototype method in nodejs. In addition to this I may branch off with some other related topics on buffers in general.
So first we need to create a new buffer, or come across a buffer instance by whatever other means such as inside the body of a handler for a data stream for example. In any case once there is a buffer instance to work with the write prototype method can be used to write some data to that buffer.
A string value can be given as the first argument, and then also an encoding as the second argument for starters at least when it comes to a basic example of the buffer write method when playing around with this method for the first time. More on encoding shortly, but for now lets look at a basic example of the buffer write method in action by passing just a string to it, and an additional string that sets the encoding to hex rather than the default utf8 value.
So as one would expect this writes the hex value ff as the first byte of the buffer. However what if I want the string to be treated as a different encoding, and what if I want to place it at a different byte index in the buffer. No problem there are additional arguments for the method that can be used to change the encoding an byte index values.
So a second, third, or final argument can be used to set the character encoding to be used when writing the given string value to a buffer. By default the encoding is utf8, but it makes sense to always set the encoding even if it is the default to help make things more clear and readable as to what it is going on in a project.
To help show why consider the following.
This is an might be unexpected result, but it is what will happen because of the default encoding of utf8. Even if I want the encoding to be utf8 I still generally choose to pass a string value of utf8 anyway to make it clear that is the encoding that I want to be used when writing data to a buffer.
So then there is the subject of having control over the index value in the buffer to start writing, and the number of bytes to write to that buffer index forward.
For this I can use three arguments, or four arguments. If three arguments the second argument is a starting index in the buffer to begin writing at, and then the encoding. If four argument again the second is a starting location, the third is the number of bytes to write, and then the encoding.