Http://nodejs.org/api/buffer.html
Pure JavaScript is Unicode friendly and not nice to binary data. When dealing with TCP streams or the file system, it's necessary to handle octet streams. Node has several strategies for manipulating, creating, and consuming octet.
Raw data is stored in instances of the Buffer
class. A is similar to an Buffer
array of integers but corresponds to a raw memory allocation outside the V8 heap. A Buffer
cannot be resized.
Buffer
The class is a global, making it very rare that one would need to ever require(‘buffer‘)
.
Converting between buffers and JavaScript string objects requires an explicit encoding method. Here is the different string encodings.
' ASICC ', ' binary ' and so on.
Creating a typed array from a Buffer
works with the following caveats:caveat: defects
The buffer ' s memory is copied, not shared.
The buffer ' s memory is interpreted as an array, not a byte array. That's, new Uint32Array(new Buffer([1,2,3,4]))
creates a 4-element Uint32Array
with elements, not a with [1,2,3,4]
a single Uint32Array
element [0x1020304]
or [0x4030201]
.
NOTE:Node.js v0.8 simply retained a reference to the buffer in array.buffer
instead of cloning it.
While more efficient, it introduces subtle incompatibilities with the typed arrays specification. ArrayBuffer#slice()
Makes a copy of the slice while Buffer#slice()
creates a view.
New Buffer (str, [encoding]) #
str
String-string to encode.
encoding
String-encoding to use, Optional.
Allocates a new buffer containing the given str
. encoding
defaults to ‘utf8‘
.
New Buffer (size) #
Allocates a new buffer of size
octets.
New Buffer (Array) #
Allocates a new buffer using an of array
octets.
> New Buffer ([255,0,149]);
<buffer FF 95>
>
As shown in example 4-24, we use a string to create buffer, which is UTF-8 encoded by default. Such as
If you do not specify a coded grid , it will be considered a UTF-8 string. This does not mean that the buffer will
Complete the string to be able to save any Unicode character size (blindly assigning 4 words to each character)
section), it means that it does not truncate character content . In this example, we see that when the input string is small
When writing letters, Buffer uses the same byte structure regardless of which encoding is used, as each
All the letters fall into the same range. However, when we enter the "é" character, either the default UTF-8
Or are we explicitly specified as UTF-8, which are encoded into 2 byte sizes. But when we specify the code as
When ASCII, characters are truncated into a single byte.
You can also write a string to a buffer that already exists. Buffer.write () writes the string to the
Buffer at the specified location. If there is enough space at the beginning of the buffer specified position, the entire string
will be written. Otherwise, the tail of the string is truncated so that its size can be placed in buffer. In these two
Case, Buffer.write () will return a number indicating how many bytes were successfully written. Right
For UTF-8 strings, if a full character cannot be written to buffer, it will not be written separately
into one of the bytes of the character. As in example 4-25, because buffer is too small to write to a
Non-ASCII characters, so it is empty.
You can also write a string to a buffer that already exists. Buffer.write () writes the string to the location specified in buffer. If there is enough space at the beginning of the specified buffer, the entire string will be written. Otherwise, the tail of the string is truncated so that its size can be placed in buffer. In both cases, buffer.write () returns a number that indicates how many bytes were successfully written. For UTF-8 strings, if a full character cannot be written to buffer, a byte of that character is not written separately. In example 4-25, because buffer is too small to write a non-ASCII character, it is empty.
The buffer module of the node. JS Module