Node. js development-Buffer usage

Source: Internet
Author: User
Tags tojson

Node. js development-Buffer usage

In Node. jsBufferClass, you must introduce it, because we are using Node. when js is used for server-side development, all http, tcp, udp, file io and other types of operations will use Buffer, and it is basically impossible to play without it.

What is Buffer?

The String object in JavaScript stores strings and is Unicode encoded.

Buffer represents a Buffer, which stores binary data and is a byte stream. This byte stream is transmitted over the network. When writing a file, it is also a written byte stream.

Encoding format

Strings are encoded, such as UTF-8. Buffer does not have the encoding format. The two can be converted to each other. The encoding format must be specified during conversion.

The http module we have used before (seeNode. js development-HTTP File ServerAndGetting started with Node. js Development -- HelloWorld Analysis), The prototype of the callback function required by the http. createServer method is:

function (req, res)

The first parameter of this callback, req, is of the http type. incomingMessage, whereas http. incomingMessage is a read-only stream that implements the Readable interface, stream. the data read by Readable (the listening data event can be processed) is the Buffer object, which is a byte stream. When we use it in a program, we often need to convert it to a String. In turn, res (Type http. serverResponse, Writable stream, implements the Writable Interface) there is a method setDefaultEncoding, used to set the stream encoding format. When writing data, it uses the specified encoding format to encode the data, and then send it to the client.

That is to say, the network transmits Buffer, and the program needs to process String, which can be converted between Buffer and String. The Buffer has the toString method. You can convert bytes into strings in the specified encoding format.

Also, the file system module (see Node. js development entry-Use http to access the external world or Node. js development-HTTP File Server), fs. createWriteStream and fs. both createReadStream methods have an optional parameter options, which can be used to specify defaultEncoding. The encoding format specified here is also used to convert between Buffer and String.

Currently, in Node. js, when the Buffer is converted to a string, the first parameter of the toString method is the encoding type. Common encoding formats are supported:

Utf8 is a multi-byte Unicode character. Most documents and webpages use this encoding format ascii and 8-bit encoding. One character occupies one byte utf16le, and the unicode Character utf16be in the small-end encoding, unicode ucs2 of the big-end encoding. Each character occupies two bytes of base64, and the Base-64 string encoding hex. Each byte is encoded as two hexadecimal characters.

If you are not sure whether the encoding format is correct, you can use the Buffer. isEncoding (encoding) method for testing.

When using the toString method of Buffer, if you do not specify the encoding format, utf8 is used by default for conversion. ToString prototype:

buf.toString([encoding][, start][, end])

The first parameter is the encoding format, the second is the start position (0 to buf. length-1), and the third is the end position (excluding the data at this index position ).

Create a Buffer instance

You can use the new operator to create a Buffer instance in four ways:

New Buffer (size): Creates a buffer new buffer (array) with a fixed Buffer size. Creates a buffer new Buffer (str [, encoding]) based on a byte array. creates a buffer based on a string and encoding format. When encoding is not specified, utf8 new Buffer (buffer) is used by default. A new buffer is created based on the buffer instance.

For example, the following code creates a Buffer instance:

var buf1 = new Buffer(256);var buf2 = new Buffer(Hello Buffer);var buf3 = new Buffer([0x65,0x66,0x67]);var buf4 = new Buffer(buf2);

Note that the Buffer allocated using the new Buffer (size) is not initialized. In that memory, it may be dirty. Try the following code:

var buf1 = new Buffer(256);buf1.write('abc');console.log(buf1's content: , buf1.toString());

The above code tries to use toString to convert the Buffer, but you will see a lot of garbled characters in the console ...... Modify it and fill it with the buf. fill () method:

var buf1 = new Buffer(256);buf1.fill(0);buf1.write('abc');console.log(buf1's content: , buf1.toString());

The string ends with "". After buf1.fill (0) is called, everything is fine.

Write Data to the buffer zone

We have used the buf. write method to write data to the buffer zone. The write prototype is as follows:

buf.write(string[, offset][, length][, encoding])

Buf. write is used to write a string to the buffer and return the actual number of bytes written. The parameter description is as follows:

String: the offset and buffer offset of the string object to be written. If this parameter is specified, the data is written from this position. If this parameter is not specified, the default value is 0 length. The number of bytes to be written is encoding, for example, the encoding format of a string. The default value is utf8.

There are many other methods for Buffer operations. For example, writeUInt8, writeUInt16LE, writeUInt16BE, writeUInt31LE, writeUInt32BE, writeInt8, writeInt16LE, and writeInt32LE.

LE-little endian, in the small byte order.
BE-big endian: The network byte order.

It should be noted that the writeX method does not automatically save the current location for you as the file does. If you do not specify the offset, it will always start from the 0 position to write ......

Others. Read the document ......

Read data from the buffer zone

Buf. toString is actually a way to read data. Of course, there are other things. For example, buf [index] can read byte according to subscript, buf. readIntXXX, buf. readUIntXXX ......

Buf. toJSON () can convert a Buffer object to JSON format. When you call the JSON. stringify Method for a Buffer object, buf. toJSON () will be called. For example:

var buf = new Buffer('test');var json = JSON.stringify(buf);console.log(json);// '{type:Buffer,data:[116,101,115,116]}'
Buffer Length

The size of a Buffer object is fixed during creation and cannot be changed after creation. Hey, this is easy to misunderstand, especially when you think the Buffer stores strings. Try the following code to help you understand this:

var buf1 = new Buffer(256);buf1.fill(0);buf1.write('abc');console.log(buf1's length - %d, not 3, buf1.length);buf1.write('abcdef');console.log(buf1's length - %d, not 6, buf1.length);

In addition, when you want to determine the length of the byte occupied by the String in the buffer, the length attribute of the String cannot be used, because String. length returns the length of the character. For UTF-8 encoded strings, one character may occupy multiple bytes. Therefore, the String length represented by String. length is inconsistent with the byte length. Note that Buffer. length returns the length of the Buffer in bytes, which is the length at the time of creation and does not change with the Buffer content.

To measure the length of the byte occupied by a string, you can use Buffer. byteLength (string [, encoding]) to measure the length of the byte occupied by a string in the specified encoding format.

Let's look at a simple example:

Var name = new String ('who is Zhang Sanfeng? '); Console. log ('name. length = % d', name. length); console. log ('bytelength = % d', Buffer. byteLength (name, 'utf8 '));
Buffer operations

Buffer also supports operations such as slicing, copying, splicing, and comparison.

Buf. slice ([start [, end]) can slice a Buffer Based on the start and end position (excluding the data corresponding to the end position) and return a new Buffer object, it is convenient for us to operate on a certain area of the buffer zone. However, it is worth noting that this slice references the original buffer, not the copy. Your modifications to the slice content actually modify the original buffer. This method returns a Buffer object representing the slice.

Buf. copy (targetBuffer [, targetStart] [, sourceStart] [, sourceEnd]) can copy the content of a specified region of a buffer to a specified region of another buffer. Similar to memcpy in C language. TargetStart specifies the starting offset of the target buffer, and sourceStart specifies the starting offset of the source buffer. Both are 0 by default. sourceEnd specifies the Ending position of the source buffer. The default value is the length of the source buffer. During actual replication, the length of the target buffer zone and the length of the region to be copied are compared.

Buffer has a class method, concat (list [, totalLength]), which can splice a series of buffers into one. The first parameter list is a buffer array, and the second is the total length of the buffer to be spliced. If you do not provide totalLength, concat will traverse the buffer in the list and calculate the total length, causing a slight performance loss. This method returns the spliced buffer.

Take a look at the sample code to demonstrate the usage of slicing, copying, and splicing:

var buf1 = new Buffer('1234');var buf2 = new Buffer('12567');var bufList = [buf1, buf2];var buf3 = Buffer.concat(bufList);console.log('buf3 - %s', buf3.toString());var buf4 = buf3.slice(3, 8);console.log('buf4 - %s', buf4.toString());var buf5 = new Buffer(5);buf3.copy(buf5, 0, 1);console.log('buf5 - %s', buf5.toString());

Buf. equals (otherBuffer) checks whether the current buffer is equal to another one. If it is equal, true is returned.

Buf. compare (otherBuffer) compares the size of the current buffer and the other buffer, returns 0 equal, returns less than-1, and returns 1 greater. See the following sample code:

var buf1 = new Buffer('1234');var buf2 = new Buffer('12567');var buf3 = new Buffer('1234');var buf4 = new Buffer('0123');console.log(' = ',;console.log(' = ',;console.log(' = ',;


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.