SoFunction
Updated on 2025-03-04

Analysis of the usage of buffer buffer in the basic nodejs

This article describes the usage of buffer buffers in the basic nodejs. Share it for your reference, as follows:

JavaScript Language itselfOnly string data types, no binary data types. However, when processing streams like TCP or file streams, binary data must be used. Therefore, in , a Buffer class is defined, which is used to create a cache area that specifically stores binary data. In , the Buffer class is a core library released with the Node kernel. The Buffer library brings a way to store raw data, allowing the processing of binary data whenever it needs to be inWhen processing data moved during I/O operations, it is possible to use the Buffer library. The raw data is stored in an instance of the Buffer class. A Buffer is similar to an array of integers, but it corresponds to a piece of raw memory outside of V8 heap memory.

Create a buffer class

1. Create a buffer instance with a specified byte length

var buffer = new Buffer(10); //Create a buffer instance with a length of 10 bytes

2. Create a buffer instance through an array

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

3. Create a buffer instance through a string

var buffer = new Buffer('','utf-8');
//utf-8 is the default encoding method. In addition, the following encodings can be specified: "ascii", "utf8", "utf16le", "ucs2", "base64" and "hex"

Buffer length

grammar:

;

Return value:

Returns the length of memory occupied by the Buffer object.

Example:

var buffer = new Buffer('');
// Buffer length("buffer length: " + ); //buffer length: 14

Write to the buffer

grammar:

(string[, offset][, length][, encoding])

parameter:

  • string: A string written to the buffer.
  • offset: The index value that the buffer starts to write, defaults to 0.
  • length: The number of bytes written, default is
  • encoding: The encoding used. Default is 'utf8' 。

Return value:

Returns the actual size (number type). If the buffer space is insufficient, only part of the string will be written.

Example:

buf = new Buffer(256);
len = ("");
("Written bytes: "+ len); //Written bytes: 14

Read data from the buffer

grammar:

([encoding][, start][, end])

parameter:

  • encoding: The encoding used, default is utf-8
  • start: Specify the index position to start reading, default is 0
  • end: Specify the end position of the read, default to the end of the buffer

Return value:

Decode the buffer data and return the string using the specified encoding

Example:

buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
 buf[i] = i + 97;
}
( ('ascii'));  // Output: abcdefghijklmnopqrstuvwxyz( ('ascii',0,5)); // Output: abcde( ('utf8',0,5)); // Output: abcde( (undefined,0,5)); // Use 'utf8' to encode and output: abcde

Convert buffer object to json object

grammar:

toJSON()method

Return value:

json object

Example:

var buf = new Buffer('');
var json = (buf);
(json); //[ 119, 119, 119, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109 ]

Buffer merge

grammar:

(list[, totalLength])

parameter:

  • list: A list of arrays of Buffer objects used for merge.
  • totalLength: Specifies the total length of the merged Buffer object.

Return value:

Returns a new Buffer object that is merged with multiple members.

Example:

var buf1 = new Buffer("Google URL:");
var buf2 = new Buffer("");
var buf3 = ([buf1,buf2]);
('buf1's content is:'+()); //The content of buf1 is: Google URL:('buf2's content is:'+()); //The content of buf2 is:('buf3's content is:'+()); //The content of buf3 is: Google URL:

Buffer copy

grammar:

(targetBuffer[, targetStart][, sourceStart][, sourceEnd])

parameter:

The parameters are described as follows:

  • targetBuffer- Buffer object to copy.
  • targetStart- Number, optional, default: 0
  • sourceStart- Number, optional, default: 0
  • sourceEnd- Number, optional, default:

Return value:

No return value.

Example:

var buf1 = new Buffer('ABC');
// Copy a buffervar buf2 = new Buffer(2);
var buf3 = new Buffer(3);
var buf4 = new Buffer(5);
var buf5 = new Buffer('abcdef');
(buf2);
(buf3);
(buf4);
(buf5);
("buf2 content: " + ()); //buf2 content: AB
("buf3 content: " + ()); //buf3 content: ABC
("buf4 content: " + ()); //buf4 content: ABC has opened up a lot of memory, so I'll get a bunch of weird symbols here("buf5 content: " + ()); //buf5 content: ABCdef

Buffer clipping

grammar:

([start][, end])

parameter:

The parameters are described as follows:

  • start- Number, optional, default: 0
  • end- Number, optional, default:

Return value:

Returns a new buffer pointing to the same memory as the old buffer, but cuts from the index start to end.

Example:

var buffer1 = new Buffer('runoob');
// Cut buffervar buffer2 = (0,2);
("buffer2 content: " + ()); //buffer2 content: ru

I hope this article will be helpful to everyone's nodejs programming.