Working with Buffers

Buffers in Node

In this tutorial, we would cover Buffers in Node.js under the following sub-topics:

  1. Introduction to Buffers
  2. How to Create Buffers
  3. Writing to Buffers
  4. Reading from Buffers
  5. Converting Buffers To JSON
  6. Concatenating Buffers
  7. Comparing Buffers
  8. Copying Buffers
  9. Slicing Buffers
  10. Other Buffer Class Methods

 

1. Introduction to Buffers

The buffer class is provided by Node.js to data from streams. For instance, streams of data from file system or from network.

This is raw data similar to an array of integers. However, they corresponds to a raw memory allocation that is outside the V8 memory heap. Learn about V8 here.

The Buffer class in Node.js is a global class. I can be accessed in your application without having to import the buffer module.

 

2. How to Create Buffers

Some of the methods of creating a buffer is given below

The code below creates a buffer of 10 octets that is uninitialized

var mybuffer = new Buffer(10);

 

Here, a buffer is created from an array

var mybuffer = new Buffer([20, 30, 40, 50, 60]);

 

Finally, in this code, a buffer is created from a string. You can pass the encoding as optional argument

var mybuffer = new Buffer("The Tech Pro", "utf-8");

Other encoding can be used as well. For example, “ascii”, “utf16e”, “hex” and “base64”.

 

3. Writing to Buffers

Once a buffer is created, then you can write to it. The syntax for writing to buffer is given below:

mybuffer.write(string[, offset][, length][, encoding]);

 

The parameters to the write() method are explained as follows:

  • string − The string data that is to be written to buffer.
  • offset − The index of the buffer from where to start writing at. The default value is 0.
  • length − The number of bytes to write into the buffer. Defaults to the length of the buffer, buffer.length.
  • encoding − The encoding to use. The default encoding is ‘utf8’ is.

The write method() returns the number of octets written. If there are no enough space in the buffer, then only a part of the string is written.

 

Example  of Writing to Buffer

The code below writes a string to a buffer and outputs number of octets written.

mybuffer = new Buffer(256);
written = mybuffer.write("The Tech Pro");

console.log("Number of Octets written : "+  written);

 

4. Reading from Buffers

You can also read from a buffer using the syntax below.

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

 

The parameters are described as follows:

  • encoding − The encoding to use. ‘utf8’ is the default.
  • start − The beginning index to start reading from, defaults is 0.
  • end − End index to end reading, defaults is the complete buffer.

The return value of the read method is a string form the buffer encoded with the given character encoding

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

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

I recommend you run the code to see the outputs.

 

5. Converting Buffer to JSON

To convert buffer to json simply use the toJSON() method of the buffer object. This is as given below

mybuffer.toJSON()

 

Buffer.concat(list[, totalLength])

The parameters are explained as follows:

  • list − An Array List of Buffer objects that is to be concatenated.
  • totalLength − The total length of the buffers when concatenated.

The return value is a buffer object made up of a combination of the buffers concatenated.

The code below shows a concatenation of two buffers, buffer1 and buffer2

var buffer1 = new Buffer('The Tech Pro');
var buffer2 = new Buffer('Learning made easy!');
var buffer3 = Buffer.concat([buffer1,buffer2]);

console.log("buffer3 content: " + buffer3.toString());

Try to run to code yourself to see the output.

 

 

7. Comparing two Buffers

You can compare two buffers using the compare method of the buffer object. For example:

buffer1.compare(buffer2)

The return value is either a positive number, negative number or zero.

Positive number indicates that buffer1 comes before buffer2

Negative number indicates buffer1 comes after buffer2

Zero means the two buffers are the same

The program below illustrates the result of comparing two buffers:

var buffer1 = new Buffer('AB');
var buffer2 = new Buffer('ABCDE');
var result = buffer1.compare(buffer2);

if(ret_val< 0) {
   console.log(buffer1 +" comes before " + buffer2);
} else if(ret_val === 0) {
   console.log(buffer1 +" is same as " + buffer2);
} else {
   console.log(buffer1 +" comes after " + buffer2);
}

 

8. Copying Buffers

You can copy buffer using the copy instance method of the buffer object. The syntax is given below:

buffer.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])

where:

  • targetBuffer − The buffer object where buffer will be copied to.
  • targetStart − Number, Optional, Default is 0
  • sourceStart − Number, Optional, Default is0
  • sourceEnd − Number, Optional, Default: buffer.length

There is no return value.

 

9. Slicing a Buffer

Slicing a buffer means extracting a part of the buffer. The syntax is:

buf.slice([start][, end])

It returns a new buffer which is part of the original buffer but references the same memory location as the original buffer.

 

10. Other Buffer Class Methods

There are yet other buffer methods I would like you to try out. Find them in the table below:

SN. Method and brief description
1 Buffer.isEncoding(encoding)

Returns true if the encoding is a valid encoding argument, otherwise it returns false.

2 Buffer.isBuffer(obj)

Checks if obj is a Buffer.

3 Buffer.byteLength(string[, encoding])

Gives the actual length of a string in byte. encoding default is ‘utf8’. This is not the same as String.prototype.length, becaise String.prototype.length returns the number of characters in a string.

4 Buffer.concat(list[, totalLength])

We already mentioned this. It returns a buffer which is the result of concatenating all the buffers in the list together.

5 Buffer.compare(buffer1, buffer2)

The same as buffer1.compare(buffer2). Used for sorting an array of buffers.