Node .js Buffer (Buffer)

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

However, binary data must be used when working with streams like TCP or file. So in the node .js, a Buffer class is defined that creates a cache that holds binary data specifically.

In node .js, the Buffer class is the core library that is released with the Node kernel. T he Buffer library .js a way for Node.js to store raw data, allowing Node.js to process binary data, making it possible to use the Buffer library whenever you need to process data moved in an I/O operation in Node.js T he raw data is stored in an instance of the Buffer class. A Buffer is similar to an integer array, but it corresponds to a piece of raw memory outside of V8 heap memory.


Create a Buffer class

The Node Buffer class can be created in a number of ways.

Method 1

Create a Buffer instance with a length of 10 bytes:

var buf = new Buffer(10);

Method 2

Create a Buffer instance from a given array:

var buf = new Buffer([10, 20, 30, 40, 50]);

Method 3

Create a Buffer instance with a string:

var buf = new Buffer("www.w3cschool.cn", "utf-8");

utf-8 is the default encoding method, and it also supports the following encodings: "ascii," "utf8," "utf16le," "ucs2," "base64" and "hex."


Write to the buffer

Grammar

The syntax for writing to the Node buffer is as follows:

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

Parameters

The parameters are described as follows:

  • String - A string written to the buffer.

  • offset - The index value at which the buffer starts writing, which defaults to 0.

  • length - The number of bytes written, which defaults to buffer.length

  • encoding - the code used. The default is 'utf8'.

Returns a value

Returns the size of the actual write. If there is not enough space for buffer, only part of the string is written.

Instance

buf = new Buffer(256);
len = buf.write("www.w3cschool.cn");

console.log("写入字节数 : "+  len);

The above code is executed and the output is:

$node main.js
写入字节数 : 16

Read the data from the buffer

Grammar

The syntax for reading Node buffer data is as follows:

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

Parameters

The parameters are described as follows:

  • encoding - the code used. The default is 'utf8'.

  • Start - Specifies the index location where the reading begins, which defaults to 0.

  • end - End position, which defaults to the end of the buffer.

Returns a value

Decode the buffer data and return the string with the specified encoding.

Instance

buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
  buf[i] = i + 97;
}

console.log( buf.toString('ascii'));       // 输出: abcdefghijklmnopqrstuvwxyz
console.log( buf.toString('ascii',0,5));   // 输出: abcde
console.log( buf.toString('utf8',0,5));    // 输出: abcde
console.log( buf.toString(undefined,0,5)); // 使用 'utf8' 编码, 并输出: abcde

The above code is executed and the output is:

$ node main.js
abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde

Convert Buffer to a JSON object

Grammar

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

buf.toJSON()

Returns a value

Returns the JSON object.

Instance

var buf = new Buffer('www.w3cschool.cn');
var json = buf.toJSON(buf);

console.log(json);

The above code is executed and the output is:

{ type: 'Buffer',
  data: [ 119, 119, 119, 46, 119, 51, 99, 115, 99, 104, 111, 111, 108, 46, 99, 110 ] }

Buffer merge

Grammar

The syntax for node buffer merge is as follows:

Buffer.concat(list[, totalLength])

Parameters

The parameters are described as follows:

  • List - An array of Buffer objects for consolidation.

  • TotalLength - Specifies the total length of the merged Buffalo object.

Returns a value

Returns a new Buffer object that is merged by multiple members.

Instance

var buffer1 = new Buffer('W3Cschool教程 ');
var buffer2 = new Buffer('www.w3cschool.cn');
var buffer3 = Buffer.concat([buffer1,buffer2]);
console.log("buffer3 内容: " + buffer3.toString());

The above code is executed and the output is:

buffer3 内容: W3Cschool教程 www.w3cschool.cn

Buffer comparison

Grammar

The function syntax of the Node Buffer comparison is as follows, and the method .js in node v0.12.2:

buf.compare(otherBuffer);

Parameters

The parameters are described as follows:

  • otherBuffer - another Buffer object compared to the buf object.

Returns a value

Returns a number that indicates that the buf is before or after otherBuffer, or the same.

Instance

var buffer1 = new Buffer('ABC');
var buffer2 = new Buffer('ABCD');
var result = buffer1.compare(buffer2);

if(result < 0) {
   console.log(buffer1 + " 在 " + buffer2 + "之前");
}else if(result == 0){
   console.log(buffer1 + " 与 " + buffer2 + "相同");
}else {
   console.log(buffer1 + " 在 " + buffer2 + "之后");
}

The above code is executed and the output is:

ABC在ABCD之前

Copy buffer

Grammar

The Node buffer copy syntax looks like this:

buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])

Parameters

The parameters are described as follows:

  • TargetBuffer - Buffer object to copy.

  • TargetStart - number, optional, default: 0

  • SourceStart - Digital, Optional, Default: 0

  • SourceEnd - Digital, Optional, Default: buffer.length

Returns a value

No value was returned.

Instance

var buffer1 = new Buffer('ABC');
// 拷贝一个缓冲区
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log("buffer2 content: " + buffer2.toString());

The above code is executed and the output is:

buffer2 content: ABC

Buffer cropping

The Node buffer crop syntax looks like this:

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

Parameters

The parameters are described as follows:

  • start - number, optional, default: 0

  • end - number, optional, default: buffer.length

Returns a value

Returns a new buffer that points to the same piece of memory as the old buffer, but is cut from the position where the index starts to end.

Instance

var buffer1 = new Buffer('youj');
// 剪切缓冲区
var buffer2 = buffer1.slice(0,2);
console.log("buffer2 content: " + buffer2.toString());

The above code is executed and the output is:

buffer2 content: yo

The length of the buffer

Grammar

The Node buffer length calculation syntax is as follows:

buf.length;

Returns a value

Returns the length of memory occupied by the Buffer object.

Instance

var buffer = new Buffer('www.w3cschool.cn');
//  缓冲区长度
console.log("buffer length: " + buffer.length);

The above code is executed and the output is:

buffer length: 16

Method reference manual

The following are .js methods commonly used by Node and the Buffer module (note that some methods were not available in older versions):

Serial number Methods and descriptions
1 new Buffer(size)
Allocate a new size size unit of 8-bit bytes of buffer. Note that size must be less than kMaxLength, otherwise the exception RangeError will be thrown.
2 new Buffer(buffer)
Copy data from the parameter buffer to the Buffer instance.
3 new Buffer(str[, encoding])
Assign a new buffer that contains the incoming str string. Encoding encoding defaults to 'utf8'.
4 buf.length
Returns the number of bytes for this buffer. N ote that this is not necessarily the size of the contents of the buffer. Length is the amount of memory allocated by the buffer object, and it does not change as the contents of the buffer object change.
5 buf.write(string[, offset[, length]][, encoding])
The parameter string data is written to the buffer based on the parameter offset offset and the specified encoding encoding method. T he offset offset default is 0, and encoding encoding is utf8 by default. T he length length is the bytes size of the string to be written. R eturns the number type, which indicates how many 8-bit byte streams were written. I f buffer does not have enough space to put the entire string, it will write only part of the string. L ength defaults to buffer.length - offset. This method does not write part of the character.
6 buf.writeUIntLE(value, offset, byteLength[, noAssert])
Write value into buffer, which is determined by offset and byteLength and supports 48-bit calculations such as:
var b = new Buffer(6);
b.writeUIntBE(0x1234567890ab, 0, 6);
// <Buffer 12 34 56 78 90 ab>
When the noAssert value is true, the validity of value and offset is no longer validated. The default is false.
7 buf.writeUIntBE(value, offset, byteLength[, noAssert])
Write value to buffer, which is determined by offset and byteLength and supports 48-bit computing. W hen the noAssert value is true, the validity of value and offset is no longer validated. The default is false.
8 buf.writeIntLE(value, offset, byteLength[, noAssert])
Write value to buffer, which is determined by offset and byteLength and supports 48-bit computing. W hen the noAssert value is true, the validity of value and offset is no longer validated. The default is false.
9 buf.writeIntBE(value, offset, byteLength[, noAssert])
Write value to buffer, which is determined by offset and byteLength and supports 48-bit computing. W hen the noAssert value is true, the validity of value and offset is no longer validated. The default is false.
10 buf.readUIntLE(offset, byteLength[, noAssert])
Supports reading numbers below 48 bits. When the noAssert value is true, offset no longer verifies that the buffer is longer than it is, the default is false.
11 buf.readUIntBE(offset, byteLength[, noAssert])
Supports reading numbers below 48 bits. When the noAssert value is true, offset no longer verifies that the buffer is longer than it is, the default is false.
12 buf.readIntLE(offset, byteLength[, noAssert])
Supports reading numbers below 48 bits. When the noAssert value is true, offset no longer verifies that the buffer is longer than it is, the default is false.
13 buf.readIntBE(offset, byteLength[, noAssert])
Supports reading numbers below 48 bits. When the noAssert value is true, offset no longer verifies that the buffer is longer than it is, the default is false.
14 buf.toString([encoding[, start[, end]]])
A decoded string type is returned based on the encoding parameter (the default is 'utf8'). The range of values is also taken based on the incoming parameters start (0 by default) and end (buffer.length by default).
15 buf.toJSON()
Convert the Buffer instance to a JSON object.
16 buf[index]
Gets or sets the specified byte. The return value represents a byte, so the legal range of the return value is 0x00 0xFF or heteth 0 to 255.
17 buf.equals(otherBuffer)
Compare whether the two buffers are equal, if true is returned, otherwise false is returned.
18 buf.compare(otherBuffer)
Compare the two Buffer objects and return a number that indicates that the buf is before otherBuffer, or after or the same.
19 buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
Buffer copy, source and destination can be the same. T argetStart target start offset and sourceStart source start offset default is 0. The sourceEnd source end offset defaults to the source's length buffer.length.
20 buf.slice([start[, end]])
Cut the Buffer object, offset and crop the index based on start (0 by default) and end (buffer.length by default). The negative index is calculated from the tail of the buffer.
21 buf.readUInt8(offset[, noAssert])
Read a signed 8-bit integer based on the specified offset. I f the parameter noAssert is true, the offset offset parameter will not be validated. I f this happens, offset may exceed the end of the buffer. The default is false.
22 buf.readUInt16LE(offset[, noAssert])
Depending on the specified offset, a signed 16-bit integer is read in a special endian byte order format. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
23 buf.readUInt16BE(offset[, noAssert])
Depending on the specified offset, a signed 16-bit integer is read in a special endian byte order format. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
24 buf.readUInt32LE(offset[, noAssert])
Depending on the specified offset, a signed 32-bit integer is read in the specified endian byte order format. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
25 buf.readUInt32BE(offset[, noAssert])
Depending on the specified offset, a signed 32-bit integer is read in the specified endian byte order format. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
26 buf.readInt8(offset[, noAssert])
Read an signed 8-bit integer based on the specified offset. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
27 buf.readInt16LE(offset[, noAssert])
Depending on the specified offset, read a signed 16-bit integer in a special endian format. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
28 buf.readInt16BE(offset[, noAssert])
Depending on the specified offset, read a signed 16-bit integer in a special endian format. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
29 buf.readInt32LE(offset[, noAssert])
Depending on the specified offset, read a signed 32-bit integer in the specified endian byte order format. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
30 buf.readInt32BE(offset[, noAssert])
Depending on the specified offset, read a signed 32-bit integer in the specified endian byte order format. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
31 buf.readFloatLE(offset[, noAssert])
Read a 32-bit float using the specified endian byte-order format based on the specified offset. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
32 buf.readFloatBE(offset[, noAssert])
Read a 32-bit float using the specified endian byte-order format based on the specified offset. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
33 buf.readDoubleLE(offset[, noAssert])
Read a 64-bit double in the specified endian byte order format based on the specified offset. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
34 buf.readDoubleBE(offset[, noAssert])
Read a 64-bit double in the specified endian byte order format based on the specified offset. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that offset may exceed the end of the buffer. The default is false.
35 buf.writeUInt8(value, offset[, noAssert])
Value is written to buffer based on the incoming offset offset. N ote: Value must be a legitimate signed 8-bit integer. I f the parameter noAssert is true, the offset offset parameter will not be validated. T his means that value may be too large, or offset may exceed the end of the buffer, causing value to be discarded. D on't use this parameter unless you're very sure about it. The default is false.
36 buf.writeUInt16LE(value, offset[, noAssert])
Value is written to buffer based on the incoming offset offset and the specified endian format. N ote: Value must be a legitimate signed 16-bit integer. I f the argument noAssert is true, the value and offset offset parameters will not be validated. T his means that the value may be too large, or offset may exceed the end of the buffer, causing the value to be discarded. T ry not to use this parameter unless you are very sure about it. The default is false.
37 buf.writeUInt16BE(value, offset[, noAssert])
Value is written to buffer based on the incoming offset offset and the specified endian format. N ote: Value must be a legitimate signed 16-bit integer. I f the argument noAssert is true, the value and offset offset parameters will not be validated. T his means that the value may be too large, or offset may exceed the end of the buffer, causing the value to be discarded. T ry not to use this parameter unless you are very sure about it. The default is false.
38 buf.writeUInt32LE(value, offset[, noAssert])
Value is written to the buffer based on the incoming offset offset and the specified endian format. N ote: Value must be a legitimate signed 32-bit integer. I f the argument noAssert is true, the value and offset offset parameters will not be validated. T his means that the value may be too large, or the offer may exceed the end of the buffer, causing value to be discarded. T ry not to use this parameter unless you are very sure about it. The default is false.
39 buf.writeUInt32BE(value, offset[, noAssert])
Value is written to the buffer based on the incoming offset offset and the specified endian format. N ote: Value must be a legitimate signed 32-bit integer. I f the argument noAssert is true, the value and offset offset parameters will not be validated. T his means that the value may be too large, or the offer may exceed the end of the buffer, causing value to be discarded. T ry not to use this parameter unless you are very sure about it. The default is false.
40 buf.writeInt8(value, offset[, noAssert])