Wednesday, April 15, 2020

Node.js buffer module

Buffer


A Buffer can be understood as a storage unit for binary code. The buffers are used to temporarily store the binary data before or after its transmission. Buffers are used to send and receive data smoothly between the sender and receiver.



Node.js buffer module


To handle the various binary data streams and file system operations, we can use the Buffer class from the buffer module in Node. The Buffer octets are used in Node to deal with TCP streams, to support buffer operations, to perform several file IO operations and to interact with the operating system itself because all these operations are performed upon binary data.

The Buffer class, which is a subclass of the Uint8Array class from JavaScript is a global class and contains several other important methods to support additional functionalities. It means Node.js APIs can accept plain Uint8Arrays wherever Buffers are supported as well. Buffer class can be accessed in any application without importing the buffer module as it is a global class.

An instance of the Buffer class can be used to store the raw data similar to an array of integers (from 0 to 255) and corresponds to a raw memory allocation outside the V8-heap. The size of the buffer is fixed and it cannot be changed once it is created.

Creating the buffers


var buf1 = Buffer.alloc(10);
//an uninitialized Buffer of 10 octets filled with 0s
var buf2 = Buffer.alloc(10,1);
//an uninitialized Buffer of 10 octets filled with 1s
var buf3 = Buffer.from([48, 49, 50, 51, 52]);
//creating a Buffer from a given array
var buf4 = Buffer.from("Welcome to Node.js", "utf-8");
//creating a Buffer from a given string and optionally encoding type
//encoding types can be  "ascii", "utf8", "utf16le", "ucs2", "base64" or "hex"
console.log(buf1.toString());
console.log(buf2.toString());
console.log(buf3.toString());
console.log(buf4.toString());
/*
Output:


01234

Welcome to Node.js
*/

Important methods 

alloc() 

This method produces a Buffer instance of the provided length


allocUnsafe()

This method produces a non-zero-filled Buffer of the provided length


allocUnsafeSlow()

This method produces a non-zero-filled and non-pooled Buffer of the provided length.


byteLength()

This method returns the numbers of bytes in a provided object


compare()

This method is used to compare two Buffer instances

equals()

This method is used to compare two Buffer instances and this method will return true if a match is found, otherwise, false is returned

fill()

This method fills a Buffer instance with the provided values

concat()

This method is used to concatenate an array of Buffer instances into one Buffer instance


copy()

This method is used to copy the provided number of bytes of a Buffer instance


entries()

This method returns an iterator of "index" "byte" pairs of a Buffer instance


from()

This method produces a Buffer instance from an object (string/array/buffer)


includes()

This method tests if the Buffer instance contains the provided value. The method returns true if there is a match, otherwise false


indexOf()

This method tests if the Buffer instance contains the provided value. The method returns the first appearance, otherwise, it will return -1


isBuffer()

This method tests if an object is a Buffer instance or not


isEncoding()

This method tests if the Buffer instance supports the provided encoding or not


keys()

This method returns an array of keys in a Buffer instance


lastIndexOf()

This method tests if the Buffer instance contains the provided value. This method returns the first occurrence, starting from the end, otherwise, it will return -1


length

This method returns the length of a Buffer instance, in bytes


poolSize

This method sets or returns the number of bytes used for pooling


readDoubleBE()

This method reads a 64-bit double from a Buffer instance, returns the result in big-endian


readDoubleLE()

This method reads a 64 bit double from a Buffer instance, returns the result in little-endian


readFloatBE()

This method reads a 32-bit float from a Buffer instance, returns the result in big-endian


readFloatLE()

This method reads a 32-bit float from a Buffer instance, returns the result in little-endian


readInt8()

This method reads an 8-bit integer from a Buffer instance


readInt16BE()

This method reads a 16-bit integer from a Buffer instance, returns the result in big-endian


slice()

This method slices a Buffer instance into a new Buffer instance starting and ending at the provided positions


swap16()

This method swaps the byte-order of a 16-bit Buffer instance


swap32()

This method swaps the byte-order of a 32-bit Buffer instance


swap64()

This method swaps the byte-order of a 64-bit Buffer instance


toString()

This method returns a string version of a Buffer instance


toJSON()

This method returns a JSON version of a Buffer instance


values()

This method returns an array of values in a Buffer instance


write()

This method writes a provided string to a Buffer instance


writeDoubleBE()

This method writes the provided bytes, using big-endian, to a Buffer instance. The bytes must be 64 -bit double.

Simple operations on buffers


Reading a buffer

We can use to string method to read the buffer data. The syntax for the toString() method is,

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


 //iterating buffer
buffer = new Buffer.alloc(26);
for (var i = 0 ; i < 26 ; i++) {
  buffer[i] = i + 97;
}
//toString method
console.log( buffer.toString('ascii'));       
console.log( buffer.toString('ascii',0,5));  
console.log( buffer.toString('utf8',0,5));   
console.log( buffer.toString(undefined,0,5));



/*
Output:
abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde
*/


the default encoding is UTF-8.

Converting to JSON

buffer.toJSON() method is used to convert buffer into a JSON object.

 var buffer = Buffer.from('Welcome to Node.js');
var json = buffer.toJSON(buffer);

console.log(json);
/*
Output:
{
       type: 'Buffer',
       data: [
          87, 101, 108,  99, 111, 109,
         101,  32, 116, 111,  32,  78,
         111, 100, 101,  46, 106, 115
       ]

     }
*/


Concatenate buffers


buffer.concat(list[, totalLength]) method is used to apply concatenation.

 var buf1 = Buffer.from('Welcome to Node.js');
var buf2 = Buffer.from('A JavaScript framework');
var buf3 = Buffer.concat([buf1,buf2]);

console.log("Resultant buffer: " + buf3.toString());
/*
Output:
Resultant buffer: Welcome to Node.jsA JavaScript framework
*/


Compare buffers


We can use bufA.compare(bufB) method to compare two buffers. This method returns a positive or negative value based upon which comes before or after.

var buf1 = Buffer.from('abc');
var buf2 = Buffer.from('abcd');
var res = buf1.compare(buf2);

if(res < 0) {
   console.log(buf1 +" comes before " + buf2);
} else if(res === 0) {
   console.log(buf1 +" is same as " + buf2);
} else {
   console.log(buf1 +" comes after " + buf2);
} 
console.log("Resultant buffer: " + buf3.toString());
/*
Output:
abc comes before abcd
*/

Copy Buffer

We can use copy method content of one buffer into other buffer.The syntax for this method is buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])


var buf1 = Buffer.from('Node.js');

//copy the buffer
var buf2 = Buffer.alloc(7);
buf1.copy(buf2);
console.log("Copied content: " + buf2.toString());
/*
Output:
Copied content: Node.js
*/