buf.fill(value[, offset[, end]][, encoding])
曆史
| 版本 | 變化 | 
|---|---|
| v11.0.0 | 拋出   | 
| v10.0.0 | 負   | 
| v10.0.0 | 嘗試用零長度緩衝區填充非零長度緩衝區會觸發拋出的異常。  | 
| v10.0.0 | 為   | 
| v5.7.0 | 現在支持  | 
| v0.5.0 | 添加於:v0.5.0  | 
參數
value<string> | <Buffer> | <Uint8Array> | <integer> 要填充的值buf。offset<integer> 開始填充之前要跳過的字節數buf。 默認:0。end<integer> 在哪裏停止填充buf(不包括在內)。 默認:。buf.lengthencoding<string> 如果value是字符串,則value的編碼。 默認:'utf8'。- 返回: <Buffer>  對 
buf的引用。 
用指定的 value 填充 buf 。如果沒有給出offset和end,則將填充整個buf:
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: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhconst { Buffer } = require('node:buffer'); // Fill a `Buffer` with the ASCII character 'h'. const b = Buffer.allocUnsafe(50).fill('h'); console.log(b.toString()); // Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
如果 value 不是字符串、Buffer 或整數,則 value 被強製轉換為 uint32 值。如果結果整數大於 255(十進製),則 buf 將用 value & 255 填充。
如果 fill() 操作的最終寫入落在 multi-byte 字符上,則僅寫入適合 buf 的該字符的字節:
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>const { Buffer } = require('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>
如果value包含無效字符,則被截斷;如果沒有有效的填充數據,則拋出異常:
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.const { Buffer } = require('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.
相關用法
- Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.toString([encoding[, start[, end]]])用法及代碼示例
 - Node.js Buffer buf.writeDoubleLE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.writeBigInt64LE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.keys()用法及代碼示例
 - Node.js Buffer buf.writeFloatLE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.indexOf(value[, byteOffset][, encoding])用法及代碼示例
 - Node.js Buffer buf.readFloatLE([offset])用法及代碼示例
 - Node.js Buffer buf.readInt32LE([offset])用法及代碼示例
 - Node.js Buffer buf.writeInt8(value[, offset])用法及代碼示例
 - Node.js Buffer buf.swap32()用法及代碼示例
 - Node.js Buffer buf.writeInt32LE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.writeIntLE(value, offset, byteLength)用法及代碼示例
 - Node.js Buffer buf.values()用法及代碼示例
 - Node.js Buffer buf.writeDoubleBE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.length用法及代碼示例
 - Node.js Buffer buf.byteOffset用法及代碼示例
 - Node.js Buffer buf.readUInt32BE([offset])用法及代碼示例
 - Node.js Buffer buf.readDoubleLE([offset])用法及代碼示例
 - Node.js Buffer buf.writeFloatBE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.readBigUInt64LE([offset])用法及代碼示例
 - Node.js Buffer buf.readUInt16BE([offset])用法及代碼示例
 - Node.js Buffer buf.writeInt16BE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.swap64()用法及代碼示例
 - Node.js Buffer buf.readUInt8([offset])用法及代碼示例
 
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 buf.fill(value[, offset[, end]][, encoding])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
