Node.js USES Buffer to encode and decode binary data

  • 2020-03-30 03:42:52
  • OfStack

JavaScript is good at handling strings, but because it was originally designed to handle HTML documents, it's not very good at handling binary data. JavaScript has no byte types, no structured types, or even byte arrays, just Numbers and strings. (the original: JavaScript doesn 't have a byte type - it just from the Numbers - or structured types, or even http://skylitecellars.com/ byte arrays: it just has strings.)

Because Node is javascript-based, it can naturally handle text protocols like HTTP, but you can also use it to interact with databases, handle image or file uploads, and you can imagine how difficult it would be to do that with just strings. Earlier, Node processed binary data by encoding byte into a text character, but this approach proved impractical, wasteful, slow, inflexible, and difficult to maintain.

Node implements Buffer with a binary Buffer, a pseudo-class that provides a series of apis for working with binary data, simplifying the task of working with binary data. The length of the buffer is determined by the length of the bytes, and you can randomly set and retrieve the bytes in the buffer.

Note: the Buffer class has a special place where the memory occupied by the bytes of data in the Buffer is not allocated in JavaScrp

On the It VM memory heap, that is, these objects are not processed by the JavaScript garbage collection algorithm, but instead are a permanent memory address that cannot be modified, which also avoids the CPU waste caused by memory replication of cached content.

Create a buffer

You can create buffers with a utf-8 string, like this:


var buf = new Buffer( ' Hello World!');

You can also create a buffer with a specified encoded string:

var buf = new Buffer('8b76fde713ce', 'base64');

Acceptable character encodings and identifiers are as follows:

1. ASCII -- ASCI, only applicable to ASCII character set.
2. Utf8 -- utf-8, this variable-width encoding for any character in the Unicode character set, has become the preferred encoding in the Web world and the default encoding type for Node.
3. Base64 -- base64, this encoding is based on 64 printable ASCII characters to represent binary data. Base64 is commonly used to embed binary data into a character document that can be converted to a string, and then converted back to the original binary format intact if needed.

If there is no data to initialize the buffer, an empty buffer can be created with the specified capacity:


var buf = new Buffer(1024); //Create a buffer of 1024 bytes

Gets and sets the cached data

After you have created or received a buffered object, you may want to view or modify its contents.


var buf = new Buffer('my buffer content');
//The 10th byte in the access buffer
console.log(buf[10]); // -> 99

Note: when you create an initialized buffer, it is important to note that the cached data is not initialized to 0, but is random data.


var buf = new Buffer(1024); console.log(buf[100]); //-> 5 (some random value)

You can modify the data anywhere in the buffer by:


buf[99] = 125; //Set the value of the 100th byte to 125

Note: in some cases, some buffering operations do not produce errors, such as:

1. The maximum number of bytes in the buffer is 255. If a byte is assigned a number greater than 256, it is modulated with 256 and the result is assigned to the byte.
2. If a byte of the buffer is assigned a value of 256, its actual value will be 0.
3. If you assign a byte in the buffer to a floating-point number, such as 100.7, the actual value will be the integer part of the floating-point number -- 100
4. If you try to assign a value to a location beyond the buffer capacity, the assignment will fail and the buffer will not be modified.

You can use the length property to get the length of the buffer:


var buf = new Buffer(100); console.log(buf.length); // -> 100

You can also use the buffer length to iterate over the contents of the buffer 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 new buffer of 100 bytes and sets each byte in the buffer from 0 to 99.

Shred the buffered data

Once a buffer is created or received, you may need to extract a portion of the cached data. You can slice the existing buffer by specifying the starting location to create another smaller 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 that no new memory is allocated or copied when a buffer is shred. The new buffer USES the memory of the parent buffer, which is simply a reference to the parent buffer (specified by the starting location). This passage contains several meanings.

First, if your program changes the contents of the parent buffer, these changes will also affect the relevant child buffer, because the parent buffer and child buffer are different JavaScript objects, so it is easy to ignore this problem and cause some potential bugs.

Second, when you create a smaller child buffer from the parent buffer in this way, the parent buffer object will still be retained after the operation is over and will not be garbage collected, which can easily cause memory leaks if you are not careful.

Note: if you are concerned about memory leaks as a result, you can use the copy method instead of slice, which is described below.

Copy buffered data

You can use copy to copy a portion of the buffer to another buffer like this:


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"

In the above code, the 9th to 20th bytes of the source buffer are copied to the starting position of the target buffer.

Decoded buffered data

The cached data can be converted to a utf-8 string like this:


var str = buf.toString();

You can also decode the buffered data into any encoding type by specifying the 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 into 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 doesn't have a clear way to do this, so Node provides a Buffer class that encapsulates operations on contiguous blocks of memory. You can shred or copy memory data between two buffers.

You can also convert a buffer to some encoded string, or vice versa, convert a string to a buffer to access or process each bit.


Related articles: