Buffer and Stream Node.js—Web backend hands-on experiment

Buffer _

The JavaScript language itself only has string data types, not binary data types.

But when dealing with streams like TCP or file streams, binary data must be used. Therefore, in Node.js, a Buffer class is defined, which is used to create a buffer area specifically for storing binary data.

In Node.js, the Buffer class is a core library shipped with the Node core. The Buffer library brings a method of storing raw data to Node.js, allowing Node.js to process binary data. Whenever you need to process data moved during I/O operations in Node.js, it is possible to use the Buffer library. . Raw data is stored in instances of the Buffer class. A Buffer is similar to an integer array, but it corresponds to a piece of raw memory outside of the V8 heap memory.

  1. ascii - Only 7-bit ASCII data is supported. This encoding is very fast if the high bits are removed.
  2. utf8 - Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8.
  3. utf16le - 2 or 4-byte, little-endian encoded Unicode character. Supports surrogate pairs (U+10000 to U+10FFFF).
  4. ucs2 - Alias ​​for utf16le.
  5. base64 - Base64 encoding base64_Baidu Encyclopedia
  6. latin1 - A way to encode a Buffer into a one-byte encoded string.
  7. binary - Alias ​​for latin1.
  8. hex - Encodes each byte into two hexadecimal characters.

[Experiment 1] Run the following program

Screenshot of running results

Create Buffer class

Buffer provides the following API to create Buffer classes:

  1. Buffer.alloc(size[, fill[, encoding]]) : Returns a Buffer instance of the specified size. If fill is not set, it will be filled with 0 by default.
  2. Buffer.allocUnsafe(size) : Returns a Buffer instance of the specified size, but it will not be initialized, so it may contain sensitive data
  3. Buffer.allocUnsafeSlow(size)
  4. Buffer.from(array) : Returns a new Buffer instance initialized by the value of array (the elements of the array passed in can only be numbers, otherwise they will be automatically overwritten by 0)
  5. Buffer.from(arrayBuffer[, byteOffset[, length]]) : Returns a new Buffer that shares the same memory as the given ArrayBuffer.
  6. Buffer.from(buffer) : Copies the data of the incoming Buffer instance and returns a new Buffer instance
  7. Buffer.from(string[, encoding]) : Returns a new Buffer instance initialized by the value of string

[Experiment 2] Run the following program, output the contents of buf1-6, and take a screenshot of the results

write buffer

buf.write(string[, offset[, length]][, encoding])

The parameters are described as follows:

string - The string to write to the buffer.

offset - the index value where the buffer starts to be written, default is 0.

length - the number of bytes written, defaults to buffer.length

encoding - the encoding to use. Default is 'utf8'.

return value

Returns the actual size written. If there is insufficient buffer space, only part of the string will be written.

[Experiment 3] Run the following code:

Screenshot of running results:

Read data from buffer

grammar

The syntax for reading Node buffer data is as follows:

buf.toString([encoding[, start[, end]]])

The parameters are described as follows

encoding - the encoding to use. Default is 'utf8'.

start - specifies the index position to start reading, the default is 0.

end - the end position, defaults to the end of the buffer.

return value

Decodes the buffer data and returns a string using the specified encoding.

[Experiment 4] Run the following code

Screenshot of running results: 

Convert Buffer to JSON object

【Experiment 5】 Run the code

Screenshot of running results:

 

buffer merge

grammar

The syntax for Node buffer merging is as follows:

Buffer.concat(list[, totalLength])

The parameters are described as follows

list - an array list of Buffer objects used for merging.

totalLength - specifies the total length of the combined Buffer objects.

return value

Returns a new Buffer object with multiple members merged.

[Experiment 6] Run the following program:

Screenshot of running results:

Node.js Stream( stream)

Read data from stream

Create an input.txt file with the following content:

Department of Information and Computer Science, Xinhua College, Ningxia University: Computer Science and Technology Major

[Experiment 7] Run the following program

Screenshot of running results:

write stream

[Experiment 8 ] Create the main.js file, the code is as follows:

Screenshot of running results:

pipe flow

Pipes provide a mechanism from an output stream to an input stream. Usually we use it to get data from one stream and pass the data to another stream.

[Experiment 9] Create the input.txt file with the following content:

Department of Information and Computer Science, Xinhua College, Ningxia University: Computer Science and Technology Major

Run the following code:

Screenshot of running results:

The function of this program is:

It can process files more efficiently and simplify the operation of copying files.

Guess you like

Origin blog.csdn.net/weixin_57398221/article/details/124100191