Node.js: Buffer - Day Six

Introduction to Buffer

Earlier, the ECMA Script 2015, also known as ES6 JavaScript, had no mechanism to read and manipulate the stream of binary data. The Buffer class was introduced as a part of the Node.js API to make it possible to deal with the stream of data in the context of things, like TCP streams and file system operations. Instances of the Buffer class are similar to arrays of integers but correspond to fixed-sized, raw memory allocations outside the V8 heap. The size of the Buffer is established when it is created and cannot be resized.

In simple words, we can define buffer as, “Buffer is just like a temporary holding spot used for moving the data from one place to another”.

Before the buffer in JavaScript,  we read stream data in sequence.
data
data
data
data
data
data
data
Before the concept of buffer, JavaScript read the stream data in sequence manner - once at a time. That makes it slow. But, in Node.js, comes with concept of buffer that has made the process faster. Let’s see how buffer increases the process speed of Node.js.

data
data
data
data
data
data
data

In buffer, the data comes down in stream and is hold for a temporary time. When the buffer becomes full, then it sends the data to the process so that it reduces the process time.

Now, we take some example to learn more about the buffer in Node.js.

Create and read Buffer Data.  
  1. var Buf = new Buffer('Pankaj''utf8');  
  2. console.log(Buf);  
Output

Output

We know that Buffer is an in-built class (module) of Node.js but we don’t need to use the require method because buffer is fundamental concept of Node.js. So, in Node.js, the Buffer class is made as global. In the above example, we create a buffer that's size is equal to the size of “Pankaj” string and we define the “utf8” encoding that means Node.js converts the string data “Pankaj” into binary data using the “utf8” encoding. If we try to read the data of buffer, then it will return the binary data but in hexadecimal representation for easy to read format.

Output

In this example, we convert the binary data into string.

Output

Now, we convert the binary data into JSON format that holds the ASCII value of each character.

Example
  1. var Buf1 = new Buffer('Pankaj''utf8');  
  2. var Buf2 = new Buffer(12);  
  3. var Buf3 = new Buffer([1, 2, 3, 5, 6]);  
  4. var Buf4 = new Buffer(Buf1);  
  5.   
  6. console.log(Buf1);  
  7. console.log(Buf2);  
  8. console.log(Buf3);  
  9. console.log(Buf4)  
Output

Output

In the above code snippet, we create 4 buffers. Buf1 occupies the 6 octants (size of string), Buf2 occupies 12 octants, Buf3 occupies 5 octants (size of array) and size of Buf4 is equal to the size of Buf1.

Write Data to Buffer

Using “Write” method of buffer class, we can write the data into a fixed size buffer.

Output
Syntax

Buf.Write(string[,offset][,length][,encoding])

Parameters
 
Parameter Description
String Data to write in buffer
Offset Starting index of buffer to write
Length Numbers of bytes to write
Encoding Encoding to use, default is ‘utf8’

Example:

  1. var Buf = new Buffer(6);  
  2. var len = Buf.write("pankaj");  
  3. console.log(len);  
  4. console.log(Buf.toString())  
Output:

Output

Write method of buffer class returns the numbers of bytes written in buffer.

Example
  1. var Buf = new Buffer(6);  
  2. var len = Buf.write("pankaj");  
  3. console.log(Buf.toString());  
  4. Buf.write("10");  
  5. console.log("Data Over Write");  
  6. console.log(Buf.toString());  
Output

Output

We create a buffer of size 6. If the buffer is full and we write more data, then it will over write the old data.

Example
  1. var Buf = new Buffer(6);  
  2. var len = Buf.write("pankaj");  
  3. console.log(Buf.toString());  
  4. Buf.write("10",3,2);  
  5. console.log("Data Over Write");  
  6. console.log(Buf.toString());  
Output

Output

In this example, we write the data string into buffer at index 3 and length of 2.

Read Data from Buffer

Using “toString” method of buffer class, we can read the data from a buffer.

Output
Syntax:

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

Parameter
 
Parameter Description
Encoding Define mode of encoding, default is “utf8”
Start Starting index of buffer to read
End End index of buffer to read

Example

  1. var Buf = new Buffer(6);  
  2. var len = Buf.write("pankaj");  
  3. console.log(Buf.toString('utf8'));  
  4. console.log(Buf.toString('ascii'));  
  5. console.log(Buf.toString('utf16le'));  
  6. console.log(Buf.toString('ucs2'));  
  7. console.log(Buf.toString('base64'));  
  8. console.log(Buf.toString('binary'));  
  9. console.log(Buf.toString('hex'));  
  10.   
  11. console.log(Buf.toString('utf8',2));  
  12. console.log(Buf.toString('utf8', 2, 3));  
Output

Output

Concatenate Buffers

Using “concat” method of buffer class, we can concatenate the list of buffers to a single buffer.

Output

Syntax

Buffer.concat(list[,totalLength])


Parameters
 
Parameter Description
List List of all buffers to concatenate
totalLength Total length of buffers when concatenate

Example

  1. var Buf1 = new Buffer("My Name is Pankaj");  
  2. var Buf2 = new Buffer("I live in Alwar");  
  3. var Buf3 = new Buffer("Working as a software developer");  
  4.   
  5. var newBuf = Buffer.concat([Buf1, Buf2, Buf3]);  
  6. console.log(newBuf.toString());  
Output

Output

Example
  1. var Buf = new Buffer("My Name is Pankaj");  
  2. console.log("Buffer Slice from 1 to 4 ="+Buf.slice(1, 4).toString());  
  3. console.log("Buffer Slice from 3 to 6 =" +Buf.slice(3, 6).toString());  
  4. console.log("Length of Buffer Slice ="+Buf.slice(1, 4).length);  
Output

Output

Using this example, I tried to explain the slice and length method. Slice method is used to slice or get a portion of buffer and length, I used to get the size of a buffer.

Copy Buffer: Using Copy method of buffer class , data can be copy from one buffer to another buffer.

Copy Buffer

Syntax: Buf.copy(target_Buffer[,targetStart][,sourceStart][,sourceEnd])

Parameters
 
Parameter Description
Target_Buffer Buffer where data will copy
targetStart Starting index of target buffer, default value is 0
sourceStart Starting index of source buffer, default value is 0
sourceEnd Ending index of source buffer, default value is length of buffer

Example

  1. var buf1 = new Buffer("I like Node.js");  
  2. var buf2 = new Buffer(11);  
  3. buf1.copy(buf2, 0, 0, 11);  
  4. console.log(buf2.toString()); 
Output

Output

ArrayBuffer in JavaScript

In the beginning of this article, I said that JavaScript doesn’t contain any buffer related concept but a new version of V8 engine removes this problem and now, JavaScript contains ArrayBuffer using which we can deal with the binary data. It represents a raw buffer of binary data, which is used to store data for the different typed arrays. ArrayBuffers cannot be read from or written to directly but can be passed to a typed array or DataView Object to interpret the raw buffer as needed.

You can get more information about the ArrayBuffer in ES6 documentation.

code

Example
  1. var arrbuf = new ArrayBuffer(12);  
  2. var intarr = new Int32Array(arrbuf);  
  3. intarr[0] = 10;  
  4. intarr[1] = 23;  
  5. intarr[2] = 45;  
  6. console.log(intarr.toString());  
Output

Output

I hope you liked this article. If you have any doubts, then write in the comment section.