The Buffer class was introduced as part of the Node.js API to make it possible to interact with octet streams in the context of things like TCP streams and file system operations.
Buffers are used to process binary data, and are the most common in networks and files.
Buffer object
In fact, when you first see a buffer, you will think that it looks like an array. The instance object of a buffer is like an array, but the size is fixed and you can allocate as much memory as you like. Moreover, buffer allocates physical memory outside the V8 heap, which is implemented by the C++ layer. Ok, let’s look at an example of a Buffer:
We can see what the buffer instance looks like, and the size of the buffer varies depending on the encoding of the string, such as utF-8, which is 3 for Chinese characters and 1 for letters and half-corners.
So what are the ways to create a Buffer instance?
// The method to create a Buffer instance
// 1. new Buffer(), the parameter passed in length, each Buffer element is filled with 0, but officially deprecated, recommend using the following
const buf1 = new Buffer(10);
console.log(buf1);
// 2.buffer.alloc (), can be passed two arguments, the first is the length of the Buffer, the second is the value of the Buffer fill, if not passed the second argument all 0 fill
const buf2 = Buffer.alloc(10.1);
console.log( buf2 );
// 3. buffer.allocunsafe (), which has obvious performance advantages because it does not need to be overwritten, but may contain old or sensitive data
const buf3 = Buffer.allocUnsafe(10);
console.log( buf3 );
// 4. buffer.from (), which can pass several parameters
// string
const buf4 = Buffer.from('node');
console.log(buf4 );
// array
const buf5 = Buffer.from([1.2.3]);
console.log( buf5 );
// copy the data in the buffer to a new buffer
const buf6 = Buffer.from(buf5);
console.log( buf6 );
// Of course, there is also an arrayBuffer, which is not covered hereCopy the code
Take a look at the output created above:
2. Some methods of Buffer
The method inside introduces several commonly used methods.
1. Buffer method
– Buffer.isBuffer()
As the name suggests, you can tell at a glance what this method is for and whether it is a Buffer.
– Buffer.byteLength()
This method is used to return the length of the buffer, similar to the length of the array.
– Buffer.concat()
The purpose of this method is to merge several buffers together to form a new buffer.
2. Method of Buffer instance
– buf.entries()
In fact, we can see that there are almost all the methods for arrays or arrays and objects in buffer, and the functions are similar. Therefore, when you use buffer, feel free to look up the methods you think should be available.
– buf.length
This returns the length of the buffer.
– buf.toString()
This method is similar to toString for Object.
Now, there’s a StringDecoder thing that I found out when I was learning about it on a couple of blogs and books. This thing comes from the string_decoder module. Let’s look at this object in code. There are also examples of simplicity.
// We all know that in UTF8, the Chinese character length is 3, so what happens if we print 5?
const buf = Buffer.from('It's been a wonderful day');
const buf1 = Buffer.alloc(5);
buf.copy(buf1, 0.0.5);
console.log(buf1.toString()); // Now, we can see the garbled characters because the remaining two bytes are not converted into Chinese characters
// Let's try another way
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');
const buf = Buffer.from('It's been a wonderful day');
const buf1 = Buffer.alloc(10);
const buf2 = Buffer.alloc(11);
buf.copy(buf1, 0.0.10);
buf.copy(buf2, 0.10.21);
console.log(decoder.write(buf1)); / / this is beautiful
console.log(decoder.write(buf2)); // What a wonderful dayCopy the code
As you can see, amazing, Chinese transcoding and no separate decode, wonderful separately from the two words is the decoding, the StringDecoder know after get encoding utf8 wide character is 3 bytes of storage in the form of so decoding only 9 for the first time, the remaining a reserved internally, the second combination to decode.
This is not the only thing about buffer, buffer is also very common in our development process, otherwise it would not be included in the global, this thing is very important, still need to continue to learn and strengthen, thank you.
PS: As for the use of TMUX, I think it is convenient to use a piece of article to amway, haha.