Definition of js Buffers
How Node.js Buffers Works?
Node.js buffers work with a binary stream of data. All types of array methods we can use in buffers as buffers are like integer arrays. First, we need to understand why we got this to work with a buffer in node.js. Node.js buffer class works exactly similarly like integer array. Node.js buffer object shows the sequence of bytes to work with. In node.js we are mostly working with API (Application Programming interface). When we are working with buffers in node.js, we are not able to adjust the size of it once given. As we have seen earlier Buffer is a subclass of unit8Array, the same way unit8Array is a subclass of TypeArray. So indirectly whatever methods available in TypeArray also exist in Buffers. At the same time, we can also find some difficulties between these two APIs.
How to Create Node.js Buffers?
Buffer class can be created in various ways. In lower node.js versions those are less than version 6 buffers were get created by using their constructor functions. Here when we were creating buffers it was get allocated differently, as per the arguments provided at the time of creating the buffer. There are many ways to create buffer in node.js. We will see one by one. The first way to declare a variable and allocate a specified amount of memory to it.
var bufferMemory = Buffer.alloc(4);
This will give us 4 places.
The next way to write buffer is while writing we can initialize it. Now we will add values to the given buffer.
var bufferMemory = Buffer.from([5,6,4,1]);
For initializing buffer we used array. And as we know the contents of an array are integers because it works as an integer array. Generally when we work with buffer character encoding id used as utf-8 by default.
Suppose, we passed string in the above buffer, then it will give a corresponding value of it.
var bufferMemory = Buffer.from("Hello","utf-8");
The above code initializes string to the binary format. Utf-8 is by default, node.js buffer supports many more types of encoding but this one is mostly used. There other also like utf16le, latin1, ASCII, binary, hex, base64, etc.
How to Write Node.js Buffers?
In any programming language when we writing program we write step by step. If you are following this article till now then you must know till now we have created buffer and initialized it also. Now let’s see how can we write to buffers. But if you remember first of all we have to allocate some memory to it.
var bufferData = Buffer.alloc(32); // here we are allocating memory
// now let’s write something to it
bufferData.write("Hello World", "utf-8"); //we have given string and type as utf-8
Run above two lines and let’s check the output for it.
How to read Node.js Buffers?
Till now we have seen how to write to the buffer. Now it’s time to see how to read data from buffer. We can achieve this with the help of toString method. It very simple. Simply write below code in node.js command prompt. And enter.
it will give the string which we wrote above and remaining memory which was allocated earlier. As we allocated 32.
But if you see closely we don’t want used memory to show up. For this, we have some method to get the required output only. We will give the specified range to show up. In short, we are slicing it. Let’s see how can we implement that.
bufferData.toString("utf-8", 0, 11);
If you see in the above example the first two positions are occupied by our string. We gave index no 0 to 11. This contains our required string to be printed. And it returns Hello World as expected. We can also slice the buffer.
This is having two parameters one starts which shows a starting position. And other is the end which is the by default length of the buffer. Similarly, you can always check the length of the buffer by following method.
We have a method to compare with other buffer. Please follow the syntax given below:
Here, another buffer referring to the other created buffer. You can create any buffer as shown above in the article. Put it in the given syntax above and try on node.js command prompt. At some point, we need to convert buffer into Json format. This method returns the JSON structure of the buffer.
This will Give Jason Representation of buffer as below:
Advantages of using Node.js Buffers
- js buffers used to handle binary data.
- We don’t need to import buffer in node.js with require syntax.
- We always need to deal with binary data when it comes to TCP or a file system, buffer helps here.
- Buffer is a global class in node.js.
- Buffer has many methods to encode different types of data with binary.
js buffers are very useful in programming. To get more idea about it, one thing is practice. Try each and every possible aspect with different methods specified above. Buffers are really useful to handle data flow.
This is a guide to Node.js Buffers. Here we discuss the Introduction, how Node.js Buffers works? and how to create Node.js Buffers using various ways. You may also have a look at the following articles to learn more –