fill

fun fill(value: String, offset: Number = definedExternally, end: Number = definedExternally, encoding: BufferEncoding = definedExternally)(source)
fun fill(value: Uint8Array<*>, offset: Number = definedExternally, end: Number = definedExternally, encoding: BufferEncoding = definedExternally)(source)
fun fill(value: Double, offset: Number = definedExternally, end: Number = definedExternally, encoding: BufferEncoding = definedExternally)(source)

Fills buf with the specified value. If the offset and end are not given, the entire buf will be filled:

import { Buffer } from 'node:buffer';

// Fill a `Buffer` with the ASCII character 'h'.

const b = Buffer.allocUnsafe(50).fill('h');

console.log(b.toString());
// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

// Fill a buffer with empty string
const c = Buffer.allocUnsafe(5).fill('');

console.log(c.fill(''));
// Prints: <Buffer 00 00 00 00 00>

value is coerced to a uint32 value if it is not a string, Buffer, or integer. If the resulting integer is greater than 255 (decimal), buf will be filled with value &#x26; 255.

If the final write of a fill() operation falls on a multi-byte character, then only the bytes of that character that fit into buf are written:

import { Buffer } from 'node:buffer';

// Fill a `Buffer` with character that takes up two bytes in UTF-8.

console.log(Buffer.allocUnsafe(5).fill('\u0222'));
// Prints: <Buffer c8 a2 c8 a2 c8>

If value contains invalid characters, it is truncated; if no valid fill data remains, an exception is thrown:

import { Buffer } from 'node:buffer';

const buf = Buffer.allocUnsafe(5);

console.log(buf.fill('a'));
// Prints: <Buffer 61 61 61 61 61>
console.log(buf.fill('aazz', 'hex'));
// Prints: <Buffer aa aa aa aa aa>
console.log(buf.fill('zz', 'hex'));
// Throws an exception.

Since

v0.5.0

Return

A reference to buf.

Parameters

value

The value with which to fill buf. Empty value (string, Uint8Array, Buffer) is coerced to 0.

encoding='utf8' The encoding for value if value is a string.