node缓冲区_Node.js缓冲区介绍
node緩沖區(qū)
什么是緩沖液? (What are Buffers?)
Binary is simply a set or a collection of 1 and 0. Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations. For example, the following are five different binaries:
Binary只是1或0的集合或集合。 二進(jìn)制中的每個(gè)數(shù)字,一組中的每個(gè)1和0稱為bit 。 計(jì)算機(jī)將數(shù)據(jù)轉(zhuǎn)換為該二進(jìn)制格式以存儲(chǔ)和執(zhí)行操作。 例如,以下是五個(gè)不同的二進(jìn)制文件:
10, 01, 001, 1110, 00101011
10, 01, 001, 1110, 00101011
JavaScript does not have a byte type data in its core API. To handle binary data Node.js includes a binary buffer implementation with a global module called Buffer.
JavaScript的核心API中沒有字節(jié)類型數(shù)據(jù)。 為了處理二進(jìn)制數(shù)據(jù),Node.js包括一個(gè)二進(jìn)制緩沖區(qū)實(shí)現(xiàn),該實(shí)現(xiàn)帶有一個(gè)名為Buffer的全局模塊。
創(chuàng)建一個(gè)緩沖區(qū) (Creating a Buffer)
There are different ways you can create a buffer in Node.js. You can create an empty buffer by with a size of 10 bytes.
您可以使用多種方法在Node.js中創(chuàng)建緩沖區(qū)。 您可以創(chuàng)建一個(gè)10字節(jié)大小的空緩沖區(qū)。
const buf1 = Buffer.alloc(10);From UTF-8-encoded strings, the creation is like this:
通過UTF-8編碼的字符串,創(chuàng)建過程如下所示:
const buf2 = Buffer.from('Hello World!');There are different accepted encoding when creating a Buffer:
創(chuàng)建緩沖區(qū)時(shí),可以接受不同的編碼:
- ascii ASCII
- utf-8 utf-8
- base64: base64:
- latin1 拉丁語1
- binary 二元
- hex 十六進(jìn)制
There are three separate functions allocated in the Buffer API to use and create new buffers. In above examples we have seen alloc() and from(). The third one is allocUnsafe().
在緩沖區(qū)API中分配了三個(gè)單獨(dú)的函數(shù),以使用和創(chuàng)建新緩沖區(qū)。 在上面的示例中,我們看到了alloc()和from() 。 第三個(gè)是allocUnsafe() 。
const buf3 = Buffer.allocUnsafe(10);When returned, this function might contain old data that needs to be overwritten.
返回時(shí),此函數(shù)可能包含需要覆蓋的舊數(shù)據(jù)。
與緩沖液的相互作用 (Interactions with Buffer)
There are different interactions that can be made with the Buffer API. We are going to cover most of them here. Let us start with converting a buffer to JSON.
Buffer API可以進(jìn)行不同的交互。 我們將在這里介紹其中的大多數(shù)內(nèi)容。 讓我們從將緩沖區(qū)轉(zhuǎn)換為JSON開始。
let bufferOne = Buffer.from('This is a buffer example.'); console.log(bufferOne);// Output: <Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>let json = JSON.stringify(bufferOne); console.log(json);// Output: {"type": "Buffer", "data": [84,104,105,115,32,105,115,32,97,32,98,117,102,102,101,114,32,101,120,97,109,112,108,101,46]}The JSON specifies that the type of object being transformed is a Buffer, and its data. Converting an empty buffer to JSON will show us that it contains nothing but zeros.
JSON指定要轉(zhuǎn)換的對象的類型是Buffer及其數(shù)據(jù)。 將空緩沖區(qū)轉(zhuǎn)換為JSON將向我們顯示,它只包含零。
const emptyBuf = Buffer.alloc(10);emptyBuf.toJSON();// Output: { "type": "Buffer", "data": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] }Do notice that, Buffer API also provides a direct function toJSON() to convert a buffer into a JSON object. To examine the size of a buffer, we can use length method.
請注意,Buffer API還提供了toJSON()的直接函數(shù),可將緩沖區(qū)轉(zhuǎn)換為JSON對象。 要檢查緩沖區(qū)的大小,我們可以使用length方法。
emptyBuf.length; // Output: 10Now let us convert buffer to a readable string, in our case, the utf-8 encoded.
現(xiàn)在,讓我們將緩沖區(qū)轉(zhuǎn)換為可讀字符串,在本例中為utf-8編碼。
console.log(bufferOne.toString('utf8'));// Output: This is a buffer example..toString() by default converts a buffer to a utf-8 format string. This is how you decode a buffer. If you specify an encoding you can convert the buffer to another encoding
默認(rèn)情況下, .toString()將緩沖區(qū)轉(zhuǎn)換為utf-8格式的字符串。 這就是解碼緩沖區(qū)的方式。 如果指定一種編碼,則可以將緩沖區(qū)轉(zhuǎn)換為另一種編碼
console.log(bufferOne.toString('base64'));有關(guān)緩沖區(qū)的更多信息: (More info on Buffers:)
Need a better understanding of buffers in Node.js? Check this out.
是否需要更好地了解Node.js中的緩沖區(qū)? 看一下這個(gè)。
翻譯自: https://www.freecodecamp.org/news/node-js-buffer-explained/
node緩沖區(qū)
總結(jié)
以上是生活随笔為你收集整理的node缓冲区_Node.js缓冲区介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到上课是什么意思
- 下一篇: 黑客马拉松 招募_我如何赢得第一次黑客马