Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Buffer module of Node.js

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use the Buffer module of Node.js". In the daily operation, I believe many people have doubts about how to use the Buffer module of Node.js. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the Buffer module of Node.js". Next, please follow the editor to study!

A binary stream is a collection of large amounts of binary data. Because the size of the binary stream is usually quite large, the binary stream is not usually transported together, but is divided into small pieces before transportation and sent one by one.

When the data processing unit temporarily no longer receives 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, while files are actually binary streams at the storage level. In addition, Node.js can be used with TCP streams to enable TCP streams to provide reliable end-to-end byte flow guaranteed communication over unreliable Internet networks.

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

Let's delve into the various uses of buffers (Buffer) to learn more about them and learn how to use them in Node.js programs.

Node.js Buffer's method

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

Let's take a look at some of the important Node.js caching modules.

Buffer.alloc ()

This method creates 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) / / this creates a 6-byte buffer console.log (buf) / /

Buffer.byteLength ()

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

Var buf = Buffer.alloc (10) var buffLen = Buffer.byteLength (buf) / / check buffer length console.log (buffLen) / / 10

Buffer.compare ()

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

Translator's note: buf.compare (otherBuffer); this call returns a number-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) / / this prints 0var buf1 = Buffer.from ('a') var buf2 = Buffer.from ('b') var a = Buffer.compare (buf1, buf2) console.log (a) / / this prints-1var buf1 = Buffer.from ('b') var buf2 = Buffer.from ('a') var a = Buffer.compare (buf1) Buf2) console.log (a) / / this will print 1

Buffer.concat ()

As the name implies, we can use this function to connect two buffers. Of course, just like a string, we can 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 [,] * / / connect two buffers var buf = Buffer.concat (arr) console.log (buf) / / concat successful through the Buffer.concat method

Buffer.entries ()

Buffer.entries () creates and returns an iterator in the form of [index, byte] with the contents of this buffer.

Var buf = Buffer.from ('xyz') for (an of buf.entries ()) {console.log (a) / * this will output an array of bytes with buffer location and contents on the console [0,120] [1,121] [2122] * /}

Buffer.fill ()

We can use the function Buffer.fill () to insert or populate the data into the buffer. See below for more information.

Const b = Buffer.alloc (10). Fill ('a') console.log (b.toString ()) / / aaaaaaaaaa

Buffer.includes ()

Like a string, it confirms that the buffer has this value. We can do this using the Buffer.includes () method, which returns a Boolean value, that is, true or false, based on the search.

Const buf = Buffer.from ('this is a buffer') console.log (buf.includes (' this')) / / trueconsole.log (buf.includes (Buffer.from ('a buffer example') / / false

Buffer.isEncoding ()

We may know that binaries must be encoded, so what if we want to check whether the data type supports character encoding? We can use the Buffer.isEncoding () method to confirm. If supported, it returns true.

Console.log (Buffer.isEncoding ('hex')) / / trueconsole.log (Buffer.isEncoding (' utf-8')) / / trueconsole.log (Buffer.isEncoding ('utf/8')) / / falseconsole.log (Buffer.isEncoding (' hey')) / / false

Buffer.slice ()

Buf.slice () will be used to create a new buffer using the selected elements of the buffer-- when the buffer is sliced, a new buffer is created containing a list of items to be found in the new buffer slice.

Var a = Buffer.from ('uvwxyz'); var b = a.slice (2,5); console.log (b.toString ()); / / wxy

Buffer.swapX ()

Buffer.swapX () is used to swap the byte order of the buffer. 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.

Const buf1 = Buffer.from ([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]) console.log (buf1) / Exchange 16-bit byte order buf1.swap16 () console.log (buf1) / swap 32-bit byte order buf1.swap32 () console.log (buf1) / swap 64-bit byte order buf1.swap64 () console.log (buf1) / /

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]} this is the end of the study on "how to use Node.js 's Buffer module", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report