30 Days of node
Day 21 : Node.js Buffers






30 days of node - Nodejs tutorial series

Introduction to Buffers

Buffer refers to space in memory which is used to store data temporarily. A buffer has traditionally been used between devices with speed mis-match so that they can keep on operating at their respective speeds without loss of data. In Node.js Buffers are used when dealing with file streams or tcp streams which are mainly octets of binary data.

Buffer methods

  • Buffer.alloc() : This method is used to create a Buffer object of given length with initializing all the value to fill or 0 .
    1. Syntax : The syntax of Buffer.alloc() method is given below :
      													
      Buffer.alloc(size[, fill[, encoding]])											
      													
      												

      Where ,
      • size : Desired length of new Buffer. It accepts integer type of data.
      • fill : The value to prefill the buffer. Default value is 0 .It accepts any of the follwing : integer , string , buffer type of data.
      • encoding
    2. Example : Coding example of Buffer.alloc() method is given below :
      													
      //Name of the file : buffer.alloc.js
      var buff = Buffer.alloc(20);
      console.log(buff);
      													
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.alloc.js
      <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>											
      													
      												

  • Buffer.allocUnsafe() : This method is used to create a Buffer object of given length but it will not initialize the values.Due to which contents of the newly created buffer are not known which causes a security threat because it might contain some sensitive or confidential data.
    1. Syntax : The syntax of Buffer.allocUnsafe() method is given below :
      													
      Buffer.allocUnsafe(size)										
      													
      												

      Where ,
      • size : Desired length of new Buffer. It accepts integer type of data.
    2. Example : Coding example of Buffer.allocUnsafe() method is given below :
      													
      //Name of the file : buffer.allocUnsafe.js
      var buff = Buffer.allocUnsafe(10);
      console.log(buff);
      													
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.allocUnsafe.js
      <Buffer 00 00 00 00 08 00 00 00 07 00>										
      													
      												

  • Buffer.from() : This method is used to create a Buffer from an string, object , array or buffer.
    1. Syntax : The syntax of Buffer.from() method is given below :
      													
      Buffer.from(string[, encoding])
      													
      												

      Where ,
      • string : data is passed here.
      • encoding : The encoding of string. Default value is utf8. This is an optional parameter.
    2. Example : Coding example of Buffer.from() method is given below :
      													
      //Name of the file : buffer.from.js
      var buff1 = Buffer.from('Nodejsera');
      console.log("buff1 : " + buff1);						
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.from.js
      buff1 : Nodejsera
      													
      												

  • buf.compare() : This method is used to compare buffers. It returns
    • 0 : If both buffers are same
    • 1 : If target buffer comes before the source buffer.
    • -1 : If source buffer comes before the target buffer.

    1. Syntax : The syntax of buf.from() method is given below :
      													
      buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])
      													
      												

      Where ,
      • target : target buffer.
      • targetStart : Position from where comparion begins on target buffer.
      • targetEnd : Position on which comparison ends on target buffer.
      • sourceStart : Position from where comparion begins on source buffer.
      • sourceEnd : Position on which comparison ends on source buffer.
    2. Example : Coding example of buf.compare() method is given below :
      													
      //Name of the file : buffer.compare.js
      var buffer1 = Buffer.from('Nodejsera');
      var buffer2 = Buffer.from('Nodejsera');
      var output = buffer1.compare(buffer2);
      console.log(output)
      if(output < 0) {
         console.log(buffer1 +" comes before " + buffer2);
      }else if(output == 0){
         console.log(buffer1 +" is same as " + buffer2);
      }else {
         console.log(output +" comes after " + buffer2);
      }						
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.compare.js
      0
      Nodejsera is same as Nodejsera
      													
      												

  • Buffer.concat() : This method is used to concatenate two or more buffers together.
    1. Syntax : The syntax of Buffer.concat() method is given below :
      													
      Buffer.concat(list)
      													
      												

      Where ,
      • list : List of buffers to be concatenated.
    2. Example : Coding example of Buffer.concat() method is given below :
      													
      //Name of the file : buffer.concat.js
      var buff1 = Buffer.from('Nodejsera for nodejs');
      var buff2 = Buffer.from('- 30 days of node');
      var buff3 = Buffer.concat([buff1,buff2]);
      console.log("buff3 content: " + buff3.toString());						
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.concat.js
      buff3 content: Nodejsera for nodejs- 30 days of node
      													
      												

  • buf.copy() : This method is used to copy specified amount of bytes from source buffer to target buffer.
    1. Syntax : The syntax of buf.copy() method is given below :
      													
      buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
      													
      												

      Where ,
      • target : buffer to which we need to copy.
      • targetStart : Position from where copy starts from source.
      • sourceStart : position from which copy starts.
      • sourceEnd : Position till which copy is done.
    2. Example : Coding example of buf.copy() method is given below :
      													
      //Name of the file : buffer.copy.js
      var buff = Buffer.from('Nodejsera');
      var newbuff = Buffer.alloc(20);
      buff.copy(newbuff);
      console.log("Content of newbuff :  " + newbuff.toString());					
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.copy.js
      Content of newbuff :  Nodejsera
      													
      												

  • buf.equals() : This method is used to compare 2 buffers. It returns true if buffers match, otherwise it will return false .
    1. Syntax : The syntax of buf.equals() method is given below :
      													
      buf.equals(otherBuffer)
      													
      												

      Where ,
      • otherBuffer : Buffer to compare with.
    2. Example : Coding example of buf.equals() method is given below :
      													
      //Name of the file : buffer.equals.js
      var buff1 = Buffer.from('nodejsera');
      var buff2 = Buffer.from('nodejsera');
      
      console.log(buff1.equals(buff2));					
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.equals.js
      true
      													
      												

  • buf.fill() : This method is used to fill the buffer with a specified value.
    1. Syntax : The syntax of buf.fill() method is given below :
      													
      buf.fill(value)
      													
      												

      Where ,
      • value : The value with which we will fill the buffer.
    2. Example : Coding example of buf.fill() method is given below :
      													
      //Name of the file : buffer.fill.js
      var buff = Buffer.allocUnsafe(10).fill('nj');
      console.log(buff.toString());					
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.fill.js
      njnjnjnjnj
      													
      												

  • buf.indexOf() : This method is used to check whether the buffer contains a specified value. If the value is present it will return the index of first occurrence of the value, otherwise it will return -1 .
    1. Syntax : The syntax of buf.indexOf() method is given below :
      													
      buf.indexOf(value)
      													
      												

      Where ,
      • value : the value we are looking for.
    2. Example : Coding example of buf.indexOf() method is given below :
      													
      //Name of the file : buffer.indexOf.js
      var buff1 = Buffer.from('Nodejsera');
      console.log(buff1.indexOf('j'));					
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.indexOf.js
      4
      													
      												

  • buf.length : This method is used to return the length of buffer object.
    1. Syntax : The syntax of buf.length method is given below :
      													
      buf.length
      													
      												

    2. Example : Coding example of buf.length method is given below :
      													
      //Name of the file : buffer.length.js
      var buf = Buffer.from('Nodejsera for nodejs');
      console.log( buf.length); 					
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.length.js
      20
      													
      												

  • Buffer.slice() : This method is used to slice the buffer into a new buffer with specified start and end point.
    1. Syntax : The syntax of buf.slice() method is given below :
      													
      buf.slice([start[, end]])
      													
      												

      Where ,
      • start : start offset of new buffer.
      • end : end offset of new buffer.
    2. Example : Coding example of buf.slice() method is given below :
      													
      //Name of the file : buffer.slice.js
      var buff1 = Buffer.from('Nodejsera');
      var buff2 = buff1.slice(0,5);
      console.log("content of buff2 : " + buff2.toString());					
      													
      												

    3. Run : We can run it in the following way :
      													
      >node buffer.slice.js
      content of buff2 : Nodej
      													
      												

  • buf.toJSON() : This method is used to convert the buffer into JSON.
    1. Syntax : The syntax of buf.toJSON() method is given below :
      													
      buf.toJSON()
      													
      												

    2. Example : Coding example of buf.toJSON() method is given below :
      													
      //Name of the file : bufferToJSON.js
      var buf = Buffer.from('Nodejsera');
      var json = buf.toJSON(buf);
      console.log(json);					
      													
      												

    3. Run : We can run it in the following way :
      													
      >node bufferToJSON.js
      { type: 'Buffer',
        data: [ 78, 111, 100, 101, 106, 115, 101, 114, 97 ] }
      													
      												

  • buf.toString() : This method is used to convert the buffer into string.
    1. Syntax : The syntax of buf.toString() method is given below :
      													
      buf.toString([encoding[, start[, end]]])
      													
      												

      Where ,
      • encoding : Character encoding to decode to.
      • start : starting offset.
      • end : ending offset.
    2. Example : Coding example of buf.toString() method is given below :
      													
      //Name of the file : bufferToString.js
      var buf = Buffer.from('Nodejsera for nodejs');
      console.log( buf.toString('ascii')); 						
      													
      												

    3. Run : We can run it in the following way :
      													
      >node bufferToString.js
      Nodejsera for nodejs
      													
      												

Summary

In this chapter of 30 days of node tutorial series, we learned about what are buffers and some basic methods of buffer module of node.js .



Get the code on