The buffer of node. js

Source: Internet
Author: User

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. So in node. js, a buffer class is defined that creates a buffer dedicated to binary data that allows node. js to process binary data and, whenever it needs to process data moved in I/O operations in node. JS, it is possible to use the buffer library, a Buffer is similar to an array of integers, but it corresponds to a piece of raw memory outside of the V8 heap memory.


Create a Buffer object (3 ways)

var New // Create a Buffer instance with a length of 10 bytes
var New // creates a Buffer instance from the given array
var New // using a string to create a Buffer instance, Utf-8 is the default encoding, with other encodings "ASCII", "UTF8", "Utf16le", "UCS2", "base64", and "hex". 

Write buffers (2 ways)

var New Buffer (buf.write); ("HelloWorld");
var New Buffer (+);  for (var i = 0; i <; i++= i + +);} Console.log (buf.tostring (///  output: abcdefghijklmnopqrstuvwxyz//default = UTF8 optional)

Returns a JSON object.

var New Buffer (' www.runoob.com '); var json =// output for [119, 119, 119, 111, 111, 98, 117, A. A, a.

Buffer consolidation

var New Buffer (' rookie tutorial '); var New Buffer (' www.runoob.com '); var buffer3 = Buffer.concat ([Buffer1,buffer2]); Console.log (// output is Buffer3 content: Rookie Tutorial Www.runoob.com

  

The buffer of node. js

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.