Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js JavaScript Learn
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
Node.js OS
Node.js Timer
Node.js Errors
Node.js DNS
Node.js Net
Node.js Crypto
Node.js TLS/SSL
Node.js Debugger
Node.js Process
Node.js Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
Node.js Email Send
Node.js BuiltIn Modules
Node.js Assert Code
Node.js Assert Function
Node.js console.time()
Node.js console.table()
Node.js DNS Code
Node.js cipher.final()
Node.js URL.hash API
Node.js URL.host API
Node.js URL.origin API
Node.js MySQL
Folders in Node.js
Folders Node.js part3
Rename Folder Node.js
Remove Folder Node.js
Read Email File
File Upload Container
 


  

 
Node.js Buffers
Question Posted on 01 Apr 2024

Home >> Tutorial >> Node.js Tutorial >> Node.js Buffers


Node.js Buffers

Node.js Buffers


Well when we say about the Node.js Buffers, and this Buffer class is used to store raw data which is similar to an array of integers but this is corresponds to a raw memory allocation outside the V8 heap. Here we used Buffer class and the reason behind this is that as JavaScript is not nice to binary data. SO overcome this about the binary data we use Buffer class. So when dealing with TCP streams or file system, this is necessary to handle octet streams.

Buffer class is one of the global class. And this can be accessed in application without importing buffer module.


Node.js Creating Buffers


There are many ways to create a Node buffer. Below are the 3 mostly used methods:-

(1)Create an uninitiated buffer:-Well below is the syntax to create an uninitiated buffer of 10 octets:-
var buf = new Buffer(10);

(2)Create an buffer from array:-Well below is the syntax to create a Buffer from a given array:-
var buf = new Buffer([10, 20, 30, 40, 50]);

(3)Create a buffer from string:-Well below is the syntax to create a Buffer from agiven string and optionally encoding type:-
var buf = new Buffer("Simple and Easy Learning", "utf-8");


Node.js Writing to Buffers


To write a method into a Node buffer. And below is the syntax to write node.js buffers:-
buf.write(string[, offset][, length][, encoding])

Parameter explanation:-
  • string:-Here we will specifies the string data to be written to buffer.

  • offset:-Here we will specifies the index of the buffer to start writing at. Its default value is 0.

  • length:-Here we will specifies the number of bytes to write.Defaults to buffer.length

  • encoding:-Encoding to use. 'utf8' is the default encoding.


Return values from writing buffers:-
Well this method is mainly used to return number of octets written. And in the case of space shortage for buffer to fit the entire string, this will write a part of the string.

To understand take example:-
Now we will create a JavaScript file which named as "main.js" which is haveing the below code with file name:-

File: main.js
buf = new Buffer(256);
len = buf.write("Simply Easy Learning");
console.log("Octects written: " + len);


Now we open Node.js command prompt to execute the below code:-

node main.js

Now get the output of above file:-


Buffer Write Class

Node.js Reading from buffers


Well below is the method to read data from a Node buffer. And below is the syntax:-

buf.toString([encoding][, start][, end])

Parameter explanation:-
  • encoding:-Here we will specifies the encoding to use. 'utf8' is the default encoding.

  • start:-Here we will specifies the beginning index to start reading, defaults to 0.

  • end:-Here we will specifies end index to end reading, defaults is complete buffer.


Return values from writing buffers:-
Well this method decodes and returns a string from buffer data encoded using the specified character set encoding. And below is the example and file:-

File: main.js


Read Buffer Node.js
Now open Node.js command prompt and then execute the below command:-
node main.js

And below is the output after execute the read buffer:-


Read Buffer Node.js





Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |