An object of buffer class in Node.js used to represent a fixed-length sequence of bytes. It stores raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. The Buffer class in Node.js is a subclass of JavaScript”s Uint8Array class. Although an instance Buffer class is a global object, it is recommended that the class is explicitly referenced via an import or require statement.
In earlier versions of Node.js, a buffer object is declared with new operator −
var buf = new Buffer(10);
A buffer object can also be created from a given array −
var buf = new Buffer([10, 20, 30, 40, 50]);
or from a given string −
var buf = new Buffer("Simply Easy Learning", "utf-8");
However, the use of new keyword has now been deprecated. You need to use the following static methods to create a Buffer object −
// Creates a zero-filled Buffer of length 10.
const buf1 = Buffer.alloc(10);
// Creates an uninitialized buffer of length 10.
const buf2 = Buffer.allocUnsafe(10);
// Creates a Buffer containing array
const buf3 = Buffer.from([1, 2, 3]);
// creates a buffer from string
const buf4 = Buffer.from(''hello world'', ''utf8'');
Static methods
alloc()
Allocates a new Buffer of specified size in bytes.
Buffer.alloc(size[, fill[, encoding]])
Parameters
size − The desired length of the new Buffer.
fill − A value to pre-fill the new Buffer with. Default: 0.
encoding − If fill is a string, this is its encoding. Default: ”utf8”.
Example
const buf = Buffer.alloc(5);
console.log(buf);
Output
<Buffer 00 00 00 00 00>
allocUnsafe()
Creates an uninitialized buffer of specified size in bytes
Allocates a new Buffer using an array of bytes in the range 0 – 255.
Example
// Creates a new Buffer containing the UTF-8 bytes of the string ''buffer''.
const buf1 = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
console.log(buf1);
// Creates a new Buffer from integer array.
const buf2 = Buffer.from([10, 20, 30, 40, 50]);
console.log(buf2);
encoding <string> The encoding of string. Default: ”utf8”. The encoding parameter identifies the character encoding to be used when converting string into bytes.
targetStart − The offset within target at which to begin comparison. Default: 0.
targetEnd − The offset within target at which to end comparison (not inclusive). Default: target.length.
sourceStart − The offset within source buffer at which to begin comparison. Default: 0.
sourceEnd − The offset within source buffer at which to end comparison (not inclusive). Default: buf.length.
The function returns a number indicating whether the first buffer comes before, after, or is the same as the other buffer object in sort order.
0 is returned if second is the same as first
1 is returned if second buffer should come before the first when sorted.
-1 is returned if second buffer should come after the first when sorted.
Example
var buffer1 = Buffer.from(''BALL'');
var buffer2 = Buffer.from(''BAT'');
var result = buffer1.compare(buffer2);
if(result < 0) {
console.log(buffer1 +" comes before " + buffer2);
} else if(result === 0) {
console.log(buffer1 +" is same as " + buffer2);
} else {
console.log(buffer1 +" comes after " + buffer2);
}
Output
BALL comes before BAT
In the above example, change the two Buffer objects to −
var buffer1 = Buffer.from(''CURE'');
var buffer2 = Buffer.from(''CORE'');
Output
CURE comes after CORE
copy()
Copies data from a region of this buffer to a region in the target buffer even if the target memory region overlaps with the source.
targetStart − The offset within target at which to begin writing. Default: 0.
sourceStart − The offset within buf from which to begin copying. Default: 0.
sourceEnd − The offset within buf at which to stop copying (not inclusive). Default: buf.length.
Example
var buffer1 = Buffer.from(''Hello World'');
var buffer2 = Buffer.allocUnsafe(buffer1.length);
var result = buffer1.compare(buffer2);
buffer1.copy(buffer2);
console.log("buffer2 content: " + buffer2.toString());
Output
buffer2 content: Hello World
entries()
returns an iterator of [index, byte] pairs from the contents of buffer object.
Example
var buf = Buffer.from(''Hello World'');
for (const pair of buf.entries()) {
console.log(pair);
}
Returns a new buffer, offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.
Example
var buffer1 = Buffer.from(''TutorialsPoint'');
//slicing a buffer
var buffer2 = buffer1.slice(0,9);
console.log("buffer2 content: " + buffer2.toString());
Output
buffer2 content: Tutorials
write()
Writes string to a buffer at offset according to the specified encoding.
write(string[, offset[, length]][, encoding])
Parameters
string − String to write to buf.
offset − Number of bytes to skip before starting to write string. Default: 0.
length − Maximum number of bytes to write (written bytes will not exceed buf.length – offset). Default: buf.length – offset.
encoding − The character encoding of string. Default: ”utf8”.
Allocates a new buffer of size octets. Note that the size must be no more than kMaxLength. Otherwise, a RangeError will be thrown here.
2
new Buffer(buffer)
Copies the passed buffer data onto a new Buffer instance.
3
new Buffer(str[, encoding])
Allocates a new buffer containing the given str. encoding defaults to ”utf8”.
4
buf.length
Returns the size of the buffer in bytes. Note that this is not necessarily the size of the contents. length refers to the amount of memory allocated for the buffer object. It does not change when the contents of the buffer are changed.
5
buf.write(string[, offset][, length][, encoding])
Writes a string to the buffer at offset using the given encoding. offset defaults to 0, encoding defaults to ”utf8”. length is the number of bytes to write. Returns the number of octets written.
Writes a value to the buffer at the specified offset and byteLength. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of value and offset. Defaults to false.
Writes a value to the buffer at the specified offset and byteLength. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of value and offset. Defaults to false.
Writes a value to the buffer at the specified offset and byteLength. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of value and offset. Defaults to false.
Writes a value to the buffer at the specified offset and byteLength. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of value and offset. Defaults to false.
10
buf.readUIntLE(offset, byteLength[, noAssert])
A generalized version of all numeric read methods. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.
11
buf.readUIntBE(offset, byteLength[, noAssert])
A generalized version of all numeric read methods. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.
12
buf.readIntLE(offset, byteLength[, noAssert])
A generalized version of all numeric read methods. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.
13
buf.readIntBE(offset, byteLength[, noAssert])
A generalized version of all numeric read methods. Supports up to 48 bits of accuracy. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.
14
buf.toString([encoding][, start][, end])
Decodes and returns a string from buffer data encoded using the specified character set encoding.
15
buf.toJSON()
Returns a JSON-representation of the Buffer instance. JSON.stringify implicitly calls this function when stringifying a Buffer instance.
16
buf[index]
Get and set the octet at index. The values refer to individual bytes, so the legal range is between 0x00 and 0xFF hex or 0 and 255.
17
buf.equals(otherBuffer)
Returns a boolean if this buffer and otherBuffer have the same bytes.
18
buf.compare(otherBuffer)
Returns a number indicating whether this buffer comes before or after or is the same as the otherBuffer in sort order.
Copies data from a region of this buffer to a region in the target buffer even if the target memory region overlaps with the source. If undefined, the targetStart and sourceStart parameters default to 0, while sourceEnd defaults to buffer.length.
20
buf.slice([start][, end])
Returns a new buffer which references the same memory as the old, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.
21
buf.readUInt8(offset[, noAssert])
Reads an unsigned 8 bit integer from the buffer at the specified offset. Set noAssert to true to skip validation of offset. It means that the offset may be beyond the end of the buffer. Defaults to false.
22
buf.readUInt16LE(offset[, noAssert])
Reads an unsigned 16-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
23
buf.readUInt16BE(offset[, noAssert])
Reads an unsigned 16-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
24
buf.readUInt32LE(offset[, noAssert])
Reads an unsigned 32-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
25
buf.readUInt32BE(offset[, noAssert])
Reads an unsigned 32-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
26
buf.readInt8(offset[, noAssert])
Reads a signed 8-bit integer from the buffer at the specified offset. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
27
buf.readInt16LE(offset[, noAssert])
Reads a signed 16-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
28
buf.readInt16BE(offset[, noAssert])
Reads a signed 16-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
29
buf.readInt32LE(offset[, noAssert])
Reads a signed 32-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
30
buf.readInt32BE(offset[, noAssert])
Reads a signed 32-bit integer from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
31
buf.readFloatLE(offset[, noAssert])
Reads a 32-bit float from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
32
buf.readFloatBE(offset[, noAssert])
Reads a 32-bit float from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
33
buf.readDoubleLE(offset[, noAssert])
Reads a 64-bit double from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
34
buf.readDoubleBE(offset[, noAssert])
Reads a 64-bit double from the buffer at the specified offset with the specified endian format. Set noAssert to true to skip validation of offset. It means the offset may be beyond the end of the buffer. Defaults to false.
35
buf.writeUInt8(value, offset[, noAssert])
Writes a value to the buffer at the specified offset. Note that the value must be a valid unsigned 8-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
36
buf.writeUInt16LE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid unsigned 16-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of correctness. Defaults to false.
37
buf.writeUInt16BE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid unsigned 16-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
38
buf.writeUInt32LE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid unsigned 32-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
39
buf.writeUInt32BE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid unsigned 32-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
40
buf.writeInt8(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 8-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
41
buf.writeInt16LE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 16-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
42
buf.writeInt16BE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 16-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
43
buf.writeInt32LE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 32-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
44
buf.writeInt32BE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid signed 32-bit integer. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of correctness. Defaults to false.
45
buf.writeFloatLE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note that the value must be a valid 32-bit float. Set noAssert to true to skip validation of value and offset. It means that the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
46
buf.writeFloatBE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note, value must be a valid 32-bit float. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
47
buf.writeDoubleLE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note, value must be a valid 64-bit double. Set noAssert to true to skip validation of value and offset. It means that value may be too large for the specific function and offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
48
buf.writeDoubleBE(value, offset[, noAssert])
Writes a value to the buffer at the specified offset with the specified endian format. Note, value must be a valid 64-bit double. Set noAssert to true to skip validation of value and offset. It means the value may be too large for the specific function and the offset may be beyond the end of the buffer leading to the values being silently dropped. It should not be used unless you are certain of its correctness. Defaults to false.
49
buf.fill(value[, offset][, end])
Fills the buffer with the specified value. If the offset (defaults to 0) and end (defaults to buffer.length) are not given, it will fill the entire buffer.
Class Methods
Sr.No.
Method & Description
1
Buffer.isEncoding(encoding)
Returns true if the encoding is a valid encoding argument, false otherwise.
2
Buffer.isBuffer(obj)
Tests if obj is a Buffer.
3
Buffer.byteLength(string[, encoding])
Gives the actual byte length of a string. encoding defaults to ”utf8”. It is not the same as String.prototype.length, since String.prototype.length returns the number of characters in a string.
4
Buffer.concat(list[, totalLength])
Returns a buffer which is the result of concatenating all the buffers in the list together.
5
Buffer.compare(buf1, buf2)
The same as buf1.compare(buf2). Useful for sorting an array of buffers.