node. JS Cache

Source: Internet
Author: User
Tags base64 tojson

node. js buffer (buffer)

The JavaScript language itself has only a string data type and no binary data type.

However, when processing a TCP stream or file stream, you must use the binary data. Therefore, in node. js, a buffer class is defined that is used to create a buffer that is dedicated to storing the binary data.

In node. js, the Buffer class is the core library that is published with the Node kernel. The buffer library provides a way for node. JS to store the raw data, allowing node. js to process the binary data, possibly using the buffer library whenever data moved in the I/O operation needs to be handled 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 piece of raw memory outside of the V8 heap memory.

Buffer and character encoding

A Buffer instance is typically used to represent a sequence of encoded characters, such as UTF-8, UCS2, Base64, or hexadecimal encoded data. By using an explicit character encoding, you can convert a Buffer instance to a normal JavaScript string.

Const BUF = buffer.from (' Runoob ', ' ASCII ');//Output 72756e6f6f62console.log (buf.tostring (' hex '));//Output CnVub29iconsole.log (buf.tostring (' base64 '));

  

The character encodings currently supported by node. JS include:

    • ASCII -supports only 7-bit ASCII data. This encoding is very fast if the setting is removed from the high.

    • UTF8 -multibyte-encoded Unicode characters. Many web pages and other document formats use UTF-8.

    • Utf16le -2 or 4 bytes, a small-endian encoded Unicode character. Support for proxy pairs (u+10000 to U+10FFFF).

    • Alias of UCS2 - utf16le .

    • Base64 -Base64 encoding.

    • latin1 -a way to encode Buffer into a byte-encoded string.

    • binary -the alias of the latin1 .

    • Hex -encodes each byte as a two hexadecimal character.

Convert Buffer to JSON object syntax

The function syntax for converting Node Buffer to a JSON object is formatted as follows:

BUF.  ToJSON()  

When a Buffer instance is serialized, Json.stringify () implicitly invokes the ToJSON ().

return value

Returns a JSON object.

Instance
Const BUF = Buffer.from ([0x1, 0x2, 0x3, 0x4, 0x5]); Const JSON = json.stringify (BUF);//output: {"type": "Buffer", "Data": [N/A, 4,5]}console.log (JSON); const COPY = Json.parse (JSON, (key, value) + {  return value && value.type = = = Buff Er '?    Buffer.from (value.data):    value;}); /output: <buffer 05>console.log (copy);

  

Execute the above code and the output is:

{"type": "Buffer", "Data": [1,2,3,4,5]}<buffer 05>

  

Buffer clipping

The Node buffer clipping syntax is as follows:

BUF.  Slice([start[,end]])      
Parameters

The parameters are described as follows:

    • Start -number, optional, default: 0

    • End -number, optional, default: Buffer.length

return value

Returns a new buffer that points to the same piece of memory as the old buffer, but cuts from the index start to end.

Instance
var buffer1 = buffer.from (' Runoob ');//shear buffer var buffer2 = Buffer1.slice (0,2); Console.log ("Buffer2 content:" + buffer2.to String ());

  

Execute the above code and the output is:

Buffer2 Content:ru

  

The clipping function returns the actual buffer or part of the original buffer, which operates in the same memory area as the original buffer.

Crop var buffer_origin = buffer.from (' Runoob '), var buffer_slice = Buffer_origin.slice (0,2); Console.log ("Buffer slice Content: "+buffer_slice.tostring ()"); Console.log ("Buffer origin content:" +buffer_origin.tostring ()); Buffer_ Slice.write ("Wirte"); Write buffer slice//before cropping with the original string change Console.log ("Buffer slice content:" +buffer_slice.tostring ()); Console.log ("Buffer Origin content: "+buffer_origin.tostring ()");

  

Output:

Buffer Slice Content:rubuffer origin Content:runoobbuffer slice Content:wibuffer origin Content:winoob

  

You can see that the write operation returned to the crop  is also written to the original . 

Excerpt from: http://www.runoob.com/nodejs/nodejs-buffer.html


node. JS Cache

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.