buffer.fill()

buf.fill(value[, offset[, end]][, encoding])

Fills the Buffer with the specified value. If the offset (defaults to 0) and end (defaults to buf.length) are not given the entire buffer will be filled. The method returns a reference to the Buffer, so calls can be chained. This is meant as a small simplification to creating a Buffer. Allowing the creation and fill of the Buffer to be done on a single line:

const b = Buffer.alloc(50, 'h');
console.log(b.toString());
  // Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

encoding is only relevant if value is a string. Otherwise it is ignored. value is coerced to a uint32 value if it is not a String or Number.

The fill() operation writes bytes into the Buffer dumbly. If the final write falls in between a multi-byte character then whatever bytes fit into the buffer are written.

Buffer.alloc(3, '\u0222');
  // Prints: <Buffer c8 a2 c8>
doc_Nodejs
2016-04-30 04:37:33
Comments
Leave a Comment

Please login to continue.