[node. js] Buffer

Source: Internet
Author: User
Tags tojson

Summary

There is no binary type in JS, but when dealing with TCP or a file stream, you must use binary data, so node. JS defines a buffer type that is used to create a buffer that stores binary data specifically. 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, and it is possible to use 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 certificates, but she corresponds to a piece of raw memory outside of the V8 heap memory.

Create a buffer class

Buffer can be created in several ways.

// Way One var buf=New Buffer (ten); // mode two creates a buffer instance from the given array var buf2=New Buffer ([10,20,30,40,50]); // method Three creates a buffer instance from a string // Utf-8 is the default encoding method, and it also supports encoding: "ASCII", "UTF8", "Utf16le", "UCS2", "base64". Hex "var buf3=New buffer (" Hello Buffer wolrd "," utf-8 ");
Write buffers

Grammar

Buf.write (sttring[,offset[,length]][,encoding])

Parameter description

String: Writes a buffer.

Offset: The location index where the buffer begins writing, default is 0.

Length: The number of bytes written, the default is Buffer.length.

Encoding: The encoding used, the default is UTF8.

return value

Returns the actual write size. If the buffer space is insufficient, a partial string is written.

An example:

var buf=New Buffer (n); var len=buf.write ("Hello buffer World"); Console.log ("number of bytes written:" +len);

Perform

Reading data

Grammar

Buf.tostring ([Encoding[,start[,end]])

Parameter description

Encoding: Use encoding, default is UTF8.

Start: The position index at which to begin reading. Default is 0

End: The ending position index, which defaults to the end of the buffer.

return value

Decodes the buffer data and returns the string using the specified encoding.

An example

var buf=New Buffer (+);  for (var i = 0; I <26; i++) {    buf[i]=i+97;}; Console.log (buf.tostring ("ASCII")), Console.log (buf.tostring ("ASCII", 0,5)); Console.log ( Buf.tostring ("UTF8", 0,5)); Console.log (buf.tostring (Undefined,0,5));

Convert buffer to JSON object

Grammar

Buf.tojson ()

return value

Returns a JSON object.

An example

var buf=New buffer ("Hello Buffer World") ); var json=Buf.tojson (); Console.log (JSON);

Buffer Merge

Grammar

Buffer.concat (List[,totallength])

Parameter description

List: Buffer object data listing for merging.

Totallength: Specifies the total length of the merged buffer object.

return value

Returns a new buffer object with multiple members merged.

An example

var buf1=New buffer ("Hello Buffer"); var buf2=New Buffer ("World") ); var buf3=Buffer.concat ([buf1,buf2]); Console.log ("merged content:" +buf3.tostring ());

Perform

Buffer comparison

The comparison function for buffer was introduced in the node. JS v0.12.2 version, with the following syntax

Buf.compare (Otherbuffer)

Parameters

Otherbuffer: A different buffer object compared to the Buf object.

return value

Returns a number that identifies buf before or after Otherbuffer, or the same.

An example

var buf1=New Buffer ("ABC"); var buf2=New Buffer ("ABCD"); var result=Buf1.compare (BUF2); if (result<0) {    Console.log (buf1+ "before" +buf2+ ");} Else if (result==0) {    Console.log (buf1+ "and" +buf2+ ");} Else {    Console.log (buf1+ "after" +buf2+ ");}

Copy buffer

Grammar

Buf.copy (Targetbuffer[,targetstart[,sourcestart[,sourceend]])

Parameters

Targetbuffer: The buffer object to copy.

Targetstart: The starting position of the object to copy, default 0.

Sourcestart: Source object start position, default 0

Sourceend: Source object end Position, default 0

no return value.

An example

var buf1=New Buffer ("ABC"); var targetbuf=New Buffer (3); buf1.copy (TARGETBUF); Console.log (targetbuf.tostring ());

Buffer clipping

Grammar

Buf.slice ([start[, end]])

Parameters

Start: The clipping position begins.

End: the ending position.

return value

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

An example

var buf1=New Buffer ("Wolfy"); var buf2=buf1.slice (0,2); Console.log (buf2.tostring ());

Buffer length

Grammar

Buf.length

return value

Returns the memory length occupied by the buffer object.

An example
var buf1=New Buffer ("Wolfy"); var buf2=buf1.slice (0,2); Console.log (buf2.tostring ()); Console.log (buf2.length) ;

Method Reference Manual

Http://www.runoob.com/nodejs/nodejs-buffer.html

[node. js] Buffer

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.