- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
Node.js - Buffer.concat() Method
The NodeJS Buffer.concat() method will return a single buffer object by concatenating all the given buffer objects. If the buffers used have no items or the length of the buffers is 0, the final buffer created will be a new zero-length buffer.
Syntax
Following is the syntax of the Node.JS Buffer.concat() Method −
Buffer.concat( array, length )
Parameters
This method accepts two parameters. The same is explained below.
array −(required) An array of buffers that needs to be concatenated.
length −(optional). The length you want for the final concatenated buffer. If not given the length will be the sum of the given buffer lengths used in concat().
Return value
The method Buffer.concat() will return a buffer with its length equivalent to the length of the buffers used in concatenating or unless specified by using the length parameter in buffer.concat().
Example
The example will create a new buffer using NodeJS buffer.concat() method.
const buffer1 = Buffer.from('A'); const buffer2 = Buffer.from('B'); const buffer3 = Buffer.from('C'); var myarray = [buffer1, buffer2, buffer3]; var newBuffer = Buffer.concat(myarray); console.log(newBuffer); console.log("The length of new buffer is : "+newBuffer.length);
Output
<Buffer 41 42 43> The length of new buffer is : 3
Example
In this example let us create a new buffer and also specify the length of the buffer.
const buffer1 = Buffer.from('Hello'); const buffer2 = Buffer.from('How'); const buffer3 = Buffer.from('are you!'); var myarray = [buffer1, buffer2, buffer3]; var newBuffer = Buffer.concat(myarray, 10); console.log(newBuffer); console.log(newBuffer.toString()); console.log("The length of new buffer is : "+newBuffer.length);
Output
The length of the final buffer given is 10. Even though the buffers in the array has length more than the specified length, it will still fall back to the length specified. You can check the same in the output.
<Buffer 48 65 6c 6c 6f 48 6f 77 61 72> HelloHowar The length of new buffer is : 10
Example
The example will create a new buffer using the buffer.concat() method by using Buffer.alloc() and Buffer.fill().
const buffer1 = Buffer.alloc(5); const buffer2 = Buffer.alloc(5); buffer1.fill('H'); buffer2.fill('W'); var myarray = [buffer1, buffer2]; var newBuffer = Buffer.concat(myarray); console.log(newBuffer); console.log("The length of new buffer is : "+newBuffer.length);
Output
<Buffer 48 48 48 48 48 57 57 57 57 57> The length of new buffer is : 10
To Continue Learning Please Login
Login with Google