Node.js in the use of buffer encoding, decoding binary data detailed _node.js

Source: Internet
Author: User
Tags base64 garbage collection

JavaScript is good at handling strings, but because it was originally designed to work with HTML documents, it is not very good at processing binary data. JavaScript has no byte type, no structured type (structured types), and 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 by Te Arrays:it just has strings.)

Because node is based on JavaScript, it can naturally handle text protocols like HTTP, but you can also use it to interact with databases, process images or file uploads, and imagine how difficult it would be to just do it with strings. Earlier, node handled binary data by encoding the byte into text characters, but it was later proven to be impractical, wasteful, slow, inflexible, and difficult to maintain.

Node has a binary buffer implementation buffer, this pseudo class (Pseudo-class) provides a series of APIs for processing binary data, simplifying those tasks that need to process binary data. The length of the buffer is determined by the length of the byte data, and you can randomly set and get the byte data in the buffer.

Note: The buffer class has a special place in which the byte data in the buffer is occupied by memory that is not allocated in JAVASCRP

On the It VM memory heap, which means that these objects will not be processed by JavaScript's garbage collection algorithm, and replaced by a permanent memory address that will not be modified, which avoids the CPU waste caused by memory duplication of buffered content.

Create a buffer

You can create a buffer with a UTF-8 string, like this:

Copy Code code as follows:

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

You can also create a buffer with the specified encoded string:
Copy Code code as follows:

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

The acceptable character encoding and identification is as follows:

1.ascii--asci, applies only to the ASCII character set.
2.utf8--utf-8, this variable width encoding applies to any character in the Unicode character set, which has become the preferred encoding for the web world and the default encoding type for node.
3.base64--base64, this encoding represents binary data based on 64 printable ASCII characters, and Base64 is typically used to embed binary data that can be converted into strings in a character document, which can be converted back to its original binary format when needed.

If there is no data to initialize the buffer, you can create an empty buffer with the specified capacity size:

Copy Code code as follows:

var buf = new Buffer (1024); Create a 1024-byte buffer

Getting and setting buffered data

After you create or receive a buffered object, you may want to view or modify its contents, and you can access one of the buffered bytes via the [] operator:

Copy Code code as follows:

var buf = new buffer (' My buffer content ');
Accessing the 10th byte in the buffer
Console.log (buf[10]); -> 99

Note: When you create an initialized buffer (using buffer capacity size), it is important to note that the buffered data is not initialized to 0, but rather random data.

Copy Code code as follows:

var buf = new Buffer (1024);

Console.log (buf[100]); -> 5 (a random value)

You can modify the data in any position in the buffer like this:

Copy Code code as follows:

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 byte in the buffer is 255, and if a byte is given a number greater than 256, it will be modulo with 256, and the result will be assigned to the byte.
2. If you assign a byte of a buffer to a value of 256, its actual value will be 0 (the translator Note: In fact, repeat with the first one, 256%256=0)
3. If you use a floating-point number to assign a value to a byte in the buffer, such as 100.7, the actual value will be the integer portion of the floating-point number--100
4. If you try to assign a value to a location that exceeds the buffer capacity, the assignment will fail, and the buffer does not make any changes.

You can use the Length property to get the buffer lengths:

Copy Code code as follows:

var buf = new Buffer (100);

Console.log (buf.length); -> 100

You can also use the buffer length iteration buffer to read or set each byte:

Copy Code code as follows:

var buf = new Buffer (100);

for (var i = 0; i < buf.length; i++) {

Buf[i] = i;

}

The code above creates a 100-byte buffer and sets each byte in the buffer from 0 to 99.

Shard Buffer Data

Once you have created or received a buffer, you may need to extract part of the buffered data by splitting the existing buffer by specifying the starting position to create another smaller buffer:

Copy Code code as follows:

var buffer = new Buffer ("This are the content of my buffer");

var smallerbuffer = Buffer.slice (8, 19);

Console.log (Smallerbuffer.tostring ()); -> "The content"

Note that when a buffer is split and no new memory is allocated or replicated, the new buffer uses the memory of the parent buffer, which is simply a reference to the parent buffer for a segment of data (specified by the starting position). This passage contains several meanings.

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

Second, when you create a smaller child buffer from the parent buffer in this way, the parent buffer is retained at the end of the operation and is not garbage collected, which can easily lead to memory leaks if not noted.

Note: If you are concerned about a memory leak problem, you can use the Copy method instead of the slice operation, which will be described in copy below.

Copy buffered data

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

Copy Code code as follows:

var buffer1 = new Buffer ("This are 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, copying the 9th to 20th byte of the source buffer to the beginning of the target buffer.

Decoding buffered data

Buffered data can be converted to a UTF-8 string as follows:

Copy Code code as follows:

var str = buf.tostring ();

You can also decode buffered data into any encoded type of data by specifying the encoding type. For example, if you want to decode a buffer into a base64 string, you can do this:

Copy Code code as follows:

var b64str = buf.tostring ("base64");

With the ToString function, you can also turn a UTF-8 string into a base64 string:
Copy Code code as follows:

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 it, so node provides a buffer class that encapsulates operations for contiguous chunks of memory. You can slice or copy the memory data between two buffers.

You can also convert a buffer into a coded string, or, conversely, convert a string into a buffer to access or process each bit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.