Buffers in Node JS

B

Before studying the buffers in Node JS, let’s learn some terms before.

What is unicode representation?

Unicode is a standard to encode the text and each character is assigned a unique numeric value in this standard. Then this standard value remains the same on every platform and can be used cross-platform.

Pure JavaScript supports Unicode well but in the case of the binary form of data, it does not provide great support. For instance, while working with File System and TCP streams where data is in binary format and flows in the form of streams of zeroes and ones, we need to handle the octet streams.

In this article, you will learn how to use Buffers in Node JS to handle the stream inputs, outputs and handle octet data.

Note: Octet data is the data that consists of eight bits.

Node JS provides a buffer class that provides methods to store the raw data similar to an array of integers. But it is like allocating a raw memory outside the v8 heap.

What is v8 Heap?

For your extra knowledge, v8 heap is a hard limit implemented on the heap memory. So, if the Java application reaches this maximum limit, it tries the garbage collection to free up some space, But, if the garbage collection does not work to free up space and keep it below the v8 limit, it stops working without of memory acknowledgment.

How to create buffers in Node JS?

There are multiple ways to create a buffer in Node JS. Let’s explore them one by one.

Method 1.

To create a buffer of 10 octets, use the following syntax.

var buf = new Buffer(10);

Method 2.

To create a buffer in Node JS using a given array of data, use the following code.

var buf = new Buffer([10, 20, 30, 40, 50]);

Method 3.

To create a buffer from string or any encoding type, use the buffer class in the following manner.

var buf = new Buffer("Simply Easy Learning", "utf-8");

Note: utf-8 encoding is the default encoding type. If you want to use another, you can choose from the following available encoding schemes.

  • ascii
  • utf8
  • utf16le
  • ucs2
  • base64
  • hex

How to write data into Buffers in Node JS?

In this section, we will learn the method of Buffer class that will allow us to write some data in Buffer.

Use the following code to write data in the buffer.

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

In the above code, the write method of buffer instance is receiving some parameters. Let’s go through the purpose of each one by one.

  • string – The data in string format to be written in buffer.
  • offset – The default value of offset starts from 0. It defines the index in buffer to start putting data at.
  • length – By default, its value is equal to the length of the buffer. It defines the length of the data to write in buffer.
  • encoding – As defined before, it defines the encoding scheme and by default it is set to utf-8.

After writing the data successfully in Buffer, the write method of Buffer class returns the number of octets that have been written inside the buffer.

Note: If the string to write in the buffer exceeds the total space available in the buffer, it will write the part of the string that can reside in the buffer.

Example

buf = new Buffer(256);
len = buf.write("Simply Easy Learning");

console.log("Octets written : "+  len);

The output of the above program

Octets written : 20

How to read data from Buffers in Node JS?

After we are done with writing the data in the buffer, the next major thing to learn about them is to read the saved data.

Use the following code to read data from buffers.

buf.toString([encoding][, start][, end])

Parameters in the toString method of the buffer class are explained below.

  • encoding – defines the encoding scheme. By default it is utf-8.
  • star – defines the starting index to read data from. By default is 0.
  • end – defines the ending index to stop reading data at. By default it is set to the total lenght of the buffer.

The toString method of the buffer class returns the string read from the buffer. And it uses the encoding scheme for the returned string.

Example

buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
  buf[i] = i + 97;
}

console.log( buf.toString('ascii'));       // outputs: abcdefghijklmnopqrstuvwxyz
console.log( buf.toString('ascii',0,5));   // outputs: abcde
console.log( buf.toString('utf8',0,5));    // outputs: abcde
console.log( buf.toString(undefined,0,5)); // encoding defaults to 'utf8', outputs abcde

The above example produces the following output.

abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde

In the next tutorial, we will see the buffers in more depth and will learn more methods in Buffer class. You must practice the concepts of this tutorial before moving next.

Reference to the official documentation of Buffers in Node

About the author

Tutor Network Team

Add comment

Tutor Network