We split the buffers topic into two tutorials for better understanding. In the previous article, we learned about the introduction to buffers, writing data in a buffer, and reading it using Buffer class methods in Node JS.
In this article, you will learn more methods provided by the Node JS buffer class. These methods are commonly used while working with buffers, so you must practice them on your own after going through this article.
How to convert Buffer to JSON?
By default, the buffer does not return the data in JSON format but most of the APIs responses required data in JSON form. You can use the toJSON method to simply convert the buffer data into JSON.
toJSON method returns a JSON representation of the Buffer instance.
Example
var buf = new Buffer('Simply Easy Learning'); var json = buf.toJSON(buf); console.log(json);
In the above example, we create a buffer using the Node JS buffer class with some string as its parameter. We convert the buffer into JSON using the toJSON method and log it in the console.
The above code snippet produces the following output in the console. Notice one thing that the string which we passed as the parameter is stored in the form of numeric values, that is due to the utf-8 encoding.
{ type: 'Buffer', data: [ 83, 105, 109, 112, 108, 121, 32, 69, 97, 115, 121, 32, 76, 101, 97, 114, 110, 105, 110, 103 ] }
How to join two or more buffers in Node JS?
It’s a really useful and interesting use case of buffers that we can combine two or more buffers using the Concat operator provided by the Buffer class. Look at the syntax of the method below.
Buffer.concat(list[, totalLength])
In the contact method, the parameters are defined below.
- list – Array containing instances of buffers to concat.
- totalLength – The total length of buffers after concatenation.
After concatenation of buffers, this method returns a buffer instance. This buffer instance now contains the combination of buffers provided in the first paramter of concat method.
Example
var buffer1 = new Buffer('TutorialsPoint '); var buffer2 = new Buffer('Simply Easy Learning'); var buffer3 = Buffer.concat([buffer1,buffer2]); console.log("buffer3 content: " + buffer3.toString());
The above program will produce the following output.
buffer3 content: TutorialsPoint Simply Easy Learning
How to compare two buffers in Node JS?
Buffer class in Node JS provides compare method to simply compare the two Node buffers. Look at the following syntax to do so.
buf.compare(otherBuffer);
It accepts one parameter, that is the instance of the buffer to compare the current buffer with. And, this function returns a number that represents the following meanings.
- return some number < 0 (negative) – It means buffer 1 content comes before the content of the buffer 2.
- returns 0 – Buffer 1 and buffer 2 are same.
- returns some positive number – Content of buffer 1 comes after the contents of buffer 2.
Example of buffer comparison in Node JS
var buffer1 = new Buffer('ABC'); var buffer2 = new Buffer('ABCD'); var result = buffer1.compare(buffer2); if(result < 0) { console.log(buffer1 +" comes before " + buffer2); } else if(result === 0) { console.log(buffer1 +" is same as " + buffer2); } else { console.log(buffer1 +" comes after " + buffer2); }
The above program produces the following results. However, you are recommended to practice at your end and verify the following output.
ABC comes before ABCD
How to copy buffer content into other buffer?
Node JS buffer class provided a built-in method to copy the contents of a buffer into another buffer. Look at the following syntax.
buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])
The copy method accepts the following parameters.
- targetBuffer – It defines the target instance of buffer which recieves the contents of other buffeer.
- targetStart – It defines the index number at which the target buffer will start receiving the copied data from the source buffer. By default , it is set to 0.
- sourceStart – It defines the index number from which the source buffer should be copied. By default it is set to 0.
- sourceEnd – It defines the ending index of the source buffer to stop copying data at. By default it is set to the length of the buffer.
The copy method does not return any value. It copies the source buffer data starting from the sourceStart and ending at the sourceEnd. On the other hand, the target buffer starts accepting data starting from targerStart.
Example
var buffer1 = new Buffer('ABC'); //copy a buffer var buffer2 = new Buffer(3); buffer1.copy(buffer2); console.log("buffer2 content: " + buffer2.toString());
The above example produces the following output in the console.
buffer2 content: ABC
How to slice/divide a buffer in Node JS?
The slice method of the buffer class allows getting the sub buffer. Look at the following syntax.
buf.slice([start][, end])
The slice method accepts two methods.
- start – It defines the starting index of the main buffer to start slicing it from. By default, it is set to 0.
- end – It defines the ending index of the main buffer to stop slicing it at, and take out the sub buffer. By default, it is set to the length of the buffer.
The slice method of the Buffer class returns a new buffer which interestingly points to the same memory as the old buffer but is offset and sliced by starting index and ending index.
Example
var buffer1 = new Buffer('TutorialsPoint'); //slicing a buffer var buffer2 = buffer1.slice(0,9); console.log("buffer2 content: " + buffer2.toString());
The above code produces the following output in the console.
buffer2 content: Tutorials
How to get the length of the buffer in Node JS?
length method simply returns the length of the buffer in Node JS. Go through the following example.
var buffer = new Buffer('TutorialsPoint'); //length of the buffer console.log("buffer length: " + buffer.length);
The above example will produce the following output in the console.
buffer length: 14