node缓冲区_Node.js缓冲区介绍

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只是10的集合或集合。 二进制中的每个数字,一组中的每个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:)

翻译自: https://www.freecodecamp.org/news/node-js-buffer-explained/

node缓冲区

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值