0% found this document useful (0 votes)
22 views2 pages

Buf Length

Node example

Uploaded by

Troll Troll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Buf Length

Node example

Uploaded by

Troll Troll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

buf.

fill(value[, offset[, end]][, encoding])#


value <string> | <Buffer> | <Uint8Array> | <integer> The value with which to fill buf.
offset <integer> Number of bytes to skip before starting to fill buf. Default: 0.
end <integer> Where to stop filling buf (not inclusive). Default: buf.length.
encoding <string> The encoding for value if value is a string. Default: 'utf8'.
Returns: <Buffer> A reference to buf.
Fills buf with the specified value. If the offset and end are not given, the entire buf will be filled:
// Fill a `Buffer` with the ASCII character 'h'.

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

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

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 & 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:
// 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:
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.

buf.includes(value[, byteOffset][, encoding])#


Added in: v5.3.0
value <string> | <Buffer> | <Uint8Array> | <integer> What to search for.
byteOffset <integer> Where to begin searching in buf. If negative, then offset is calculated from
the end of buf. Default: 0.
encoding <string> If value is a string, this is its encoding. Default: 'utf8'.
Returns: <boolean> true if value was found in buf, false otherwise.
Equivalent to buf.indexOf() !== -1.
const buf = Buffer.from('this is a buffer');

console.log(buf.includes('this'));
// Prints: true
console.log(buf.includes('is'));
// Prints: true
console.log(buf.includes(Buffer.from('a buffer')));
// Prints: true
console.log(buf.includes(97));
// Prints: true (97 is the decimal ASCII value for 'a')
console.log(buf.includes(Buffer.from('a buffer example')));
// Prints: false
console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));
// Prints: true
console.log(buf.includes('this', 4));
// Prints: false

You might also like