The buffer library is a global type for dealing with binary data directly.
It can be constructed in a variety of ways.
Buffer objects are used to represent a fixed-length sequence of bytes.
The Buffer class is a subclass of JavaScript's Uint8Array class and extends it with methods that cover additional use cases. The API accepts plain Uint8Arrays wherever Buffers are supported as well.
https://nodejs.org/docs/latest-v14.x/api/buffer.htmlMethods
(static) alloc(length, fillopt, encodingopt) → {Buffer}
Allocates a new buffer of size bytes. If fill is undefined, the Buffer will be zero-filled.
A TypeError will be thrown if size is not a number.
If size is larger than buffer.constants.MAX_LENGTH or smaller than 0, ERR_INVALID_OPT_VALUE is thrown.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
length |
integer | The desired length of the new Buffer. | ||
fill |
string | Buffer | Unit8Array | integer |
<optional> |
0 | A value to pre-fill the new Buffer with. |
encoding |
string |
<optional> |
utf8 | If fill is a string, this is its encoding. |
Returns:
- Type
- Buffer
Example
D._unsafe.buffer.alloc(8,'1') - returns '11111111'
(static) concat(data, length) → {Buffer}
Returns a new Buffer which is the result of concatenating all the Buffer instances in the list together.
If the list has no items, or if the length is 0, then a new zero-length Buffer is returned.
If length is not provided, it is calculated from the Buffer instances in list by adding their lengths.
If length is provided, it is coerced to an unsigned integer. If the combined length of the Buffers in data list exceeds length, the result is truncated to length.
Parameters:
Name | Type | Description |
---|---|---|
data |
Array.<Buffer> | Array.<Unit8Array> | List of Buffer or Uint8Array instances to concatenate. |
length |
integer | Total length of the Buffer instances in list when concatenated. |
Returns:
- Type
- Buffer
(static) from(data, offsetOrEncodingopt, lengthopt) → {Buffer}
Allocates a new Buffer using multiple types of input data (array, arrayBuffer, buffer, object, string)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
data |
Array.<integer> | ArrayBuffer | SharedArrayBuffer | Unit8array | Buffer | string | |||
offsetOrEncoding |
integer | string |
<optional> |
0 | utf8 | Index of first byte to expose or encoding of string |
length |
integer |
<optional> |
Number of bytes to expose. |
Returns:
- Type
- Buffer