Open In App

Node.js Buffers

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Buffers are instances of the Buffer class in Node.js. Buffers are designed to handle binary raw data. Buffers allocate raw memory outside the V8 heap. Buffer class is a global class so it can be used without importing the Buffer module in an application.
Creating Buffers: Followings are the different ways to create buffers in Node.js:
 

  • Create an uninitiated buffer: It creates the uninitiated buffer of given size.
    Syntax: 
     
var ubuf = Buffer.alloc(5);
  • The above syntax is used to create an uninitiated buffer of 5 octets.
  • Create a buffer from array: It creates the buffer from given array.
    Syntax: 
     
var abuf = new Buffer([16, 32, 48, 64]);
  • The above syntax is used to create a buffer from given array.
  • Create a buffer from string: It creates buffer from given string with optional encoding.
    Syntax: 
     
var sbuf = new Buffer("GeeksforGeeks", "ascii");
  • The above syntax is used to create a Buffer from a string and encoding type can also be specified optionally.

Writing to Buffers in Node.js: The buf.write() method is used to write data into a node buffer.
Syntax: 
 

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

The buf.write() method is used to return the number of octets in which string is written. If buffer does not have enough space to fit the entire string, it will write a part of the string. 
The buf.write() method accepts the following parameters: 
 

  • string: It specifies the string data which is to be written into the buffer.
  • offset: It specifies the index at which buffer starts writing. Its default value is 0.
  • length: It specifies the number of bytes to write. Its default value is buffer.length.
  • encoding: It specifies the encoding mechanism. Its default value is ‘utf-8’.

Example: Create a buffer.js file containing the following codes.
 

javascript




// Write JavaScript code here
cbuf = new Buffer(256);
bufferlen = cbuf.write("Learn Programming with GeeksforGeeks!!!");
console.log("No. of Octets in which string is written : "+  bufferlen);


Output: 
 

Reading from Buffers: The buf.toString() method is used to read data from a node buffer. 
Syntax: 
 

buf.toString( encoding, start, end )

The buf.toString() method accepts the following parameters: 
 

  • encoding: It specifies the encoding mechanism. Its default value is ‘utf-8’.
  • start: It specifies the index to start reading. Its default value is 0.
  • end: It specifies the index to end reading. Its default value is complete buffer.

Example 1: Create a buffer.js file containing the following code.
 

javascript




// Write JavaScript code here
rbuf = new Buffer(26);
var j;
 
for (var i = 65, j = 0; i < 90, j < 26; i++, j++) { 
    rbuf[j] = i ; 
 
console.log( rbuf.toString('ascii')); 


Output: 
 

Example 2: Read the data from Node.js buffer specifying the start and end point of reading. Create a buffer.js file containing the following code.
 

javascript




// Write JavaScript code here
rbuf = new Buffer(26); 
var j;
 
for (var i = 65, j = 0; i < 90, j < 26; i++, j++) { 
    rbuf[j] = i ; 
}
 
console.log( rbuf.toString('utf-8', 3, 9)); 


Output: 
 

 



Last Updated : 21 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads