Node.js | Buffers

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 = new Buffer(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 biffer.js file containing the following codes.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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'));  

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));  

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.