Buffer.alloc(size[, fill[, encoding]])
size
<integer> 新的Buffer
所需的长度。fill
<string> | <Buffer> | <Uint8Array> | <integer> 用于预填充新Buffer
的值。 默认值:0
。encoding
<string> 如果fill
是字符串,则这就是它的编码。 默认值:'utf8'
。
分配 size
个字节的新 Buffer
。
如果 fill
为 undefined
,则 Buffer
将以零填充。
import { Buffer } from 'node:buffer';
const buf = Buffer.alloc(5);
console.log(buf);
// 打印: <Buffer 00 00 00 00 00>
const { Buffer } = require('node:buffer');
const buf = Buffer.alloc(5);
console.log(buf);
// 打印: <Buffer 00 00 00 00 00>
如果 size
大于 buffer.constants.MAX_LENGTH
或小于 0,则抛出 ERR_INVALID_ARG_VALUE
。
如果指定了 fill
,则分配的 Buffer
将通过调用 buf.fill(fill)
进行初始化。
import { Buffer } from 'node:buffer';
const buf = Buffer.alloc(5, 'a');
console.log(buf);
// 打印: <Buffer 61 61 61 61 61>
const { Buffer } = require('node:buffer');
const buf = Buffer.alloc(5, 'a');
console.log(buf);
// 打印: <Buffer 61 61 61 61 61>
如果同时指定了 fill
和 encoding
,则分配的 Buffer
将通过调用 buf.fill(fill, encoding)
进行初始化。
import { Buffer } from 'node:buffer';
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
console.log(buf);
// 打印: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
const { Buffer } = require('node:buffer');
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
console.log(buf);
// 打印: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
调用 Buffer.alloc()
可能比替代的 Buffer.allocUnsafe()
慢得多,但可确保新创建的 Buffer
实例的内容永远不会包含来自先前分配的敏感数据,包括可能尚未分配给 Buffer
的数据。
如果 size
不是数值,则会抛出 TypeError
。
size
<integer> The desired length of the newBuffer
.fill
<string> | <Buffer> | <Uint8Array> | <integer> A value to pre-fill the newBuffer
with. Default:0
.encoding
<string> Iffill
is a string, this is its encoding. Default:'utf8'
.
Allocates a new Buffer
of size
bytes. If fill
is undefined
, the
Buffer
will be zero-filled.
import { Buffer } from 'node:buffer';
const buf = Buffer.alloc(5);
console.log(buf);
// Prints: <Buffer 00 00 00 00 00>
const { Buffer } = require('node:buffer');
const buf = Buffer.alloc(5);
console.log(buf);
// Prints: <Buffer 00 00 00 00 00>
If size
is larger than
buffer.constants.MAX_LENGTH
or smaller than 0, ERR_INVALID_ARG_VALUE
is thrown.
If fill
is specified, the allocated Buffer
will be initialized by calling
buf.fill(fill)
.
import { Buffer } from 'node:buffer';
const buf = Buffer.alloc(5, 'a');
console.log(buf);
// Prints: <Buffer 61 61 61 61 61>
const { Buffer } = require('node:buffer');
const buf = Buffer.alloc(5, 'a');
console.log(buf);
// Prints: <Buffer 61 61 61 61 61>
If both fill
and encoding
are specified, the allocated Buffer
will be
initialized by calling buf.fill(fill, encoding)
.
import { Buffer } from 'node:buffer';
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
console.log(buf);
// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
const { Buffer } = require('node:buffer');
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
console.log(buf);
// Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
Calling Buffer.alloc()
can be measurably slower than the alternative
Buffer.allocUnsafe()
but ensures that the newly created Buffer
instance
contents will never contain sensitive data from previous allocations, including
data that might not have been allocated for Buffer
s.
A TypeError
will be thrown if size
is not a number.