Buffers

A Buffer is a class that provides a way of handling binary data. It is a subclass of Uint8Array class and can interoperate with strings. Supported encoding maps include "uft8", "utf16le", "latin1", "ascii", "hex", and "base64". You can use Buffer when working with binary streams, file system operations, and network communication.

// Creates a buffer of 10 bytes filled with zeros
let buffer = Buffer.alloc(10);
buffer.write("Hello");
buffer.toString(); // => "Hello"

// Creates a buffer of 10 bytes filled with 'a'
buffer = Buffer.alloc(10, "a");
buffer.toString(); // => "aaaaaaaaaa"

// Creates a buffer from an array of bytes
buffer = Buffer.from([0x41, 0x42, 0x43]);
buffer.toString("hex"); // => "414243"

// Creating a Buffer from an Existing ArrayBuffer
const arrayBuffer = new ArrayBuffer(10);
buffer = Buffer.from(arrayBuffer);

// Slicing buffers
buffer = Buffer.from("Hello World");
const slicedBuffer = buffer.subarray(0, 5);
slicedBuffer.toString(); // => "Hello"

// Concatenate a list of Buffers into a single Buffer
let buffer1 = Buffer.from("Hello ");
let buffer2 = Buffer.from("World");
let buffer3 = Buffer.concat([buffer1, buffer2]);
buffer3.toString(); // => "Hello World"

// Copying Buffers
buffer1 = Buffer.from("Hello there");
buffer2 = Buffer.alloc(5);
buffer1.copy(buffer2); // Copies buffer1 into buffer2
buffer2.toString(); // => "Hello"

// Compare two Buffers
buffer1 = Buffer.from("a");
buffer2 = Buffer.from("b");
Buffer.compare(buffer1, buffer2); // => -1: 'a' < 'b'

Let's create a buffer with 8 bytes filled with the repeating pattern from the hex string "123456789a".

buffer = Buffer.alloc(8, "123456789a", "hex");
buffer.toString("hex"); // => "123456789a123456"

which corresponds to

0x12 0x34 0x56 0x78 0xc9a 0x12 0x34 0x56

You can read and write data at a specified offset using various methods provided by the Buffer class.

// Read 1 byte from the buffer
buffer.readUInt8(0).toString(16); // => "12"
buffer.readUInt8(1).toString(16); // => "34"

// Read 2 bytes as a little-endian 16-bit unsigned integer
buffer.readUInt16LE(0).toString(16); // => "3412"
buffer.readUInt16LE(2).toString(16); // => "7856"

// Read 2 bytes as a big-endian 16-bit unsigned integer
buffer.readUInt16BE(0).toString(16); // => "1234"
buffer.readUInt16BE(3).toString(16); // => "789a"

// Read 4 bytes as a little-endian 32-bit unsigned integer
buffer.readUInt32LE(0).toString(16); // => "78563412"
buffer.readUInt32LE(4).toString(16); // => "5634129a"

// Read 4 bytes as a big-endian 32-bit unsigned integer
buffer.readUInt32BE(0).toString(16); // => "12345678"
buffer.readUInt32BE(1).toString(16); // => "3456789a"

// Write 1 byte value
buffer.writeUInt8(0x21, 0); // => "213456789a123456"
buffer.writeUInt8(0xff, 1); // => "21ff56789a123456"

// Write 2 byte value as a little-endian
buffer.writeUInt16LE(0xabcd, 0); // => "cdab56789a123456"
buffer.writeUInt16LE(0x1234, 2); // => "cdab34129a123456"

// Write 2 byte value as a big-endian
buffer.writeUInt16BE(0x8452, 0); // => "845234129a123456"
buffer.writeUInt16BE(0x4210, 3); // => "8452344210123456"

// Write 4 byte value as a little-endian
buffer.writeUInt32LE(0xabcd1234, 0); // => "3412cdab10123456"
buffer.writeUInt32LE(0xaabbccdd, 4); // => "3412cdabddccbbaa"

// Write 4 byte value as a big-endian
buffer.writeUInt32BE(0x12345678, 0); // => "12345678ddccbbaa"
buffer.writeUInt32BE(0x90abcdef, 4); // => "1234567890abcdef"