Node.js Buffer
The JavaScript language itself only has a string data type and no binary data type.
The Buffer class in Node.js is a core tool for handling binary data, providing efficient operations on binary data.
The Buffer class is particularly useful in scenarios such as file operations, network communication, and image processing.
Features:
- Binary data:
BufferThe object is a fixed-size array containing raw binary data. Each element occupies one byte (8 bits), soBufferit is suitable for processing binary data, such as file contents, network packets, etc. - Immutability: Although
Bufferthe content of the object can be modified after creation, its length is fixed and cannot be changed dynamically.
Buffer and Character Encodings
Buffer instances are generally used to represent sequences of encoded characters, such as UTF-8, UCS2, Base64, or hexadecimal-encoded data. By using explicit character encodings, conversion can be performed between Buffer instances and ordinary JavaScript strings.
Example
// Outputs 72756e6f6f62
console.log(buf.toString('hex'));
// Outputs cnVub29i
console.log(buf.toString('base64'));
The character encodings currently supported by Node.js include:
ascii- Only 7-bit ASCII data is supported. This encoding is very fast if the high bit is removed.
utf8- Unicode characters encoded as multibyte. Many web pages and other document formats use UTF-8.
utf16le- 2 or 4 bytes, little-endian encoded Unicode characters. Supports surrogate pairs (U+10000 to U+10FFFF).
ucs2 - utf16leAlias of .
base64- Base64 encoding.
latin1- A way toBufferencode into a one-byte encoded string.
binary - latin1Alias of .
hex- Encodes each byte into two hexadecimal characters.
Creating a Buffer Class
Buffer provides the following APIs to create a Buffer class:
- Buffer.alloc(size[, fill[, encoding]]):Creates a Buffer with a length of size bytes, equivalent to allocating size bytes of memory space, with each byte value being 0.
- Buffer.allocUnsafe(size):Creates a Buffer with a length of size bytes, but the Buffer may contain old data, which may affect the execution result, hence the name unsafe.
- Buffer.allocUnsafeSlow(size):Used to allocate a new Buffer instance of the given size, but without initializing it.
- Buffer.from(array):Returns a new Buffer instance initialized with the values of array (the elements of the passed array can only be numbers, otherwise they will be automatically overwritten by 0).
- Buffer.from(arrayBuffer[, byteOffset[, length]]):Returns a newly created Buffer that shares the same memory as the given ArrayBuffer.
- Buffer.from(buffer):Copies the data of the passed-in Buffer instance and returns a new Buffer instance.
- Buffer.from(string[, encoding]):Creates a Buffer from a string; an encoding can be specified, defaulting to UTF-8.
Example
const buf1 = Buffer.alloc(10);
// Creates a Buffer of length 10 filled with 0x1.
const buf2 = Buffer.alloc(10, 1);
// Creates a Buffer of length 10 that is uninitialized.
// This method is faster than calling Buffer.alloc(),
// but the returned Buffer instance may contain old data,
// so it needs to be rewritten using fill() or write().
const buf3 = Buffer.allocUnsafe(10);
// Creates a Buffer containing [0x1, 0x2, 0x3].
const buf4 = Buffer.from([1, 2, 3]);
// Creates a Buffer containing the UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].
const buf5 = Buffer.from('tést');
// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].
const buf6 = Buffer.from('tést', 'latin1');
Writing to Buffer
Syntax
The syntax for writing to a Node buffer is as follows:
buf.write(string[, offset[, length]][, encoding])
Parameters
The parameters are described as follows:
string- The string to be written to the buffer.
offset- The index value at which the buffer starts writing, defaulting to 0.
length- The number of bytes to write, defaulting to buffer.length.
encoding- The encoding to use. Defaults to 'utf8'.
Writes string to buf at the offset position according to the character encoding of encoding. The length parameter is the number of bytes to write. If buf does not have enough space to hold the entire string, only part of string will be written. Partially decoded characters will not be written.
Return Value
Returns the size actually written. If the buffer space is insufficient, only part of the string will be written.
Example
buf = Buffer.alloc(256);
len = buf.write("www.example.com");
console.log("写入字节数 : "+ len);
Execute the above code, and the output result is:
$node main.js 写入字节数 : 14
Reading Data from Buffer
Syntax
The syntax for reading Node buffer data is as follows:
buf.toString([encoding[, start[, end]]])
Parameters
The parameters are described as follows:
encoding- The encoding to use. Defaults to 'utf8'.
start- Specifies the index position to start reading from, defaulting to 0.
end- The end position, defaulting to the end of the buffer.
Return Value
Decodes buffer data and returns a string using the specified encoding.
Example
buf = Buffer.alloc(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)); //使用 'ascii' 编码, 并输出: abcde
console.log( buf.toString('utf8',0,5)); // 使用 'utf8' 编码, 并输出: abcde
console.log( buf.toString(undefined,0,5)); // 使用默认的 'utf8' 编码, 并输出: abcde
Execute the above code, and the output result is:
$ node main.js abcdefghijklmnopqrstuvwxyz abcde abcde abcde
Converting Buffer to JSON Object
Syntax
The function syntax format for converting a Node Buffer to a JSON object is as follows:
buf.toJSON()
When stringifying a Buffer instance,JSON.stringify()this method will be implicitly called.toJSON()。
Return Value
Returns a JSON object.
Example
const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
const json = JSON.stringify(buf);
// 输出: {"type":"Buffer","data":[1,2,3,4,5]}
console.log(json);
const copy = JSON.parse(json, (key, value) => {
return value && value.type === 'Buffer' ?
Buffer.from(value.data) :
value;
});
// 输出: <Buffer 01 02 03 04 05>
console.log(copy);
Execute the above code, and the output result is:
{"type":"Buffer","data":[1,2,3,4,5]}
<Buffer 01 02 03 04 05>
Buffer Concatenation
Syntax
The syntax for Node buffer concatenation is as follows:
Buffer.concat(list[, totalLength])
Parameters
The parameters are described as follows:
list- An array list of Buffer objects to be concatenated.
totalLength- Specifies the total length of the concatenated Buffer object.
Return Value
Returns a new Buffer object formed by concatenating multiple members.
Example
var buffer1 = Buffer.from(('Example'));
var buffer2 = Buffer.from(('www.example.com'));
var buffer3 = Buffer.concat([buffer1,buffer2]);
console.log("buffer3 内容: " + buffer3.toString());
Execute the above code, and the output result is:
buffer3 内容: Examplewww.example.com
Buffer Comparison
Syntax
The function syntax for Node Buffer comparison is as follows. This method was introduced in Node.js v0.12.2:
buf.compare(otherBuffer);
Parameters
The parameters are described as follows:
otherBuffer- withbufanother Buffer object compared against the object.
Return Value
Returns a number indicating whetherbufInotherBufferbefore, after, or the same.
Example
var buffer1 = Buffer.from('ABC');
var buffer2 = Buffer.from('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 + "之后");
}
Execute the above code, and the output result is:
ABC在ABCD之前
Copying Buffer
Syntax
The syntax for Node buffer copy is as follows:
buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]])
Parameters
The parameters are described below:
targetBuffer- The Buffer object to be copied.
targetStart- Number, optional, default: 0
sourceStart- Number, optional, default: 0
sourceEnd- Number, optional, default: buffer.length
Return Value
No return value.
Example
var buf1 = Buffer.from('abcdefghijkl');
var buf2 = Buffer.from('EXAMPLE');
//将 buf2 插入到 buf1 指定位置上
buf2.copy(buf1, 2);
console.log(buf1.toString());
Running the above code produces the following result:
abEXAMPLEijkl
Buffer Slice
The syntax for Node buffer slicing is as follows:
buf.slice([start[, end]])
Parameters
The parameters are described below:
start- Number, optional, default: 0
end- Number, optional, default: buffer.length
Return Value
Returns a new buffer that points to the same memory as the old buffer, but sliced from index start to end.
Example
var buffer1 = Buffer.from('example');
// 剪切缓冲区
var buffer2 = buffer1.slice(0,2);
console.log("buffer2 content: " + buffer2.toString());
Running the above code produces the following result:
buffer2 content: ru
Buffer Length
Syntax
The syntax for calculating Node buffer length is as follows:
buf.length;
Return Value
Returns the memory length occupied by the Buffer object.
Example
var buffer = Buffer.from('www.example.com');
// 缓冲区长度
console.log("buffer length: " + buffer.length);
Running the above code produces the following result:
buffer length: 14
Methods Reference Manual
The following lists the commonly used methods of the Node.js Buffer module (note that some methods are not available in older versions):
| No. | Method & Description |
|---|---|
| 1 | new Buffer(size) Allocates a new buffer of size bytes, where each byte is 8 bits. Note that size must be less than kMaxLength, otherwise a RangeError exception will be thrown.Deprecated: Use Buffer.alloc() instead (or Buffer.allocUnsafe()). |
| 2 | new Buffer(buffer) Copies the data from the buffer parameter to the Buffer instance.Deprecated: Use Buffer.from(buffer) instead. |
| 3 | new Buffer(str[, encoding]) Allocates a new buffer containing the given str string. The encoding defaults to 'utf8'.Deprecated: Use Buffer.from(string[, encoding]) instead. |
| 4 | buf.length Returns the number of bytes in this buffer. Note that this is not necessarily the size of the contents in the buffer. length is the amount of memory allocated for the buffer object and does not change as the contents of the buffer object change. |
| 5 | buf.write(string[, offset[, length]][, encoding]) Writes the string data to the buffer according to the offset parameter and the specified encoding. The offset defaults to 0, and the encoding defaults to utf8. The length is the byte size of the string to be written. Returns a number indicating how many 8-bit byte streams were written. If the buffer does not have enough space to fit the entire string, it will only write part of the string. length defaults to buffer.length - offset. This method will not write partial characters. |
| 6 | buf.writeUIntLE(value, offset, byteLength[, noAssert]) Writes value to the buffer, determined by offset and byteLength. Supports up to 48-bit unsigned integers, little-endian, for example: const buf = Buffer.allocUnsafe(6); buf.writeUIntLE(0x1234567890ab, 0, 6); // 输出: <Buffer ab 90 78 56 34 12> console.log(buf);When noAssert is true, the validity of value and offset is no longer checked. Default is false. |
| 7 | buf.writeUIntBE(value, offset, byteLength[, noAssert]) Writes value to the buffer, determined by offset and byteLength. Supports up to 48-bit unsigned integers, big-endian. When noAssert is true, the validity of value and offset is no longer checked. Default is false. const buf = Buffer.allocUnsafe(6); buf.writeUIntBE(0x1234567890ab, 0, 6); // 输出: <Buffer 12 34 56 78 90 ab> console.log(buf); |
| 8 | buf.writeIntLE(value, offset, byteLength[, noAssert]) Writes value to the buffer, determined by offset and byteLength. Supports up to 48-bit signed integers, little-endian. When noAssert is true, the validity of value and offset is no longer checked. Default is false. |
| 9 | buf.writeIntBE(value, offset, byteLength[, noAssert]) Writes value to the buffer, determined by offset and byteLength. Supports up to 48-bit signed integers, big-endian. When noAssert is true, the validity of value and offset is no longer checked. Default is false. |
| 10 | buf.readUIntLE(offset, byteLength[, noAssert]) Supports reading unsigned numbers of up to 48 bits, little-endian. When noAssert is true, offset is no longer checked to see if it exceeds the buffer length. Default is false. |
| 11 | buf.readUIntBE(offset, byteLength[, noAssert]) Supports reading unsigned numbers of up to 48 bits, big-endian. When noAssert is true, offset is no longer checked to see if it exceeds the buffer length. Default is false. |
| 12 | buf.readIntLE(offset, byteLength[, noAssert]) Supports reading signed numbers of up to 48 bits, little-endian. When noAssert is true, offset is no longer checked to see if it exceeds the buffer length. Default is false. |
| 13 | buf.readIntBE(offset, byteLength[, noAssert]) Supports reading signed numbers of up to 48 bits, big-endian. When noAssert is true, offset is no longer checked to see if it exceeds the buffer length. Default is false. |
| 14 | buf.toString([encoding[, start[, end]]]) Returns a decoded string according to the encoding parameter (default is 'utf8'). It also uses the passed parameters start (default 0) and end (default buffer.length) as the value range. |
| 15 | buf.toJSON() Converts a Buffer instance to a JSON object. |
| 16 | buf[index] Gets or sets the specified byte. The return value represents a byte, so the valid range of the return value is hexadecimal 0x00 to 0xFF, or decimal 0 to 255. |
| 17 | buf.equals(otherBuffer) Compares whether two buffers are equal, returning true if they are, otherwise false. |
| 18 | buf.compare(otherBuffer) Compares two Buffer objects and returns a number indicating whether buf is before, after, or the same as otherBuffer. |
| 19 | buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]]) Copies buffer; the source and target can be the same. targetStart target start offset and sourceStart source start offset both default to 0. sourceEnd source end offset defaults to the source length buffer.length. |
| 20 | buf.slice([start[, end]]) Slices a Buffer object, cropping the index based on start (default 0) and end (default buffer.length) offsets. Negative indexes are calculated from the end of the buffer. |
| 21 | buf.readUInt8(offset[, noAssert]) Reads an unsigned 8-bit integer at the specified offset. If the noAssert parameter is true, the offset parameter will not be validated. In that case, offset may go beyond the end of the buffer. Default is false. |
| 22 | buf.readUInt16LE(offset[, noAssert]) Reads an unsigned 16-bit integer at the specified offset using a special endian byte order format. If the noAssert parameter is true, the offset parameter will not be validated. This means offset may go beyond the end of the buffer. Default is false. |
| 23 | buf.readUInt16BE(offset[, noAssert]) Reads an unsigned 16-bit integer at the specified offset using a special endian byte order format, big-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means offset may go beyond the end of the buffer. Default is false. |
| 24 | buf.readUInt32LE(offset[, noAssert]) Reads an unsigned 32-bit integer at the specified offset using the specified endian byte order format, little-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means offset may go beyond the end of the buffer. Default is false. |
| 25 | buf.readUInt32BE(offset[, noAssert]) Reads an unsigned 32-bit integer at the specified offset using the specified endian byte order format, big-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means offset may go beyond the end of the buffer. Default is false. |
| 26 | buf.readInt8(offset[, noAssert]) Reads a signed 8-bit integer at the specified offset. If the noAssert parameter is true, the offset parameter will not be validated. This means offset may go beyond the end of the buffer. Default is false. |
| 27 | buf.readInt16LE(offset[, noAssert]) Reads a signed 16-bit integer at the specified offset using a special endian format, little-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means offset may go beyond the end of the buffer. Default is false. |
| 28 | buf.readInt16BE(offset[, noAssert]) Reads a signed 16-bit integer at the specified offset using a special endian format, big-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means offset may go beyond the end of the buffer. Default is false. |
| 29 | buf.readInt32LE(offset[, noAssert]) Reads a signed 32-bit integer from the buffer at the specified offset using the specified endian byte order, little-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means that offset may go beyond the end of the buffer. The default is false. |
| 30 | buf.readInt32BE(offset[, noAssert]) Reads a signed 32-bit integer from the buffer at the specified offset using the specified endian byte order, big-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means that offset may go beyond the end of the buffer. The default is false. |
| 31 | buf.readFloatLE(offset[, noAssert]) Reads a 32-bit float from the buffer at the specified offset using the specified endian byte order, little-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means that offset may go beyond the end of the buffer. The default is false. |
| 32 | buf.readFloatBE(offset[, noAssert]) Reads a 32-bit float from the buffer at the specified offset using the specified endian byte order, big-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means that offset may go beyond the end of the buffer. The default is false. |
| 33 | buf.readDoubleLE(offset[, noAssert]) Reads a 64-bit double-precision number from the buffer at the specified offset using the specified endian byte order, little-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means that offset may go beyond the end of the buffer. The default is false. |
| 34 | buf.readDoubleBE(offset[, noAssert]) Reads a 64-bit double-precision number from the buffer at the specified offset using the specified endian byte order, big-endian. If the noAssert parameter is true, the offset parameter will not be validated. This means that offset may go beyond the end of the buffer. The default is false. |
| 35 | buf.writeUInt8(value, offset[, noAssert]) Writes value to the buffer at the specified offset. Note: value must be a valid unsigned 8-bit integer. If the noAssert parameter is true, the offset parameter will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about this parameter, do not use it. The default is false. |
| 36 | buf.writeUInt16LE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: value must be a valid unsigned 16-bit integer, little-endian. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 37 | buf.writeUInt16BE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: value must be a valid unsigned 16-bit integer, big-endian. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 38 | buf.writeUInt32LE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format (LITTLE-ENDIAN: little-endian). Note: value must be a valid unsigned 32-bit integer, little-endian. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 39 | buf.writeUInt32BE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format (Big-Endian: big-endian). Note: value must be a valid signed 32-bit integer. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 40 | buf.writeInt8(value, offset[, noAssert]) |
| 41 | buf.writeInt16LE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: value must be a valid signed 16-bit integer. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 42 | buf.writeInt16BE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: value must be a valid signed 16-bit integer. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 43 | buf.writeInt32LE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: value must be a valid signed 32-bit integer. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 44 | buf.writeInt32BE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: value must be a valid signed 32-bit integer. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 45 | buf.writeFloatLE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: when value is not a 32-bit float type value, the result will be undefined. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 46 | buf.writeFloatBE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: when value is not a 32-bit float type value, the result will be undefined. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 47 | buf.writeDoubleLE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: value must be a valid 64-bit double type value. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 48 | buf.writeDoubleBE(value, offset[, noAssert]) Writes value to the buffer at the specified offset according to the specified endian format. Note: value must be a valid 64-bit double type value. If the noAssert parameter is true, the value and offset parameters will not be validated. This means that value may be too large, or offset may go beyond the end of the buffer, causing value to be discarded. Unless you are very sure about these parameters, try not to use them. The default is false. |
| 49 | buf.fill(value[, offset][, end]) Fills this buffer with the specified value. If offset (default 0) and end (default buffer.length) are not specified, the entire buffer will be filled. |