This article mainly introduces Node. details about how to use Buffer encoding and decoding of binary data in js. Buffer supports ascii, utf8, ucs2, base64, and Other encoding formats. If you need it, refer to JavaScript, however, because it was originally designed to process HTML documents, it is not very good at processing binary data. JavaScript has no byte type, no structured type (structured types), or even no byte array, only numbers and strings. (Original: JavaScript doesn't have a byte type-it just has numbers-or structured types, or http://skylitecellars.com/even byte arrays: It just has strings .)
Because Node is based on JavaScript, it can naturally process text protocols like HTTP, but you can also use it to interact with databases, process images or file uploads, etc. As you can imagine, how difficult it is to use strings to do these tasks. Earlier in the year, Node encoded byte into text characters to process binary data. However, this method was proved to be not feasible, which wastes resources, is slow, and not flexible, and is difficult to maintain.
Node has a binary Buffer to implement Buffer. This pseudo class provides a series of APIS for processing binary data, which simplifies the tasks for processing binary data. The buffer length is determined by the length of the byte data, and you can randomly set and obtain the byte data in the buffer.
Note: The Buffer class has a special place. The memory occupied by the byte data in the Buffer is not allocated in the memory of the JavaScrp.
On the It VM memory stack, that is to say, these objects will not be processed by the JavaScript garbage collection algorithm. Instead, they will be replaced by a permanent memory address that will not be modified, this also avoids CPU waste caused by memory replication of buffered content.
Create a buffer
You can create a buffer with a UTF-8 string like this:
var buf = new Buffer(‘Hello World!');
You can also use a specified encoded string to create a buffer:
var buf = new Buffer('8b76fde713ce', 'base64');
Acceptable character codes and identifiers are as follows:
1. ascii -- ASCI, applicable only to the ASCII character set.
2. utf8 -- UTF-8, which applies to any character set in the Unicode Character Set, has become the preferred encoding for the Web world and is also the default encoding type for Node.
3. base64 -- Base64. This encoding is based on 64 printable ASCII characters to represent binary data. Base64 is usually used to embed binary data that can be converted into strings in character documents, when necessary, it can be completely and lossless converted back to the original binary format.
If there is no data to initialize the buffer, you can use the specified capacity to create an empty Buffer:
Var buf = new Buffer (1024); // create a 1024-byte Buffer.
Obtain and set buffered data
After creating or receiving a buffer object, you may want to view or modify its content. You can use the [] operator to access a buffer byte:
Var buf = new Buffer ('My buffer content'); // 10th bytes of console. log (buf [10]) in the access Buffer; //-> 99
Note: When you (using the buffer capacity) Create an initialized buffer, note that the buffered data is not initialized to 0, but random data.
Var buf = new Buffer (1024); console. log (buf [100]); //-> 5 (a random value)
You can modify the data at any location in the buffer as follows:
Buf [99] = 125; // set the value of 100th bytes to 125
Note: In some cases, some buffer operations do not produce errors, such:
1. The maximum byte value in the buffer is 255. If a byte is given a number greater than 256, It is modeled by 256 and then the result is assigned to the byte.
2. If the value of a buffer byte is 256, the actual value will be 0)
3. If a floating point value is used to assign a value to a byte in the buffer, for example, 100.7, the actual value will be the integer part of the floating point value -- 100
4. If you try to assign a value to a location that exceeds the buffer capacity, the value assignment operation will fail and the buffer will not be modified.
You can use the length attribute to obtain the buffer length:
var buf = new Buffer(100);console.log(buf.length); // -> 100
You can also use the buffer length to iterate the buffer content to read or set each byte:
var buf = new Buffer(100);for(var i = 0; i < buf.length; i++) { buf[i] = i;}
The above Code creates a buffer containing 100 bytes and sets each byte in the buffer from 0 to 99.
Split and buffer data
Once a buffer is created or received, you may need to extract a portion of the buffered data. You can specify the starting position to split the existing buffer and create another small buffer:
var buffer = new Buffer("this is the content of my buffer");var smallerBuffer = buffer.slice(8, 19);console.log(smallerBuffer.toString()); // -> "the content"
Note: When splitting a buffer, no new memory is allocated or copied. The new buffer uses the parent buffer memory, which is only used by the parent buffer to buffer certain data segments (specified by the starting position). This section contains several meanings.
First, if your program modifies the content of the parent buffer, these modifications will also affect the related sub-buffer, because the parent buffer and sub-buffer are different JavaScript objects, therefore, it is easy to ignore this issue and cause some potential bugs.
Second, when you use this method to create a small sub-buffer from the parent buffer, the parent buffer object will still be retained after the operation is completed and will not be reclaimed. If you do not pay attention to it, it is easy to cause memory leakage.
Note: If you are worried about memory leakage, you can use the copy method to replace slice operations. Next we will introduce copy.
Copy buffered data
You can use copy to copy a part of the buffer to another buffer as follows:
var buffer1 = new Buffer("this is the content of my buffer");var buffer2 = new Buffer(11);var targetStart = 0;var sourceStart = 8;var sourceEnd = 19;buffer1.copy(buffer2, targetStart, sourceStart, sourceEnd);console.log(buffer2.toString()); // -> "the content"
The code above copies the 9th to 20 bytes of the source buffer to the starting position of the target buffer.
Decodes buffered data
Buffer data can be converted to a UTF-8 string like this:
var str = buf.toString();
You can also specify the encoding type to decode the buffered data into any encoding type. For example, if you want to decode a buffer into a base64 string, you can do this:
var b64Str = buf.toString("base64");
Using the toString function, you can also transcode A UTF-8 string to a base64 string:
var utf8String = 'my string';var buf = new Buffer(utf8String);var base64String = buf.toString('base64')
Summary
Sometimes, you have to deal with binary data, but native JavaScript does not have a clear way to do this, so Node provides a Buffer class that encapsulates some operations for contiguous memory blocks. You can split or copy memory data between two buffers.
You can also convert a buffer into a encoded string or, in turn, convert a string into a buffer to access or process each bit.
For more details about how to use Buffer encoding and decoding of binary data in Node. js, please follow the PHP Chinese network!