We now have a youtube channel. Subscribe!

Node.js | Buffers

Node.js Buffers


Hello folks! welcome back to a new edition of our tutorial on Node.js. In this section of our tutorial on Node.js, we will be studying about Node.js Buffers.

Pure JavaScript is unicode friendly, however it isn't so for binary data. While handling TCP streams or the file system, it is necessary to handle octet streams. Node provides Buffer class which provides instances to store raw data that's similar to an array of integers but tallies with a raw memory allocation outside the V8 heap.

Node Buffer class is a global class that can be obtained in an application without having to import the buffer module.

Creating Buffers

Node buffers can be created in many ways -

First Method

The following is the syntax for creating an uninitiated Buffer of 10 octets -

var buf = new Buffer(10);

Second Method

Following is the syntax for creating a Buffer from a given array -

var buf = new Buffer([10, 20, 30, 40, 50]);

Third Method

Following is the syntax for creating a Buffer from a given string and optionally encoding type -

var buf = new Buffer("Best Tutorial Platform", "utf-8");

Although "utf8" is the default encoding, you can use any of the following encodings like "hex", "base64", "ascii", "ucs2", "utf16le", or "utf8".


Writing to Buffers

Syntax

The following is the syntax of the method to write into a Node Buffer -

buf.write(string[, offset][, length][, encoding])

Parameter Details

Below is the description of the parameters used -

  • string - This is the string data to be written to buffer.
  • offset - This is the buffer's index to begin writing at. Default value is 0.
  • length - This is the number of bytes to write. Defaults to buffer.length.
  • encoding - The 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 whole string, it will write a part of the string.

Example

Try the following example below -

buf = new Buffer(256);
len = buf.write("Best Tutorial Platform");

console.log("Octets written : "+  len);

Output

When the above program is executed, it will produce the following result -

Octets written : 22

Reading from Buffers

Syntax

The following is the syntax of the method to read data from a Node Buffer -

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

Parameter Details

Below is the description of the parameters used -

  • encoding - This is the encoding to use. 'utf8' is the default encoding.
  • start - This the beginning index to start reading, defaults to 0.
  • end - This is the end index to end reading, defaults to complete buffer.

Return Value

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

Example

Try the following example below -

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

Output

When the above program is executed, it will produce the following result -

abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde

Convert Buffer to JSON

Syntax

The following is the syntax of the method to convert a Node Buffer into a JSON object -

buf.toJSON()

Return Value

This method returns a JSON-representation of the Buffer instance.

Example

Try the following example below -

var buf = new Buffer('Simply Easy Learning');
var json = buf.toJSON(buf);

console.log(json);

Output

When the above program is executed, it will produce 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

The following is the syntax of the method to concatenate Node buffers to a single Node Buffer -

Buffer.concat(list[, totalLength])

Parameter Details

Below is the description of the parameters used -

  • list - This is an array list of Buffer objects to be concatenated.
  • totalLength - This is the total of the buffers when concatenated.

Return Value

This method returns a Buffer instance.

Example

Try the following example below -

var buffer1 = new Buffer('WebDesignTutorialz ');
var buffer2 = new Buffer('Best Tutorial Platform');
var buffer3 = Buffer.concat([buffer1,buffer2]);

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

Output

When the above program is executed, it will produce the following result -

buffer3 content: WebDesignTutorialz Best Tutorial Platform

Compare Buffers

Syntax

The following is the syntax of the method to compare two Node buffers -

buf.compare(otherBuffer);

Parameter Details

Below is the description of the parameters used -

  • otherBuffer - This is the other buffer which will be compared with buf.

Return Value

This node method return a number showing whether it comes before or after or is same as the otherBuffer in sort order.

Example

Try the following example below -

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

Output

When the above program is executed, it will produce the following result -

ABC comes before ABCD

Copy Buffer

Syntax

The following is the syntax of the method to copy a node buffer -

buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])

Parameter Details

Below 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

This method has no return value. It copies data from a region of this buffer to a region in the target buffer regardless of the target memory region overlapping with the source. If undefined, the targetStart and sourceStart parameters defaults to 0, while sourceEnd defaults to buffer.length.

Example

Try the following example below -

var buffer1 = new Buffer('ABC');

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

Output

When the above program is executed, it will produce 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])

Parameter Details

Below is the description of the parameters used -

  • start - Number, Optional, Default: 0.
  • end - Number, Optional, Default: buffer.length.

Return Value

It 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 starts from the end of the buffer.

Example

Try the following example below -

var buffer1 = new Buffer('WebDesignTutorialz');

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

Output

When the above program is executed, it will produce the following result -

buffer2 content: WebDesign

Buffer Length

Syntax

Following is the syntax of the method to get a size of a node buffer in bytes -

buf.length;

Return Value

This method returns the size of the buffer in bytes.

Example

Try the following example below -

var buffer = new Buffer('WebDesignTutorialz');

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

Output

When the above program is executed, it will produce the following result -

buffer length: 18

Method Reference

Following is a reference of Buffers modules available in Node.js. For more details, kindly refer to the official documentation.

Sr.No.Method & Description
1

new Buffer(size)

Allocates a new buffer of size octets. Note that the size must be no more than kMaxLength. Otherwise, a RangeError will be thrown here.

2

new Buffer(buffer)

Copies the passed buffer data onto a new Buffer instance.

3

new Buffer(str[, encoding])

Allocates a new buffer containing the given str. encoding defaults to 'utf8'.

4

buf.length

Returns the size of the buffer in bytes. Note that this is not necessarily the size of the contents. length refers to the amount of memory allocated for the buffer object. It does not change when the contents of the buffer are changed.

5

buf.write(string[, offset][, length][, encoding])

Writes a string to the buffer at offset using the given encoding. offset defaults to 0, encoding defaults to 'utf8'. length is the number of bytes to write. Returns the number of octets written.

6

buf.writeUIntLE(value, offset, byteLength[, noAssert])

Writes a value to the buffer at the specified offset and byteLength. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of value and offset. Defaults to false.

7

buf.writeUIntBE(value, offset, byteLength[, noAssert])

Writes a value to the buffer at the specified offset and byteLength. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of value and offset. Defaults to false.

8

buf.writeIntLE(value, offset, byteLength[, noAssert])

Writes a value to the buffer at the specified offset and byteLength. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of value and offset. Defaults to false.

9

buf.writeIntBE(value, offset, byteLength[, noAssert])

Writes a value to the buffer at the specified offset and byteLength. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of value and offset. Defaults to false.

10

buf.readUIntLE(offset, byteLength[, noAssert])

A generalized version of all numeric read methods. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.

11

buf.readUIntBE(offset, byteLength[, noAssert])

A generalized version of all numeric read methods. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.

12

buf.readIntLE(offset, byteLength[, noAssert])

A generalized version of all numeric read methods. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.

13

buf.readIntBE(offset, byteLength[, noAssert])

A generalized version of all numeric read methods. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.

14

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

Decodes and returns a string from buffer data encoded using the specified character set encoding.

15

buf.toJSON()

Returns a JSON-representation of the Buffer instance. JSON.stringify implicitly calls this function when stringifying a Buffer instance.

16

buf[index]

Get and set the octet at index. The values refer to individual bytes, so the legal range is between 0x00 and 0xFF hex or 0 and 255.

17

buf.equals(otherBuffer)

Returns a boolean if this buffer and otherBuffer have the same bytes.

18

buf.compare(otherBuffer)

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

19

buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])

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.

20

buf.slice([start][, end])

Returns a new buffer which references the same memory as the old, 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.

21

buf.readUInt8(offset[, noAssert])

Reads an unsigned 8 bit integer from the buffer at the specified offset. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.

22

buf.readUInt16LE(offset[, noAssert])

Reads an unsigned 16-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

23

buf.readUInt16BE(offset[, noAssert])

Reads an unsigned 16-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

24

buf.readUInt32LE(offset[, noAssert])

Reads an unsigned 32-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

25

buf.readUInt32BE(offset[, noAssert])

Reads an unsigned 32-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

26

buf.readInt8(offset[, noAssert])

Reads a signed 8-bit integer from the buffer at the specified offset. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

27

buf.readInt16LE(offset[, noAssert])

Reads a signed 16-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

28

buf.readInt16BE(offset[, noAssert])

Reads a signed 16-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

29

buf.readInt32LE(offset[, noAssert])

Reads a signed 32-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

30

buf.readInt32BE(offset[, noAssert])

Reads a signed 32-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

31

buf.readFloatLE(offset[, noAssert])

Reads a 32-bit float from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

32

buf.readFloatBE(offset[, noAssert])

Reads a 32-bit float from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

33

buf.readDoubleLE(offset[, noAssert])

Reads a 64-bit double from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

34

buf.readDoubleBE(offset[, noAssert])

Reads a 64-bit double from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.

35

buf.writeUInt8(value, offset[, noAssert])

Writes a value to the buffer at the specified offset. Note that the value must be a valid unsigned 8-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

36

buf.writeUInt16LE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid unsigned 16-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of correctness. Defaults to false.

37

buf.writeUInt16BE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid unsigned 16-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

38

buf.writeUInt32LE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid unsigned 32-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

39

buf.writeUInt32BE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid unsigned 32-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

40

buf.writeInt8(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 8-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

41

buf.writeInt16LE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 16-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

42

buf.writeInt16BE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 16-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

43

buf.writeInt32LE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 32-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

44

buf.writeInt32BE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 32-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of correctness. Defaults to false.

45

buf.writeFloatLE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid 32-bit float. Set noAssert to true to skip validation of value and offset. It means that the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

46

buf.writeFloatBE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note, value must be a valid 32-bit float. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

47

buf.writeDoubleLE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note, value must be a valid 64-bit double. Set noAssert to true to skip validation of value and offset. It means that value may be too large for the specific function and offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

48

buf.writeDoubleBE(value, offset[, noAssert])

Writes a value to the buffer at the specified offset with the specified endian format. Note, value must be a valid 64-bit double. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.

49

buf.fill(value[, offset][, end])

Fills the buffer with the specified value. If the offset (defaults to 0) and end (defaults to buffer.length) are not given, it will fill the entire buffer.


Class Methods

Following below is the list of class methods available in Node.js -

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.



Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will be studying about Node.js Streams.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain