The Buffer class implements the Uint8Array API in a way better suited to Node.js. Used to process binary bytes in TCP streams, file system operations, and other scenarios
Bit and Byte
- A bit is the smallest unit of information in binary system. A bit is what we call a bit, and a 64-bit operating system CPU can process it at a timeA data
- Byte is a unit used to measure storage or transmission traffic, such as disk capacity and network speed. An English character is a Byte, which is called 1B. Chinese characters are usually two bytes.
1 byte = 8 bit
Copy the code
Buffers, on the other hand, deal with bytes, counting from 0, 255 in Buffer identifies a byte with 1 bits
Buffer features
An instance of the Buffer class is similar to an array of integers between 0 and 255 (other integers are coerced into this range by the &255 operation). Buffer is a JavaScript and C++ module that does not allocate object memory by V8. It is applied by C++ and assigned by JavaScript. The size of the buffer is determined at creation time and cannot be adjusted.
Buffer objects are so common that they are built directly into global variables that require is not required
Instantiate the Buffer
Before Node.js v6, buffers were instantiated by calling constructors that returned different results depending on the parameters. For security reasons, this approach has been deprecated since v6 and now provides three distinct functions to handle the job of instantiating buffers
Buffer.from
Buffer.from supports four parameter types
- Buffer.from(string [, encoding]) : Returns a Buffer containing the given string
- Buffer.from(Buffer) : Returns a copy of the given Buffer
- Buffer.from(array) : Returns a Buffer containing a copy of the supplied bytes. Each item in the array is a number representing eight bytes, so the value must be between 0 and 255, otherwise modulo will be taken
- Buffer.from(arrayBuffer) : Returns a new Buffer that shares memory with the given arrayBuffer
- Buffer. From (object[, offsetOrEncoding[, length]]) : Initialize Buffer with valueOf or symbol. toPrimitive of object
// Display values in hexadecimal format
const buf1 = Buffer.from('test'.'utf-8'); // <Buffer 74 65 73 74>
const buf2 = Buffer.from(buf1); //
, buf1 copy, modifying buf2 does not affect BUF1
const buf3 = Buffer.from([256.2.3]); //
const arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
// Share memory with ARR
const buf4 = Buffer.from(arr.buffer);
console.log(buf4); // <Buffer 88 13 a0 0f>
/ / modify arr
arr[1] = 6000;
console.log(buf4);
BUF4 is also affected
const buf = Buffer.from(new String('this is a test'));
// <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
Copy the code
Buffer.alloc(size [, fill [, encoding]])
Allocate a new Buffer of size bytes. If fill is undefined, fill the Buffer with 0
- Size Specifies the required length of the new Buffer
- The fill | | | for pre-filled with the new value of the Buffer. Default value: 0
- Encoding If fill is a string, this is its character encoding. Default value: UTf8
const buf = Buffer.alloc(5);
console.log(buf); // <Buffer 00 00 00 00 00>
Copy the code
If fill is specified, the allocated Buffer is initialized by calling buf.fill(fill).
const buf = Buffer.alloc(5.'a');
console.log(buf); // <Buffer 61 61 61 61 61>
Copy the code
Buffer.allocUnsafe(size)
AllocUnsafe allocates a new Buffer of size bytes. AllocUnsafe executes faster than alloc, but the name of the method sounds unsafe, and it isn’t
const buf = Buffer.allocUnsafe(10);
console.log(buf); // The print content is not certain
Copy the code
The Buffer module preallocates a Buffer instance with an internal size of buffer.poolsize, which acts as a quick allocation pool for creating new Buffer instances using allocUnsafe(). Alloc will never use the internal Buffer pool, AllocUnsafe will use an internal Buffer pool if its size is less than or equal to half of buffer. poolSize. When allocUnsafe is called, the allocated memory segment is not initialized (it does not return to zero). This allocation is slow, but the allocated memory segment may contain old data. If you do not overwrite the old data when using it, you can cause a memory leak, although it is fast, avoid using it
Buffer.allocUnsafeSlow(size)
AllocUnsafe is faster than allocUnsafeSlow because it uses the preallocated Buffer directly when the allocated space is less than 4KB. When you’re greater than or equal to 4 kilobytes, there’s no difference
coding
Buffer currently supports the following encoding formats
- ascii
- utf8
- utf16le
- base64
- binary
- hex
Buffer and string conversion
Buffer.from(string [, encoding]) The string is converted to Buffer
const buf1 = Buffer.from('test'.'utf-8'); // <Buffer 74 65 73 74>
Copy the code
Buf.tostring ([encoding[, start[, end]]]) The toString method of the Buffer instance converts Buffer to a string
const buf1 = Buffer.from('test'.'utf-8');
console.log(buf1.toString()); // test
console.log(buf1.toString('hex')); / / 74657374
Copy the code
Buffer stitching
The buffer. concat(list[, totalLength]) method concatenates multiple Buffer instances into a single Buffer instance
const buf1 = Buffer.from('a');
const buf2 = Buffer.from('b');
const buf3 = Buffer.from('c');
const buf = Buffer.concat([buf1, buf2, buf3]);
console.log(buf); // <Buffer 61 62 63>
Copy the code
StringDecoder
In NodeJS, a Chinese character is represented by three bytes. If we use a number of bytes that is not a multiple of 3 when processing Chinese characters, it will cause character Mosaic garbled characters
const buf = Buffer.from('Chinese string! ');
for(let i = 0; i < buf.length; i+=5) {var b = Buffer.allocUnsafe(5);
buf.copy(b, 0, i);
console.log(b.toString());
}
Copy the code
You can see garbled characters in the result
Use the string_decoder module to solve this problem
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');
const buf = Buffer.from('Chinese string! ');
for(let i = 0; i < buf.length; i+=5) {var b = Buffer.allocUnsafe(5);
buf.copy(b, 0, i);
console.log(decoder.write(b));
}
Copy the code
After being encoded, StringDecoder knows that wide bytes count for three bytes under UTF-8, so processing incomplete bytes is reserved until the second write(). Currently, only UTF-8, Base64, and UCS-2/UTF-16LE can be processed.
Other common Buffer apis
There are also some common Buffer apis, which are relatively simple without using code examples
- Buffer.isBuffer: Checks whether the object is a Buffer
- Buffer.isEncoding: Determines the Buffer object encoding
- Buf. length: Returns the number of bytes requested by memory for this Buffer instance, not the number of bytes of the Buffer instance content
- Buf. indexOf: Similar to array indexOf, returns the position of a string, acSIi code, or BUF in a modified BUF
- Buf. copy: Copies the (partial) contents of one BUF to another
Now that you know this, you’re ready to learn about stream
The minimalist Node. Js introductory tutorial: www.yuque.com/sunluyong/n…
In this paper, a better reading experience: www.yuque.com/sunluyong/n…