- 1 NodeJS Buffer Uses
- 2 Creating a Buffer in Node.js
- 3 Writing to Buffers in NodeJS
- 4 Reading from Buffers in NodeJS
- 5 To copy a buffer
- 6 To slice a buffer
- 7 Length of Buffer
NodeJS Buffer Uses
- At first, the buffers are introduced to be helpful for developers to access binary data in the ecosystem because the traditional method gets access to strings, not binaries.
- The buffers are linked with Streams. When the Stream processor receives data, it immediately transfers the data in a buffer.
- For example, a simple visual representation of buffer is while you watching any video on YouTube then the red line goes far away from the visual point. Similarly downloading data faster than you are viewing it.
Creating a Buffer in Node.js
The multiple ways of Node JS Buffer will help you to create a buffer. The three most commonly used methods are mentioned below.
The following syntax is used for creating an uninitiated buffer for 15 octets.
var buf = new Buffer(15);
Buffer from String
The syntax specifies the buffer creation from string with encoding type (optional)
var buf = new Buffer("Tutorials.freshernow","utf=8");
Buffer from Array
The below syntax is used to create a buffer from the already provided array.
var buf = new Buffer([10,15,20,25,30]);
Writing to Buffers in NodeJS
To write data into the node buffer, the buf.write() method is used.
buf.write() Method Syntax
buf.write(String, offset, length, encoding)
buf.write() Parameters Explanation
- string: To define the string data that written into the buffer.
- offset: To provide the index where the buffer starts writing. The default value is 0.
- length: It defines the number of bytes to write the data. The Default value is buffer.lenth.
- Encoding: It provides the encoding mechanism and its default value is “utf-8”.
The buf.write() method is used in strings that will return the number of octets. In case the buffer doesn’t contain the required space for the string then it will write a part of the string.
Reading from Buffers in NodeJS
To read the data from the buffer in Node JS use buf.toString() method.
buf.toString(encoding, start, end)
buf.toString() Parameters Explanation
- encoding: It provides the encoding mechanism and its default value is “utf-8”.
- start: It provides the index to start reading and its default value is 0.
- end: It provides the index that to end reading and its default value is a complete buffer.
To copy a buffer
The NodeJS allows you to copy a buffer with the help of a copy() method.
copy() Method Example
const buf = Buffer.from('Hey!') let bufcopy = Buffer.alloc(4) //allocate 4 bytes buf.copy(bufcopy)
To copy from the particular starting point to endpoint then use three more parameters.
const buf = Buffer.from('Hey!') let bufcopy = Buffer.alloc(2) //allocate 2 bytes buf.copy(bufcopy, 0, 2, 2) bufcopy.toString() //'He'
To slice a buffer
The slicing of the buffer is creating partial visualization of a buffer. A slice is not copying the buffer, the original buffer will be the same as you created. The slice() method is used to create and the parameters are starting point and ending point(optional).
slice() Method Example
const buf = Buffer.from('Hey!') buf.slice(0).toString() //Hey! const slice = buf.slice(0, 2) console.log(slice.toString()) //He buf = 111 //o console.log(slice.toString())
Length of Buffer
In Node JS, find the length of the buffer with the help of length property.
Length of Buffer Example
const buf = Buffer.from('Hey!') console.log(buf.length)
These are some operations that Node JS allows you to perform on Buffers.