Using the buffer class to process binary data
In the client-side JavaScript scripting code, there is no good support for binary data. The binary data must then be processed when the NODEJS needs to be processed like a TCP stream or a file stream. Therefore, in node. js, a buffer class is defined that is used to create a buffer that is dedicated to storing the binary data.
One: Create Buffer Object
In node. js, the buffer class is a global class that can be exploited in any module and does not need to load any modules for the use of that class. You can use the New keyword to create an instance object for the class. The buffer class can be used to construct functions in three ways, the first of which is as follows:
New Buffer (size)
The created Buffer object has a length property with the value of the buffer size. As follows:
We can now also use the Fill method of the buffer object to initialize everything in the buffer, as shown here:
Buf.fill (Value,[offset],[end]);
In the Fill method of the Buffer object, you can use three parameters, the first parameter is the parameter that must be specified, the value of the parameter is the value that needs to be written, the second argument is an optional argument to the third argument, where the second parameter specifies that the specified value is written from the first byte, and the default value is 0. That is, it is written from the beginning of the buffer, and the third parameter specifies that the value is written to the first byte, and the default value is the size of the buffer object, which is written to the bottom of the buffer.
Now we want to start writing 1 from the 10th byte of the buffer object's buffer to the bottom of the buffer, as shown here:
We can now fill in 2 at the 20th to 30th byte of the buffer (starting with the 20th byte, without the 30th byte), as shown below:
The second form of the buffer class constructor is to use an array directly to initialize the buffer, as shown in the following code:
New Buffer (Array)
The demo is as follows:
The third form of the buffer class constructor is to use a string directly to initialize the buffer, the code is as follows:
New Buffer (str,[encoding]);
The first parameter is the parameter that must be specified, the value of the parameter is the string used to initialize the swap, and the second parameter value is a string that specifies the text encoding format, the default value is Utf-8
As shown below:
In node. js, the encoding and decoding of string input and output will be performed automatically, using UTF-8 encoding by default, as shown in the following table:
| Coding |
Description |
| Ascii |
ASCLL string |
| Utf8 |
UTF-8 string |
| Utf16le |
Utf-16le string |
| Ucs2 |
UCS2 string |
| Base64 |
String after BASE64 encoding |
| Binary |
Binary data (deprecated) |
| Hex |
A string represented by a 16 binary value |
When using a string parameter to create a buffer object and initialize the buffer, the data in the buffer will be different if you use a separate encoding format, as shown below:
Two: The length of the string and the length of the buffer
In node. js, the length of a string is not the same as the length of the buffer created by the string, because the length of the string is calculated as a unit of text, while the length of the buffer is calculated as a unit of bytes.
For example, for the "I Love Programming" string, the Length property value of the string object is not the same as the value of the length property of the buffer object created by the string. Because the length property value of a String object gets the number of words, and the value of the length property of the buffer object is the size of the buffer, which is the byte in the buffer.
Alternatively, you can use the 0-starting ordinal to remove data from a string object or buffer. However, when you get the data, the string object is in text as a unit, and the buffer object is in bytes as a unit. For example, for a str variable referencing a string object, Str[2] Gets the third literal, and for a BUF object referencing a buffer object, buf[2] Gets the value of the third byte in the buffer that is converted to an integer. As follows:
The buffer object can be modified. You can modify the data at one of these bytes by ordinal. As follows:
The buffer object also has a slice method for fetching data at the specified location, using the same method as the slice method of the string object.
Note: because the slice method of the buffer object does not replicate the data in the buffer, it shares the memory area with that data, so if you modify the data that is taken with the slice method, the data stored in the buffer will be modified. The following shows:
Three: Buffer the conversion between an object and a string object;
1. the toString method of the Buffer object
You can use the ToString method of the buffer object to convert the data saved in the buffer object to a string, as follows:
Buf.tostring ([encoding],[start],[end]);
In the ToString method of the Buffer object, you can use three optional parameters, the first parameter specifies the text encoding format saved in the Buffer object, the default parameter value is UTF8, and the second and third specify the starting and ending positions of the transformed data, in bytes. The ToString method returns the converted string.
Four: Buffer object that Write Method
If you want to use a string as a binary data, simply create the buffer object as a parameter to the constructor of the buffer class. But sometimes we need to write a string to a buffer object that has already been created, so we can use the Write method of the buffer object, as shown in the following code:
Buf.write (string,[offset],[length],[encoding]);
In the write method of the buffer object, you can use four parameters, the first parameter is the one that must be specified, the second three parameters are optional, and the first parameter specifies the string to write. The second parameter, offset, and the third argument, length, are used to specify the write location where the string is converted to byte data. Byte data is written from the beginning of the 1+offset byte to offset+length bytes (columns such as offset is 3,length to 8, the write position is from the 4th byte to the 11th byte, including the 4th byte and 11th bytes). The fourth parameter specifies the encoding format to use when writing the string, and the default is the UTF8 format.
The following code shows:
Five: Buffer Objects and JSON Mutual conversions between objects
In node. js, you can use the json.stringfy method to convert the data saved in a buffer object to a string, or you can use the json.parse method to restore a converted string to an array.
Six: Replicate cached data.
When you need to copy the binary data stored in a buffer object into another buffer object, you can use the Copy method of the buffer object, as follows:
buf.copy (Targetbuffer,[targetstart],[sourcestart],[sourceend]);
In the copy method of the buffer object, four parameters are used, the first parameter is the one that must be specified, and the remaining three parameters are optional. The first parameter specifies the target buffer object for replication. The second parameter specifies that the target buffer object writes data from the first byte, the parameter value is an integer value that is less than the target's buffer object length, and the default value is 0 (the data is written from the beginning). The third parameter specifies the starting position when fetching data from the replication source buffer object, with the default value of 0, starting with the first byte in the replication source buffer object, and the fourth parameter specifying the end position when fetching data from the replication source buffer object. The default value is the length of the copied source buffer object, which is always obtained to copy all remaining data from the source buffer object.
For example, copy the contents of the buffer object in a to the buffer object in B, where the copied target starts at the 11th byte of the buffer object at B (the 11th byte begins writing). As shown below:
Seven: Buffer Class methods for classes
- Isbuffer method
The Isbuffer method is used to determine whether an object is a buffer object, using the following method:
buffer.isbuffer (obj)
In the Isbuffer method, a parameter is used to specify the object that needs to be judged, and if the object is a buffer object, the method returns True, otherwise false. The demo is as follows:
2. ByteLength method;
You can use the ByteLength method to calculate the number of bytes for a specified string, using the following method:
Buffer.bytelength (string,[encoding]);
In the ByteLength method, two parameters are used, the first parameter is the argument that must be entered, the string that specifies the number of bytes to be computed, and the second parameter is an optional parameter that specifies what encoding is used to calculate the number of bytes. The default value is UTF8.
The following shows:
3. Concat method.
The Concat method is used to create a combination of several buffer objects as a new buffer object, using the following method:
Buffer.concat (List,[totallength])
In the Concat method, using two parameters, the first parameter is the parameter that must be specified, the parameter value is an array that holds multiple buffer objects, and the Concat method creates all the buffer object links in it as a buffer object, and the second argument is an optional parameter. Used to specify the length of the buffer object being created, when omitted, the buffer object created is the aggregate value of the length of all the buffer objects in the first parameter array.
If a parameter value is an empty array or the second parameter value equals 0, then the Concat method returns a buffer object with a length of 0.
If there is only one buffer object in the first parameter value array, the Concat method returns the buffer object directly.
If the first parameter value array has more than one buffer object, the Concat method returns the buffer object that was created.
The following shows:
4. Isencoding method.
The Isencoding method is used to detect whether a string is a valid encoded format string, as shown in the following method:
buffer.isencoding (encoding);
In the Isencoding method, you use a parameter that specifies the string that needs to be instrumented. If the string is a valid encoded format string, the method returns True if the string is not a valid encoded format string, and the method returns false. The following shows:
nodejs-processing binary data using the buffer class