node緩沖區
什么是緩沖液? (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
的集合或集合。 二進制中的每個數字,一組中的每個1和0稱為bit 。 計算機將數據轉換為該二進制格式以存儲和執行操作。 例如,以下是五個不同的二進制文件:
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中沒有字節類型數據。 為了處理二進制數據,Node.js包括一個二進制緩沖區實現,該實現帶有一個名為Buffer
的全局模塊。
創建一個緩沖區 (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中創建緩沖區。 您可以創建一個10字節大小的空緩沖區。
const buf1 = Buffer.alloc(10);
From UTF-8-encoded strings, the creation is like this:
通過UTF-8編碼的字符串,創建過程如下所示:
const buf2 = Buffer.from('Hello World!');
There are different accepted encoding when creating a Buffer:
創建緩沖區時,可以接受不同的編碼:
- ascii ASCII
- utf-8 utf-8
- base64: base64:
- latin1 拉丁語1
- binary 二元
- hex 十六進制
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()
.
在緩沖區API中分配了三個單獨的函數,以使用和創建新緩沖區。 在上面的示例中,我們看到了alloc()
和from()
。 第三個是allocUnsafe()
。
const buf3 = Buffer.allocUnsafe(10);
When returned, this function might contain old data that needs to be overwritten.
返回時,此函數可能包含需要覆蓋的舊數據。
與緩沖液的相互作用 (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可以進行不同的交互。 我們將在這里介紹其中的大多數內容。 讓我們從將緩沖區轉換為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指定要轉換的對象的類型是Buffer及其數據。 將空緩沖區轉換為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()
的直接函數,可將緩沖區轉換為JSON對象。 要檢查緩沖區的大小,我們可以使用length
方法。
emptyBuf.length;
// Output: 10
Now let us convert buffer to a readable string, in our case, the utf-8 encoded.
現在,讓我們將緩沖區轉換為可讀字符串,在本例中為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
默認情況下, .toString()
將緩沖區轉換為utf-8格式的字符串。 這就是解碼緩沖區的方式。 如果指定一種編碼,則可以將緩沖區轉換為另一種編碼
console.log(bufferOne.toString('base64'));
有關緩沖區的更多信息: (More info on Buffers:)
Need a better understanding of buffers in Node.js? Check this out.
是否需要更好地了解Node.js中的緩沖區? 看一下這個。
翻譯自: https://www.freecodecamp.org/news/node-js-buffer-explained/
node緩沖區