![]() |
VOOZH | about |
Buffer in Node.js is a built-in object used to handle and manipulate raw binary data, especially when working with streams, files, or network operations.
• Represents raw memory allocation for binary data
• Used in low-level operations like file systems and networking
• Provides methods for reading and writing binary data
const buf = Buffer.alloc(10); // Allocates a buffer of 10 bytes.Buffer is a fixed-size memory structure in Node.js designed specifically for handling raw binary data efficiently.
• A Buffer represents a fixed memory location used to store binary data.
• Unlike arrays, Buffers are not resizable and only handle binary data.
• Arrays can store any data type, but Buffers store only binary values.
• Each integer in a Buffer represents one byte.
• console.log() can be used to print the Buffer instance.
Buffer methods provide built-in functions to create, manipulate, and manage binary data efficiently in Node.js.
Method | Description |
|---|---|
Buffer.alloc(size) | Creates a new buffer of specified size. |
Buffer.from(data) | Creates a buffer from given data. |
buffer.write(data) | Writes data to the buffer. |
buffer.toString() | Converts buffer data to a string. |
Buffer.isBuffer(obj) | Checks if the object is a buffer. |
buffer.length | Returns the length of the buffer in bytes. |
buffer.copy(targetBuffer) | Copies data from one buffer to another. |
buffer.slice(start, end) | Returns a portion of the buffer. |
Buffer.concat([buffers]) | Concatenates multiple buffers. |
Example: Basic Implementation of Node.js Buffers
Run the index.js file using the following command:
node index.jsOutput:
Happy Learning
true
100
ABC
Geek
Happy Learning With GFG