Home  >  Article  >  Web Front-end  >  Let’s talk about some important methods of the Node.js buffer module

Let’s talk about some important methods of the Node.js buffer module

青灯夜游
青灯夜游forward
2022-01-05 19:03:241451browse

This article will share with you a complete guide to the Node.js buffer and talk about some important methods of the Node buffer (Buffer) module. I hope it will be helpful to everyone!

Let’s talk about some important methods of the Node.js buffer module

A binary stream is a large collection of binary data. Since the size of the binary stream is usually quite large, the binary stream is generally not shipped together, but is divided into small chunks before shipping and sent one by one.

When the data processing unit temporarily stops receiving other data streams, the remaining data will be retained in the cache until the data processing unit is ready to receive more data.

Node.js Servers generally need to read and write in the file system, and files are actually binary streams at the storage level. In addition, Node.js can also be used with TCP streams, allowing TCP streams to provide reliable end-to-end byte streams to ensure communication over unreliable Internet networks.

The data stream sent to the receiver is buffered until the receiver is ready to receive more data to process. This is what the temporary data part of Node.js does - managing and storing binary data outside of the V8 engine.

Let’s dive into the various ways to use buffers (Buffer), learn more about them and learn how to use them in Node.js programs.

Let’s talk about some important methods of the Node.js buffer module

Node.js Buffer method

The biggest advantage of the Node.js buffer module is that it is built into Node.js, so we We can use it anywhere we want to use it.

Let’s go over some important Node.js buffer module methods.

Buffer.alloc()

This method will create a new buffer, but the allocated size is not fixed. When we call this method, we can allocate the size (in bytes) ourselves.

const buf = Buffer.alloc(6)  // 这会创建一个 6 字节的缓冲区

console.log(buf) // <Buffer 00 00 00 00 00 00>

Buffer.byteLength()

If we want to get the length of the buffer, we just call Buffer.byteLength () will do.

var buf = Buffer.alloc(10)
var buffLen = Buffer.byteLength(buf) // 检查缓冲区长度

console.log(buffLen) // 10

Buffer.compare()

By using Buffer.compare() we can compare two buffers , the return value of this method is one of -1, 0, 1.

Translator's Note: buf.compare(otherBuffer); This call will return a number -1, 0, 1, corresponding to buf before, after or the same as otherBuffer.

var buf1 = Buffer.from(&#39;Harsh&#39;)
var buf2 = Buffer.from(&#39;Harsg&#39;)
var a = Buffer.compare(buf1, buf2)
console.log(a) // 这会打印 0

var buf1 = Buffer.from(&#39;a&#39;)
var buf2 = Buffer.from(&#39;b&#39;)
var a = Buffer.compare(buf1, buf2)
console.log(a) // 这会打印 -1


var buf1 = Buffer.from(&#39;b&#39;)
var buf2 = Buffer.from(&#39;a&#39;)
var a = Buffer.compare(buf1, buf2)
console.log(a) // 这会打印 1

Buffer.concat()

As the name suggests, we can use this function to concatenate two buffers. Of course, just like strings, we can also concatenate more than two buffers.

var buffer1 = Buffer.from(&#39;x&#39;)
var buffer2 = Buffer.from(&#39;y&#39;)
var buffer3 = Buffer.from(&#39;z&#39;)
var arr = [buffer1, buffer2, buffer3]

console.log(arr)
/* buffer, !concat [ <Buffer 78>, <Buffer 79>, <Buffer 7a> ] */

// 通过 Buffer.concat 方法连接两个缓冲区
var buf = Buffer.concat(arr)

console.log(buf)
// <Buffer 78 79 7a> concat successful

Buffer.entries()

##Buffer.entries() will be created using the contents of this buffer And returns an iterator in the form [index, byte].

var buf = Buffer.from(&#39;xyz&#39;)

for (a of buf.entries()) {
    console.log(a)
    /* 这个会在控制台输出一个有缓冲区位置与内容的字节的数组 [ 0, 120 ][ 1, 121 ][ 2, 122 ] */
}

Buffer.fill()

We can use

Buffer.fill() This function inserts data into or Fill the buffer. See below for more information.

const b = Buffer.alloc(10).fill(&#39;a&#39;)

console.log(b.toString())
// aaaaaaaaaa

Buffer.includes()

Like a string, it will confirm whether the buffer has the value. We can achieve this using the

Buffer.includes() method, the given method returns a boolean value based on the search, i.e. true or false.

const buf = Buffer.from(&#39;this is a buffer&#39;)
console.log(buf.includes(&#39;this&#39;))
// true

console.log(buf.includes(Buffer.from(&#39;a buffer example&#39;)))
// false

Buffer.isEncoding()

We may know that binary files must be encoded, so if we want to check whether the data type supports character encoding what can we do about it? We can use the

Buffer.isEncoding() method to confirm. If supported, it will return true.

console.log(Buffer.isEncoding(&#39;hex&#39;))
// true

console.log(Buffer.isEncoding(&#39;utf-8&#39;))
// true

console.log(Buffer.isEncoding(&#39;utf/8&#39;))
// false

console.log(Buffer.isEncoding(&#39;hey&#39;))
// false

Buffer.slice()

##buf.slice()

will be used to use the selected buffer Element creates a new buffer - When a buffer is sliced, a new buffer is created containing a list of items to be found in the new buffer slice. <pre class="brush:js;toolbar:false;">var a = Buffer.from(&amp;#39;uvwxyz&amp;#39;); var b = a.slice(2, 5); console.log(b.toString()); // wxy</pre>

Buffer.swapX()

Buffer.swapX()

The byte order used to swap buffers . Use Buffer.swapX() (where X can be 16, 32, 64) to swap the byte order of 16-bit, 32-bit and 64-bit buffer objects. <pre class="brush:js;toolbar:false;">const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]) console.log(buf1) // &lt;Buffer 01 02 03 04 05 06 07 08&gt; // 交换 16 位字节顺序 buf1.swap16() console.log(buf1) // &lt;Buffer 02 01 04 03 06 05 08 07&gt; // 交换 32 位字节顺序 buf1.swap32() console.log(buf1) // &lt;Buffer 03 04 01 02 07 08 05 06&gt; // 交换 64 位字节顺序 buf1.swap64() console.log(buf1) // &lt;Buffer 06 05 08 07 02 01 04 03&gt;</pre>

Buffer.json()It can help us create a JSON object from the buffer, and this method will return the JSON buffer object ,

const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);

console.log(buf.toJSON());
// {"type":"Buffer", data:[1, 2, 3, 4, 5, 6, 7, 8]}

结论

如果我们需要进一步了解并使用 Node.js 的缓冲区,我们需要对缓冲区以及 Node.js 缓冲区的工作原理有更扎实的基础知识。我们还应该了解为什么我们需要使用 Node.js 缓冲区和各种 Node.js 缓冲区方法的使用。

更多node相关知识,请访问:nodejs 教程!!

The above is the detailed content of Let’s talk about some important methods of the Node.js buffer module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete