This is the 8th day of my participation in the First Challenge 2022. For details: First Challenge 2022.
preface
The JavaScript language itself only has string data types, not binary data types.
But when dealing with things like TCP streams or file streams, you must use binary data. Therefore, in Node.js, a Buffer class is defined to create a Buffer dedicated to binary data.
In Node.js, the Buffer class is the core library shipped with the Node kernel. The Buffer library gives Node.js a way to store raw data, allows Node.js to work with binary data, and is likely to be used whenever data needs to be moved during I/O operations in Node.js.
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 block of raw memory outside of V8 heap memory.
Create the Buffer class
The Node Buffer class can be created in a number of ways.
Method 1
Create a Buffer with a length of 10 bytes:
var buf = new Buffer(10);
Copy the code
Method 2
Create a Buffer instance from the given array:
var buf = new Buffer([10.20.30.40.50]);
Copy the code
Methods 3
Create a Buffer instance from a string:
var buf = new Buffer("bianchengsanmei"."utf-8");
Copy the code
Utf-8 is the default encoding, but it also supports “ASCII “,” UTF8 “, “UTF16LE “,” UCS2 “, “base64”, and “HEX”.
Write buffer
grammar
The syntax for writing to the Node buffer looks like this:
buf.write(string[, offset[, length]][, encoding])
Copy the code
parameter
The parameters are described as follows:
- String – A string written to the buffer.
- Offset – The index at which the buffer starts writing. The default value is 0.
- Length – Number of bytes to be written. The default is buffer.length
- Encoding – Specifies the encoding used. The default is ‘utf8’.
The return value
Returns the actual size of the write. If there is not enough buffer space, only part of the string is written. The instance
buf = new Buffer(256);
len = buf.write("bi");
len = buf.write("bianchengsanmei");
console.log("Bytes written:"+ len);
Copy the code
Execute the above code and the output is:
$node main.js Number of bytes written:15
Copy the code
Read data from the buffer
grammar
The syntax for reading Node buffer data looks like this:
buf.toString([encoding[,start[,end]]])
Copy the code
parameter
The parameters are described as follows:
-
Encoding – Specifies the encoding used. The default is ‘utf8’.
-
Start – Specifies the index position to start reading. Default is 0.
-
End – The end position, the default is the end of the buffer.
The return value
Decodes buffer data and returns a string using the specified encoding.
The instance
buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
buf[i] = i + 97;
}
console.log( buf.toString('ascii')); / / output: abcdefghijklmnopqrstuvwxyz
console.log( buf.toString('ascii'.0.5)); // output: abcde
console.log( buf.toString('utf8'.0.5)); // output: abcde
console.log( buf.toString(undefined.0.5)); // Encode with 'utf8' and print: abcde
Copy the code
Execute the above code and the output is:
$ node main.js
abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde
Copy the code
Convert Buffer to JSON objects
grammar
The syntax of the Node Buffer function to convert a Node Buffer to a JSON object is as follows:
buf.toJSON()
Copy the code
The return value
Returns a JSON object.
The instance
var buf = new Buffer('bianchengsanmei');
var json = buf.toJSON(buf);
console.log(json);
Copy the code
Execute the above code and the output is:
{ type: 'Buffer'.data: [ 119.119.119.46.119.51.99.115.99.104.111.111.108.46.99.110]}Copy the code
Buffer merge
grammar
The syntax for Node buffer merging looks like this:
Buffer.concat(list[, totalLength])
Copy the code
parameter
The parameters are described as follows:
- List – An array of Buffer objects used for merging.
- TotalLength – specifies the totalLength of the merged Buffer object.
The return value
Returns a new Buffer object whose members are merged.
The instance
var buffer1 = new Buffer(Programming samadhi);
var buffer2 = new Buffer('bi');
var buffer2 = new Buffer('bianchengsanmei');
var buffer3 = Buffer.concat([buffer1,buffer2]);
console.log("Buffer3 contents: + buffer3.toString());
Copy the code
Execute the above code and the output is:
Buffer3 Content: Programming Samadhi BianchengsanmeiCopy the code
Buffer comparison
grammar
The syntax of the Node Buffer comparison function is shown below, which was introduced in Node.js v0.12.2:
buf.compare(otherBuffer);
Copy the code
parameter
The parameters are described as follows:
- OtherBuffer – Another Buffer object compared to the BUF object.
The return value
Returns a number indicating that buF precedes, follows, or is the same as otherBuffer.
The instance
var buffer1 = new Buffer('ABC');
var buffer2 = new Buffer('ABCD');
var result = buffer1.compare(buffer2);
if(result < 0) {
console.log(buffer1 + "In" + buffer2 + "Before");
}else if(result == 0) {console.log(buffer1 + " 与 " + buffer2 + "The same");
}else {
console.log(buffer1 + "In" + buffer2 + "After");
}
Copy the code
Execute the above code and the output is:
ABC comes before ABCDCopy the code
Copy buffer
grammar
The Node buffer copy syntax looks like this:
buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
Copy the code
parameter
The parameters are described as follows:
- TargetBuffer – The Buffer object to copy.
- TargetStart – Number, optional, default: 0
- SourceStart – Number, optional, default: 0
- SourceEnd – Number, optional, default: buffer.length
The return value
No return value.
The instance
var buffer1 = new Buffer('ABC');
// Copy a buffer
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log("buffer2 content: " + buffer2.toString());
Copy the code
Execute the above code and the output is:
buffer2 content: ABC
Copy the code
Buffer clipping
The Node buffer clipping syntax looks like this:
buf.slice([start[, end]])
Copy the code
parameter
The parameters are described as follows:
- Start – Optional number. Default: 0
- End – Number, optional, default: buffer.length
The return value
Returns a new buffer that points to the same block of memory as the old buffer but is clipped from index start to end.
The instance
var buffer1 = new Buffer('youj');
// Cut the buffer
var buffer2 = buffer1.slice(0.2);
console.log("buffer2 content: " + buffer2.toString());
Copy the code
Execute the above code and the output is:
buffer2 content: yo
Copy the code
Buffer length
Syntax Node buffer length calculation syntax looks like this:
buf.length;
Copy the code
The return value
Returns the length of memory occupied by the Buffer object.
The instance
var buffer = new Buffer('bianchengsanmei');
// Buffer length
console.log("buffer length: " + buffer.length);
Copy the code
Execute the above code and the output is:
buffer length: 15
Copy the code
~
Thanks for reading!
~
Learn interesting knowledge, meet interesting friends, shape interesting soul!
Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!
You come, with expectations, I have ink to welcome! You return, no matter gain or loss, only to yu Yun give each other!
Knowledge and skills should be paid equal attention to, internal force and external power should be repaired simultaneously, theory and practice should grasp both hands, both hands should be hard!