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!
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.
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('Harsh') var buf2 = Buffer.from('Harsg') var a = Buffer.compare(buf1, buf2) console.log(a) // 这会打印 0 var buf1 = Buffer.from('a') var buf2 = Buffer.from('b') var a = Buffer.compare(buf1, buf2) console.log(a) // 这会打印 -1 var buf1 = Buffer.from('b') var buf2 = Buffer.from('a') 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('x') var buffer2 = Buffer.from('y') var buffer3 = Buffer.from('z') 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('xyz') for (a of buf.entries()) { console.log(a) /* 这个会在控制台输出一个有缓冲区位置与内容的字节的数组 [ 0, 120 ][ 1, 121 ][ 2, 122 ] */ }
Buffer.fill()
Buffer.fill() This function inserts data into or Fill the buffer. See below for more information.
const b = Buffer.alloc(10).fill('a') console.log(b.toString()) // aaaaaaaaaa
Buffer.includes()
Buffer.includes() method, the given method returns a boolean value based on the search, i.e.
true or
false.
const buf = Buffer.from('this is a buffer') console.log(buf.includes('this')) // true console.log(buf.includes(Buffer.from('a buffer example'))) // false
Buffer.isEncoding()
Buffer.isEncoding() method to confirm. If supported, it will return
true.
console.log(Buffer.isEncoding('hex')) // true console.log(Buffer.isEncoding('utf-8')) // true console.log(Buffer.isEncoding('utf/8')) // false console.log(Buffer.isEncoding('hey')) // false
Buffer.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. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">var a = Buffer.from(&#39;uvwxyz&#39;);
var b = a.slice(2, 5);
console.log(b.toString());
// wxy</pre><div class="contentsignin">Copy after login</div></div>
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. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8])
console.log(buf1)
// <Buffer 01 02 03 04 05 06 07 08>
// 交换 16 位字节顺序
buf1.swap16()
console.log(buf1)
// <Buffer 02 01 04 03 06 05 08 07>
// 交换 32 位字节顺序
buf1.swap32()
console.log(buf1)
// <Buffer 03 04 01 02 07 08 05 06>
// 交换 64 位字节顺序
buf1.swap64()
console.log(buf1)
// <Buffer 06 05 08 07 02 01 04 03></pre><div class="contentsignin">Copy after login</div></div>
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!