Learn the Concept of Buffers in Node.JS

1
6074
Buffers

In the last article, we discussed the concept of event emitters in Node.js. In this article, we are going to discuss on the concept of buffers in node.js and the various operations on the buffer along with the suitable examples.

Buffers in Node.js
JavaScript works on Unicode and not on the binary data. Therefore, when we are dealing with TCP streams for files, it is necessary to handle octet streams. Node.js provides the Buffer class that provides instances with which we can store raw data in the similar way when we deal with arrays. But here the raw data storage corresponds to a raw memory allocation which is outside the V8 heap.

Buffer Class
The Buffer class is a global class in Node.js which can be accessed in an application without importing the buffer module. We can create node buffers in the following ways.

  • Uninitiated Buffer of n octets: – The following is the syntax to create a buffer in Node.js n octets. Where n is a number say 100.

Output: – When we execute above code, we will observe the following output.
Out1

  • Buffer from a given array: – The following is the syntax to create a buffer from a given array.

Output: – When we execute above code, we will observe the following output.
Out2

  • Buffer from a given string: – The following is the syntax to create a Buffer from a given string and its encoding type which is an optional field. It should be noted that “utf8” is the default encoding type. Buffer class supports the following encoding types. They are “ascii”, “utf8”, “utf16le”, “ucs2”, “base64” and “hex”.

Output: – When we execute above code, we will observe the following output.
Out3

Buffer Operations
The following are the various Buffer operations in Node.js.

  • Writing to Buffer: – The Following is the syntax of the class method which is used to write into a Node Buffer.

Description: It has the following parameters
1. string – It is the actual string data to be written to buffer.
2. offset – It is the index of the buffer from where to start writing at. The default offset value is 0.
3. length − It is the number of bytes to write. The default length to buffer.length.
4. encoding – It is the encoding type to be used. The ‘utf8’ is the default encoding.

Return Value
This ‘write’ method returns the number of octets that are written. In case, there is no sufficient space in the buffer to fit the entire string, then it will write a part of the string as the return value

Example of ‘write’ method

Output: When we execute above code, we will observe the following output.
Out4

  • Reading from Buffers: – The Following is the syntax of the class method which is used to read from a Node Buffer.

Description: – It has the following parameters
1. encoding – It is the encoding type to use. The ‘utf8’ is the default encoding type.
2. start – It is the beginning index to start reading. The defaults value is 0.
3. end – It is the end index to end reading. The default value is the complete buffer.

Return Value
The above method decodes and returns a string from buffer data which is encoded as specified in the character set encoding.

Example

Output: – When we execute above code, we will observe the following output.
Out5

  • Convert Buffer to JSON: – This is the syntax of the class method which is used to convert a Node Buffer into JSON object. The returned value is a JSON-representation of the Buffer instance.

Example

Output: – When we execute above code, we will observe the following output.
Out6

  • Concatenate Buffers: – This is the syntax of the class method which is used to concatenate Node buffers to a single Node Buffer.

Description: – It has the following parameters
1. list – It is the array List of the Buffer objects which are to be concatenated.
2. totalLength − It is the total length of the buffers when they are concatenated.

Return Value
This method returns a Buffer instance composed of the concatenated buffers.

Example

Output: – When we execute above code, we will observe the following output.
Out7

  • Compare Buffers: – This is the syntax of the class method which is used to compare two Node buffers.

Description: – It has the following parameters
1. otherBuffer − It is the other buffer with which the buffer will be compared.

Return Value
It returns a number in (1, 0, and -1) that indicates whether it comes before or after or it is the same as that the other Buffer in the sorting order as shown in the below example.

Example

Output: – When we execute above code, we will observe the following output.
Out8

  • Copy Buffer: – This is the syntax of the class method which is used to copy a node buffer.

Description: – It has the following parameters
1. targetBuffer – It is the buffer object where buffer will be copied.
2. targetStart – It is the number that specifies the target buffer start offset. It is an Optional feild. The default value is 0.
3. sourceStart − It is the number that specifies the source buffer start offset. It is an optional feild. The default value is 0.
4. sourceEnd − It is the number that specifies the source buffer end offset. It is an optional feild. The default value is buffer.length

Return Value
It has no return value. It just copies the data from a region of this buffer to a region in the target buffer.

Example

Output: – When we execute above code, we will observe the following output.
Out9

  • Slice Buffer: – The Following is the syntax of the class method which is used to get a sub-buffer of a node buffer.

Description: – It has the following parameters
1. start – It is a number that specifies the start of the buffer. It is an optional field with default value as 0.
2. end − It is a number that specifies the end of the buffer. It is an optional field with default value as buffer.length

Return Value
It returns a new buffer which has the same reference memory as the old one, but the offset are cropped between the start (defaults to 0) and end (defaults to buffer.length) indexes. It should be noted that the negative indexes start from the end of the buffer.

Example

Output: When we execute above code, we will observe the following output.
Out10

  • Buffer Length: – This is the syntax of the class method which is used to get a size of a node buffer in bytes.

Return Value
It returns the size of a buffer in bytes.

Example

Output: – When we execute above code, we will observe the following output.
Out11

Class Methods Reference
The following are the few references of Buffers module available in Node.js.

S No.

Syntax

Description

1.

new Buffer(size)

This syntax is used to allocate a new buffer of size in octets. It should be noted that the size should not exceed the Maximum length, if exceeded then it will throw a RangeError.

2.

new Buffer(buffer)

This syntax is used to copy the buffer data which is passed as a parameter onto a new Buffer instance.

3.

new Buffer(str[, encoding])

This syntax is used to allocate a new buffer containing the given str. encoding defaults to ‘utf8’.

4.

buf.length

This syntax is used to return the size of the buffer in bytes. It should be noted that it is not necessarily the size of the contents. Actually, the length refers to the amount of memory allocated for the buffer object.

5.

buf.write (string[, offset][, length][, encoding])

This syntax is used to write a string to the buffer at offset using the given encoding. The default value of the offset is 0. The default value of encoding is ‘utf8’. The length is the number of bytes that we want to write. It returns the number of octets that we have written.

Source code for the concept of Buffer in Nodejs

Conclusion: –
In this article, we have covered the ‘Buffer’ class the various operations associated with the ‘Buffer’ class in Node.js along with the suitable examples which are required by a beginner developer to work.

1 COMMENT

  1. The Buffer constructors have been deprecated since v6.0.0 and should not be used. You should use Buffer.from() and Buffer.alloc() instead.

LEAVE A REPLY

Please enter your comment!
Please enter your name here