• Node.js Video Tutorials

Node.js - Buffer.write() Method



The NodeJS Buffer.write() method will write the given string value to the buffer from the position given by the offset value. If the buffer does not have enough space to accommodate the string value, part of the string will be written. Please note it will not take care of writing partially encoded string value.

Syntax

Following is the syntax of the Node.JS Buffer.write() Method

buf.write(string[, offset[, length]][, encoding])

Parameters

This method accepts four parameters. The same is explained below.

  • string − (required) The string value you want to write to the buffer.

  • offset − (optional)The offset that indicates the position to start writing. The default value is 0.

  • length − (optional) the number of bytes you want to write. The length cannot exceed buffer.length − offset. So the default value for length is buffer.length − offset.

  • encoding − (optional) The character encoding to be used for string value. The default is utf-8.

Return value

This method Buffer.write() returns the number of bytes written to the buffer.

Example

In this example will create a buffer using NodeJS Buffer.from() and later write a string value to the buffer created.

const buffer = Buffer.from('Hello');
buffer.write('World');
console.log("The final buffer is :"+buffer.toString());

Output

We have not used any offset value so by default it will be considered as 0. The length of the buffer created with the Hello string is 5. So when you write World to it which is of the same length as the buffer so a full string will be written.

The final buffer is :World

Example

In this example I will create a buffer using Buffer.alloc() and allocate 5 lengths to it. Now will use a bigger string to write to the buffer using Buffer.write() method.

const buffer = Buffer.alloc(5);
buffer.write('Hello World');
console.log("The final buffer is :"+buffer.toString());

Output

We have not used any offset value so by default it will be considered as 0. The length of the buffer created is 5. Since we are writing a string with length: 11, the buffer will accommodate for only length 5. So when you see the output the buffer will write only the string Hello, as it can take only length of 5.

The final buffer is :Hello

Example

In this example I will check with an offset value. Will specify the offset in the Buffer.write() and write the string value to the buffer created.

const buffer = Buffer.alloc(5);
buffer.write('Hello World', 3);
console.log("The final buffer is :"+buffer.toString());

Output

The offset used is 3, the length of the buffer is 5, the length of the string to be written is 11. So the buffer will fill up from 3 and the buffer length i. e 5. So only characters He will be written to the buffer.

The final buffer is :He

Example

In this example will make use of length and encoding to write to the Buffer using Buffer.write() method.

const buffer = Buffer.alloc(10);
buffer.write('Hello World', 3, 5, 'utf-8');
console.log("The final buffer is :"+buffer.toString());

Output

So the offset is from 3, the length of string we want to write is 5. The total length of the buffer is 10. Since the length to be written is 5, it will be possible to write since buffer.length − offset is 10 − 3 = 7. So it will easily accommodate the string value of length 5 from offset 3 in the buffer.

The final buffer is :Hello
nodejs_buffer_module.htm
Advertisements