Node.js Buffers
Node.js Buffers
Well when we say about the Node.js Buffers, and this Buffer class is used to store raw data which is similar to an array of integers but this is corresponds to a raw memory allocation outside the V8 heap. Here we used Buffer class and the reason behind this is that as JavaScript is not nice to binary data. SO overcome this about the binary data we use Buffer class. So when dealing with TCP streams or file system, this is necessary to handle octet streams.
Buffer class is one of the global class. And this can be accessed in application without importing buffer module.
Node.js Creating Buffers
There are many ways to create a Node buffer. Below are the 3 mostly used methods:-
(1)Create an uninitiated buffer:-Well below is the syntax to create an uninitiated buffer of 10 octets:-
var buf = new Buffer(10);
(2)Create an buffer from array:-Well below is the syntax to create a Buffer from a given array:-
var buf = new Buffer([10, 20, 30, 40, 50]);
(3)Create a buffer from string:-Well below is the syntax to create a Buffer from agiven string and optionally encoding type:-
var buf = new Buffer("Simple and Easy Learning", "utf-8");
Node.js Writing to Buffers
To write a method into a Node buffer. And below is the syntax to write node.js buffers:-
buf.write(string[, offset][, length][, encoding])
Parameter explanation:-
- string:-Here we will specifies the string data to be written to buffer.
- offset:-Here we will specifies the index of the buffer to start writing at. Its default value is 0.
- length:-Here we will specifies the number of bytes to write.Defaults to buffer.length
- encoding:-Encoding to use. 'utf8' is the default encoding.
Return values from writing buffers:-
Well this method is mainly used to return number of octets written. And in the case of space shortage for buffer to fit the entire string, this will write a part of the string.
To understand take example:-
Now we will create a JavaScript file which named as "main.js" which is haveing the below code with file name:-
File: main.js
buf = new Buffer(256);len = buf.write("Simply Easy Learning");console.log("Octects written: " + len);
Now we open Node.js command prompt to execute the below code:-
node main.js
Now get the output of above file:-
Node.js Reading from buffers
Well below is the method to read data from a Node buffer. And below is the syntax:-
buf.toString([encoding][, start][, end])
Parameter explanation:-
- encoding:-Here we will specifies the encoding to use. 'utf8' is the default encoding.
- start:-Here we will specifies the beginning index to start reading, defaults to 0.
- end:-Here we will specifies end index to end reading, defaults is complete buffer.
Return values from writing buffers:-
Well this method decodes and returns a string from buffer data encoded using the specified character set encoding. And below is the example and file:-
File: main.js
Now open Node.js command prompt and then execute the below command:-
node main.js
And below is the output after execute the read buffer:-
| |