Buffer
An instance of a class is similar to an array of integers , but Buffer
is of a fixed size and allocates physical memory outside the V8 heap. Buffer
size is determined when it is created and cannot be adjusted .
Buffer
Class is a global variable in node. js, so you don't need to use it require(‘buffer‘).Buffer
.
Create a Buffer with a length of 10 and a 0 fill. Const BUF1 = Buffer.alloc (10);
Const (constant)
//.alloc (Allocate/request memory) ????? ? Const BUF5 = buffer.from (' tést ');//Create a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74]. Const BUF6 = buffer.from (' tést ', ' latin1 ');
//tYpedarray: Object describes an array-like view of the underlying binary data buffer. There is no global property named Typedarray, nor is there a directly visible Typedarray constructor. Instead, there are many different global properties whose values are typed array constructors for a particular element type.
Buffer.from(array)
Returns a new copy that Buffer
contains the supplied byte array.
- [
Buffer.from(arrayBuffer[, byteOffset [, length]])
Buffer.from(arrayBuffer)
] Returns a newly created share of the same memory as the given one ArrayBuffer
Buffer
.
Buffer.from(buffer)
Returns a new copy that contains the contents of the provided Buffer
content Buffer
.
Buffer.from(string[, encoding])
Returns a new copy that Buffer
contains the supplied string.
- [
Buffer.alloc(size[, fill[, encoding]])
] Buffer.alloc()
returns a filled Buffer
instance of the specified size . This method can be noticeably Buffer.allocUnsafe(size)
slower, but ensures that newly created Buffer
instances never contain old and potentially sensitive data.
Buffer.allocUnsafe(size)
With the Buffer.allocUnsafeSlow(size)
return of a new size
specified Buffer
, but its contents must be initialized , can be used buf.fill(0)
or fully writable.
--zero-fill-buffers
command-line Options
Buffer
The instance is automatically populated with 0 when it is created. Using this option changes the default behavior of these methods and has a noticeable effect on performance. It is recommended that you use options only if you need to force newly allocated instances to Buffer
not contain potentially sensitive data --zero-fill-buffers
.
--zero-fill-buffers> Buffer.allocunsafe (5) <buffer xx xx 00>
—— Buffer.allocUnsafe()
And
Buffer.allocUnsafeSlow()
Not safe
When called Buffer.allocUnsafe()
and Buffer.allocUnsafeSlow()
, the allocated memory segment is uninitialized (not populated with 0). Although such a design makes the allocation of memory very fast, the allocated memory segments may contain potentially sensitive old data.
Using a memory that is not completely rewritten by creating a memory that is Buffer.allocUnsafe()
Buffer
Buffer
readable, it may reveal its old data.
While Buffer.allocUnsafe()
the use has significant performance benefits , additional care must be taken to avoid introducing security vulnerabilities to the application .
Buffer and character encoding
Buffer
Instances are typically used to represent sequences of encoded characters , such as UTF-8, UCS2, Base64, or hexadecimal-encoded data. By using an explicit character encoding, you can convert an Buffer
instance to a normal JavaScript string.
UTF-8 (Uniform Resource Locator)
UCS2 (Universal Character Set)
BASE64 (64-coded)
Hex (Hex code)
For Examle:
Const BUF = buffer.from (' Hello World ', ' ASCII ');//Output 68656c6c6f20776f726c64console.log (buf.tostring (' hex '));//Output Agvsbg8gd29ybgq=console.log (buf.tostring (' base64 '));
The character encodings currently supported by node. JS include:
-
' ASCII '
-only supports data. This encoding is very fast if the setting is removed from the high.
-
' UTF8 '
-multibyte-encoded Unicode characters. Many Web pages and other document formats use UTF-8 .
-
' Utf16le '
-2 or 4 bytes, small-endian encoded Unicode characters. Support for proxy pairs (u+10000 to U+10FFFF).
-
' ucs2 '
- ' Utf16le '
aliases.
-
' base64 '
-Base64 encoded. When creating from a string, Buffer
, this encoding will also correctly accept the "url, as specified in chapter 5th rfc4648. Security Alphabet "with file name.
-
' latin1 '
-a way to encode , Buffer
into a byte-coded string, defined by the IANA in the RFC1345 63rd page, used as Latin-1 supplemental block with C0/C1 control code).
-
' binary '
- ' latin1 '
aliases.
-
' hex '
-encodes each byte as
Note: Modern browsers follow the WHATWG encoding standard to alias ' Latin1 ' and iso-8859-1 to win-1252. This means that when an operation such as http.get()
this is performed, if the character encoding returned is in the list of WHATWG specifications, it is possible that the server really returns win-1252 encoded data, at which point the ‘latin1‘
character encoding may be used incorrectly to decode the data .
Buffer and TypedArray
Buffer
Instances are also Uint8Array
instances. But there ECMAScript 2015
are some subtle differences with the TypedArray specification in []. For example, when ArrayBuffer#slice()
creating a copy of a slice, Buffer#slice()
the implementation is created directly on the existing Buffer
copy without copying it, which makes it Buffer#slice()
more efficient.
You can also Buffer
create a new instance from one by following these considerations TypedArray
:
Buffer
The memory of the object is copied TypedArray
to , not shared .
Buffer
The memory of an object is an array of elements that are parsed into an explicit element , rather than a byte array of a target type. That is, new Uint32Array(Buffer.from([1, 2, 3, 4]))
a [1, 2, 3, 4]
four element is created Uint32Array
instead of one that contains only one element [0x1020304]
or the other [0x4030201]
Uint32Array
.
You can also create a new .buffer
TypedArray
one from the properties of the Typearray object and share the same allocated memory as the instance Buffer
.
///
Uint8array (Uint8array typed array represents an array of 8-bit unsigned integers.) The content is initialized to 0. Once established, you can use the object's methods to refer to elements in the array, or use the standard array index syntax (that is, using parentheses notation). )
node. JS Official document parsing 02-buffer buffers