NodeJS - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Node.js Buffer

Node.js Buffer

shape Introduction

The Node.js Buffer are the modules utilized to do some operations on raw bytes of data that reside in memory and this are not resizable. Even the buffer module is global in Node.js, following are the concepts covered.
  • Buffers
  • Encoding with Node.js
  • Node.js Buffer creating
  • Buffer Read and write
  • Buffer JSON representation
  • Buffer Copy and Slice functions

Buffers

shape Description

A buffer utilized to store the data when transforming from one place to another place and is a segment of physical memory. The buffer class can directly allocate with the binary data which are built in various types because the buffer class is global. It is mandatory to manage absolute binary streams of data while dealing with read, write and TCP streams. Despite the fact that JavaScript is a Unicode friendly however it can't deal with the straight binary data. Node has a few methodologies for creating, consuming octet and manipulating streams, in node the raw data is stored in instances of the buffer class.

Encoding with Node.js

shape Description

The encoding for the requires string should be indicated on order to convert the raw bytes of data into readable strings and this data can possibly convert into various string types and it depends on the developer to decide the string type. Following are various string encoding types.
Node.js Encoding Description
hex A byte can be encoded into two hexadecimal characters using this method.
base64 The base64 is an string encoding.
ascil The ascil method is fast and applicable only for 7 bit ASCIII data.
utf8 utf8 is the dominant encoding character for the world web wide.
utf16le The surrogate pairs and small endian encoded Unicode characters are supported.
ucs2 ucs2 is simialr to utf16le.
The function buffer.isEncoding(encoding) accepts only single argument, if the given node.js encoding type is valid then the function returns the value as true if not return the value as false as shown in the example below.

Node.js Buffer creating

shape Description

In Node.js buffers can be created in various ways as shown below. new Buffer(arr) - Array of octets can be used to create a buffer, the example below explain the use of new Buffer (arr) method. new buffer(n) - Buffer of ‘n’ octets can be created, the example below explain the use of new Buffer(n) method. new Buffer(str, [encoding]) - A buffer with string can be created with as shown in the example below using the new Buffer(str, [encoding]),the arguments string(str) type is required and the encoding string type  is an optional. buffer.isBuffer(obj) - The function buffer.isBuffer() check whether the given object is an instance of the buffer class and allow only single argument and returns true when the argument give is the buffer object if not then the value will be false as shown in the example below. buffer.byteLength(string[, encoding]) - The byte length of a string in the specified encoding is returned by using the function buffer.byteLength(), The function have 2 arguments as follows. utf8’ is the default encoding if not give. The example below give the byte length of the given string.

Buffer Read and write

shape Description

Write to Buffers - A string can be write to the buffer using the method buffer.write(string, [offset], [length], [encoding]), the method have four arguments, following are the arguments used in this method. The first argument is required argument and whereas the second, third and fourth are the optional argumnts. The number of written octets are returned, when a buffer can’t fit the string due to insufficient space then a portion of the string is written. The example below is used to create a buffer using the buffer.write() method. The method returns the value 26 which means, 26 bytes of the buffer have been written and even the given string “SPLessons Online Tutorials” is 26. Reading from buffer - A string can be read from the buffer using the method buffer.toString([encoding], [strat], [end]), the function toString() is the member function of buffer object which is returned by the statement new Buffer();. The method have three arguments and all the three arguments are optional. Following are the three arguments. The example explains the working of read from buffer using the buffer.toString() method.

Buffer JSON representation

shape Description

The method buffer.toJSON() gives the JSON representation of buffer, in which the .toJSON() is a member function of the buffer object which is returned by a statement new Buffer();. The example below explains the representation of JSON of buffer.

Buffer Copy and slice functions

shape Description

buffer.copy() function - One buffer can be copied into another buffer using the buffer.copy(target[,targetStart[,sourceStart[,sourceEnd]]]) function which is a member of the buffer object that returned by the statement new Buffer();. This buffer class function have 4 arguments as follows. The example explains the working of buffer.copy() function which copies one buffer into another buffer. buffer.slice() function - A buffer with its original part can be created using the function buffer.slice([start[,end]]) which is a member of the buffer object that is returned by the statement new Buffer();. Actually the buffer do not create a new buffer but the original buffer instance is create with the some part of content by which changes in the created buffer effects the change in original buffer. The function have 2 arguments as follows. The example explains the working of buffer.slice() function which creates a instance for the original buffer.

Summary

shape Key Points

  • The raw data is instances stored in the Node.js buffer class.
  • The Node.js buffer store the data for temporary while moving from one place to another.
  • The Node.js buffer class is a global calls and can be accessed in any application and can be created in different ways.