- 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.write() Method
The NodeJS Buffer.write() method will write the given string value to the buffer from the position given by the offset value. If the buffer does not have enough space to accommodate the string value, part of the string will be written. Please note it will not take care of writing partially encoded string value.
Syntax
Following is the syntax of the Node.JS Buffer.write() Method −
buf.write(string[, offset[, length]][, encoding])
Parameters
This method accepts four parameters. The same is explained below.
string − (required) The string value you want to write to the buffer.
offset − (optional)The offset that indicates the position to start writing. The default value is 0.
length − (optional) the number of bytes you want to write. The length cannot exceed buffer.length − offset. So the default value for length is buffer.length − offset.
encoding − (optional) The character encoding to be used for string value. The default is utf-8.
Return value
This method Buffer.write() returns the number of bytes written to the buffer.
Example
In this example will create a buffer using NodeJS Buffer.from() and later write a string value to the buffer created.
const buffer = Buffer.from('Hello'); buffer.write('World'); console.log("The final buffer is :"+buffer.toString());
Output
We have not used any offset value so by default it will be considered as 0. The length of the buffer created with the Hello string is 5. So when you write World to it which is of the same length as the buffer so a full string will be written.
The final buffer is :World
Example
In this example I will create a buffer using Buffer.alloc() and allocate 5 lengths to it. Now will use a bigger string to write to the buffer using Buffer.write() method.
const buffer = Buffer.alloc(5); buffer.write('Hello World'); console.log("The final buffer is :"+buffer.toString());
Output
We have not used any offset value so by default it will be considered as 0. The length of the buffer created is 5. Since we are writing a string with length: 11, the buffer will accommodate for only length 5. So when you see the output the buffer will write only the string Hello, as it can take only length of 5.
The final buffer is :Hello
Example
In this example I will check with an offset value. Will specify the offset in the Buffer.write() and write the string value to the buffer created.
const buffer = Buffer.alloc(5); buffer.write('Hello World', 3); console.log("The final buffer is :"+buffer.toString());
Output
The offset used is 3, the length of the buffer is 5, the length of the string to be written is 11. So the buffer will fill up from 3 and the buffer length i. e 5. So only characters He will be written to the buffer.
The final buffer is :He
Example
In this example will make use of length and encoding to write to the Buffer using Buffer.write() method.
const buffer = Buffer.alloc(10); buffer.write('Hello World', 3, 5, 'utf-8'); console.log("The final buffer is :"+buffer.toString());
Output
So the offset is from 3, the length of string we want to write is 5. The total length of the buffer is 10. Since the length to be written is 5, it will be possible to write since buffer.length − offset is 10 − 3 = 7. So it will easily accommodate the string value of length 5 from offset 3 in the buffer.
The final buffer is :Hello
To Continue Learning Please Login
Login with Google