Buffer A Buffer is a portion of memory that temporarily stores input and output data

The JS language has no binary data types, and you must deal with binary data when dealing with TCP and file streams

NodeJS provides a Buffer object that provides operations on binary data

Is a global object that represents memory allocation, meaning that the number of bytes to be put into the buffer needs to be determined in advance

A Buffer, like an array of multi-byte elements, can effectively represent binary data in JavaScript

byte

1024b = 1k;

8bit (8 bits) = 1b;

1 Chinese character (3 B’s)

A byte in decimal form is 255

The maximum conversion of 1 byte to hexadecimal is FF

There are three ways to define buffers

Define buffers by length

let buffer = Buffer.alloc("6");
let buffer = Buffer.allocUnsafe("6");
Copy the code

Define buffers by arrays

Let buffer = buffer. The from ([1, 2, 3, 4]); Length // The length of the buffer after converting to a bufferCopy the code

String creation creates buffer

Let buffer = buffer. From (" watch me ")Copy the code

Buffer. fill method to fill the contents of a buffer

The buffer.toString method converts a buffer to a string

Buffer. Slice method, which intercepts the desired buffer

Buffer. copy method to copy buffers

Buffer.concat method, the concatenation method of buffers

Buffer.isBuffer, check whether it is a Buffer