Handling Buffers in NodeJS

Last updated on Jan 18 2023
Prabhas Ramanathan

Table of Contents

Node.js – Buffers

Pure JavaScript is Unicode friendly, but it is not so for binary data. While dealing with TCP streams or the file system, it’s necessary to handle octet streams. Node provides Buffer class which provides instances to store raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.
Buffer class is a global class that can be accessed in an application without importing the buffer module.

Creating Buffers

Node Buffer can be constructed in a variety of ways.

Method 1

Following is the syntax to create an uninitiated Buffer of 10 octets −
var buf = new Buffer(10);

Method 2

Following is the syntax to create a Buffer from a given array −
var buf = new Buffer([10, 20, 30, 40, 50]);

Method 3

Following is the syntax to create a Buffer from a given string and optionally encoding type −
var buf = new Buffer(“Simply Easy Learning”, “utf-8”);
Though “utf8” is the default encoding, you can use any of the following encodings “ascii”, “utf8”, “utf16le”, “ucs2”, “base64” or “hex”.

Writing to Buffers

Syntax

Following is the syntax of the method to write into a Node Buffer −
buf.write(string[, offset][, length][, encoding])

Parameters

Here is the description of the parameters used −
• string − This is the string data to be written to buffer.
• offset − This is the index of the buffer to start writing at. Default value is 0.
• length − This is the number of bytes to write. Defaults to buffer.length.
• encoding − Encoding to use. ‘utf8’ is the default encoding.

Return Value

This method returns the number of octets written. If there is not enough space in the buffer to fit the entire string, it will write a part of the string.

Example

buf = new Buffer(256);
len = buf.write(“Simply Easy Learning”);

console.log(“Octets written : “+ len);
When the above program is executed, it produces the following result −
Octets written : 20

Reading from Buffers

Syntax

Following is the syntax of the method to read data from a Node Buffer −
buf.toString([encoding][, start][, end])

Parameters

Here is the description of the parameters used −
• encoding − Encoding to use. ‘utf8’ is the default encoding.
• start − Beginning index to start reading, defaults to 0.
• end − End index to end reading, defaults is complete buffer.

Return Value

This method decodes and returns a string from buffer data encoded using the specified character set encoding.

Example


buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
buf[i] = i + 97;
}

 

console.log( buf.toString(‘ascii’)); // outputs: abcdefghijklmnopqrstuvwxyz
console.log( buf.toString(‘ascii’,0,5)); // outputs: abcde
console.log( buf.toString(‘utf8’,0,5)); // outputs: abcde
console.log( buf.toString(undefined,0,5)); // encoding defaults to ‘utf8’, outputs abcde
When the above program is executed, it produces the following result −
abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde

Convert Buffer to JSON

Syntax

Following is the syntax of the method to convert a Node Buffer into JSON object −

buf.toJSON()
Return Value
This 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);
When the above program is executed, it produces the following result −
{ type: 'Buffer',
data: 
[ 
83,
105,
109,
112,
108,
121,
32,
69,
97,
115,
121,
32,
76,
101,
97,
114,
110,
105,
110,
103 
]
}

Concatenate Buffers

Syntax

Following is the syntax of the method to concatenate Node buffers to a single Node Buffer −
Buffer.concat(list[, totalLength])

Parameters

Here is the description of the parameters used −
• list − Array List of Buffer objects to be concatenated.
• totalLength − This is the total length of the buffers when concatenated.

Return Value

This method returns a Buffer instance.

Example

var buffer1 = new Buffer(' tecklearn ');
var buffer2 = new Buffer('Simply Easy Learning');
var buffer3 = Buffer.concat([buffer1,buffer2]);

console.log("buffer3 content: " + buffer3.toString());

When the above program is executed, it produces the following result −

buffer3 content: tecklearn Simply Easy Learning

Compare Buffers

Syntax

Following is the syntax of the method to compare two Node buffers −
buf.compare(otherBuffer);

Parameters

Here is the description of the parameters used −
• otherBuffer − This is the other buffer which will be compared with buf

Return Value

Returns a number indicating whether it comes before or after or is the same as the otherBuffer in sort order.

Example


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);
}

 

When the above program is executed, it produces the following result −
ABC comes before ABCD

Copy Buffer

Syntax

Following is the syntax of the method to copy a node buffer −
buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])

Parameters

Here is the description of the parameters used −
• targetBuffer − Buffer object where buffer will be copied.
• targetStart − Number, Optional, Default: 0
• sourceStart − Number, Optional, Default: 0
• sourceEnd − Number, Optional, Default: buffer.length

Return Value

No return value. Copies data from a region of this buffer to a region in the target buffer even if the target memory region overlaps with the source. If undefined, the targetStart and sourceStart parameters default to 0, while sourceEnd defaults to buffer.length.

Example

var buffer1 = new Buffer(‘ABC’);

//copy a buffer
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log(“buffer2 content: ” + buffer2.toString());

When the above program is executed, it produces the following result −
buffer2 content: ABC

Slice Buffer

Syntax

Following is the syntax of the method to get a sub-buffer of a node buffer −
buf.slice([start][, end])

Parameters

Here is the description of the parameters used −
• start − Number, Optional, Default: 0
• end − Number, Optional, Default: buffer.length

Return Value

Returns a new buffer which references the same memory as the old one, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.

Example


var buffer1 = new Buffer(' tecklearn');

//slicing a buffer
var buffer2 = buffer1.slice(0,9);
console.log("buffer2 content: " + buffer2.toString());

 

When the above program is executed, it produces the following result −
buffer2 content: Tutorials

Buffer Length

Syntax

Following is the syntax of the method to get a size of a node buffer in bytes −
buf.length;

Return Value

Returns the size of a buffer in bytes.

Example


var buffer = new Buffer(' tecklearn');

//length of the buffer
console.log("buffer length: " + buffer.length);

When the above program is executed, it produces following result −
buffer length: 14

Methods Reference

Following is a reference of Buffers module available in Node.js. For more detail, you can refer to the official documentation.

Class Methods

Sr.No. Method & Description
1 Buffer.isEncoding(encoding)

Returns true if the encoding is a valid encoding argument, false otherwise.

2 Buffer.isBuffer(obj)

Tests if obj is a Buffer.

3 Buffer.byteLength(string[, encoding])

Gives the actual byte length of a string. encoding defaults to ‘utf8’. It is not the same as String.prototype.length, since String.prototype.length returns the number of characters in a string.

4 Buffer.concat(list[, totalLength])

Returns a buffer which is the result of concatenating all the buffers in the list together.

5 Buffer.compare(buf1, buf2)

The same as buf1.compare(buf2). Useful for sorting an array of buffers.

So, this brings us to the end of blog. This Tecklearn ‘Handling Buffers in NodeJS’ blog helps you with commonly asked questions if you are looking out for a job in NodeJS Programming. If you wish to learn NodeJS and build a career in NodeJS Programming domain, then check out our interactive, Node.js Training, that comes with 24*7 support to guide you throughout your learning period.

Node.js Training

About the Course

Tecklearn’s Node.js certification training course familiarizes you with the fundamental concepts of Node.js and provides hands-on experience in building applications efficiently using JavaScript. It helps you to learn how to develop scalable web applications using Express Framework and deploy them using Nginx. You will learn how to build applications backed by MongoDB and gain in-depth knowledge of REST APIs, implement testing, build applications using microservices architecture and write a real-time chat application using Socket IO. Accelerate your career as a Node.js developer by enrolling into this Node.js training.

Why Should you take Node.js Training?

• As per Indeed.com data, the average salary of Node.js developer is about $115000 USD per annum.
• IBM, LinkedIn, Microsoft, GoDaddy, Groupon, Netflix, PayPal, SAP have adopted Node.js – ITJungle.com
• There are numerous job opportunities for Node.js developers worldwide. The job market and popularity of Node.js is constantly growing over the past few years.

What you will Learn in this Course?

Introduction to Node.js

• What is Node.js?
• Why Node.js?
• Installing NodeJS
• Node in-built packages (buffer, fs, http, os, path, util, url)
• Node.js Modules
• Import your own Package
• Node Package Manager (NPM)
• Local and Global Packages

File System Module and Express.js

• File System Module
• Operations associated with File System Module
• JSON Data
• Http Server and Client
• Sending and receiving events with Event Emitters
• Express Framework
• Run a Web Server using Express Framework
• Routes
• Deploy application using PM2 and Nginx

Work with shrink-wrap to lock the node module versions

. What is shrink-wrap
• Working with npmvet
• Working with outdated command
• Install NPM Shrinkwrap

Learn asynchronous programming

• Asynchronous basics
• Call-back functions
• Working with Promises
• Advance promises
• Using Request module to make api calls
• Asynchronous Commands

Integration with MongoDB and Email Servers

• Introduction to NoSQL Databases and MongoDB
• Installation of MongoDB on Windows
• Installation of Database GUI Viewer
• Inserting Documents
• Querying, Updating and Deleting Documents
• Connect MongoDB and Node.js Application
• Exploring SendGrid
• Sending emails through Node.js application using SendGrid

REST APIs and GraphQL

• REST API
• REST API in Express
• Postman
• MongoDB Driver API
• Express Router
• Mongoose API
• GraphQL
• GraphQL Playground

Building Node.js Applications using ES6

• ES6 variables
• Functions with ES6
• Import and Export withES6
• Async/Await
• Introduction to Babel
• Rest API with ES6
• Browsing HTTP Requests with Fetch
• Processing Query String
• Creating API using ES6
• Building Dashboard API
• Creating dashboard UI with EJS
• ES6 Aside: Default Function Parameters
• Data Validation and Sanitization

User Authentication and Application Security

• Authentication
• Types of Authentication
• Session Vs Tokens
• JSON Web Tokens
• Bcrypt
• Node-local storage

Understand Buffers, Streams, and Events

• Using buffers for binary data
• Flowing vs. non-flowing streams
• Streaming I/O from files and other sources
• Processing streams asynchronously
• File System and Security

Build chat application using Socket.io

• Getting Started
• Adding Socket.io To Your App
• Exploring the Front-end
• Sending Live Data Back & Forth
• Creating the Front-end UI
• Showing Messages In App
• Working with Time
• Timestamps
• Show Message Time In Chat App
• Chat application Project

Microservices Application

• Why Microservices?
• What is Microservices?
• Why Docker?
• What is Docker?
• Terminologies in Docker
• Child Processes
• Types of child process

 

0 responses on "Handling Buffers in NodeJS"

Leave a Message

Your email address will not be published. Required fields are marked *