Buffer
In Node.js, Buffer
is a class for processing binary data. This is very useful in scenarios that require binary data operations, such as file systems, network communications, encryption, and image processing. The Buffer
class is implemented in the buffer
module of Node.js and can be used without additional installation or reference.
Concept of Buffer
Buffer is an array-like object used to represent a fixed-length sequence of bytes. It is essentially a memory space dedicated to processing binary data.
Features of Buffer
- Fixed length: After the
Buffer
object is created, its length is fixed and cannot be dynamically expanded or reduced. - High performance:
Buffer
has high performance and can directly operate memory, avoiding the overhead of memory copying. - Typed:
Buffer
objects are binary data, so bytes can be manipulated directly, and each element is 1 byte in size.
Create Buffer
You can create Buffer
objects in a variety of ways:
- Create by byte length
// Create a Buffer with a length of 10 bytes, each byte has a value of 0
const buf = Buffer.alloc(10); // The result is <Buffer 00 00 00 00 00 00 00 00 00 00>
Buffer.alloc
creates a new Buffer
of the specified size and initializes its contents to 0. This ensures that the data in the new Buffer
object is safe and does not contain any potentially sensitive information or junk data.
- Quick creation by byte length, but content is not guaranteed to be safe
// Create a Buffer with a length of 10 bytes, but its content is not guaranteed to be safe
const buf = Buffer.allocUnsafe(10);
Buffer.allocUnsafe
creates a new Buffer
of the specified size, but its content is not initialized. This method is faster because it does not initialize the memory, but the newly created Buffer
may contain old data, so-called "dirty" data. This data has been used by other processes or programs before and may contain sensitive information, so it needs to be used with caution.
- Create from an array
const buf = Buffer.from([1, 2, 3]); // Create a Buffer containing [1, 2, 3] data
- Create from a string
const buf = Buffer.from('Hello, World!', 'utf8'); // Create a Buffer containing a UTF-8 encoded string
Operate Buffer
Buffer provides a variety of methods to read, write and manipulate its content:
- Write data
const buf = Buffer.alloc(10);
buf.write('Hello');
- Read data
const buf = Buffer.from('Hello, World!', 'utf8');
console.log(buf.toString()); // Output 'Hello, World!'
The toString()
method can specify the encoding method, such as utf8
, base64
, etc. If it is not filled in, the utf8
encoding is used by default.
- Operate partial data
In Node.js, the Buffer
object is immutable, which means that the content of the Buffer
cannot be modified directly. However, you can use the subarray()
~~ or slice()
(the slice() method has been deprecated in v17.5.0)~~ method to create a new Buffer
object containing part of the original Buffer
.
const buf = Buffer.from('Hello, World!', 'utf8');
const subarray = buf.subarray(0, 5);
console.log(subarray.toString()); // Output 'Hello'
- Compare Buffers
const buf1 = Buffer.from('ABC');
const buf2 = Buffer.from('BCD');
console.log(buf1.compare(buf2)); // Output negative number, because buf1 is less than buf2
Buffer Application Scenarios
- File Operation: Read and write binary files.
- Network Communication: Process TCP or UDP packets.
- Data Conversion: Convert data from one encoding to another.
- Encryption and Decryption: Process binary data in encryption algorithms.
Example code
Here is an example of using Buffer
to read the contents of a file and convert it into a string (the fs module is used later here, please refer to the fs module documentation for specific usage):
const fs = require('fs');
fs.readFile('example.txt', (err, data) => {
if (err) throw err;
const buf = Buffer.from(data);
console.log(buf.toString()); // Output file contents
});
In this example, fs.readFile reads the contents of the file example.txt and returns a Buffer object, which is then converted to a string for output.
In short, Buffer is an important tool for processing binary data in Node.js, through which various binary data operations can be performed efficiently.