node中buffer_您是否希望更好地了解Node.js中的Buffer? 看一下这个。

node中buffer

by Justice Mba

由Mba法官

Are you always mystified, like me, whenever you come across words like Buffer, Stream, and binary data in Node.js? Does that feeling make you shrink from understanding them, thinking they are not meant for you but only for Node.js gurus and package developers to understand?

每当您遇到诸如Buffer,Stream二进制数据之类的词时,您是否总是像我一样迷惑不解 Node.js? 这种感觉是否会使您不了解它们,以为它们不适合您而是仅供Node.js专家和包开发人员理解?

Indeed, those words could be very intimidating, especially when you’re coming into web development with Node.js without any CS degrees.

确实,这些话可能会令人生畏,尤其是当您使用没有任何CS学历的Node.js进行Web开发时。

Sadly, many tutorials and books will jump straight to teaching how to develop web applications with Node.js packages without letting you understand the core features of Node.js and why they exist. And some will brazenly tell you that you don’t need to understand them because you might never work with them directly.

可悲的是,许多教程和书籍将直接跳到如何使用Node.js包开发Web应用程序,而又不让您了解Node.js的核心功能以及它们为什么存在。 有些人会无耻地告诉您,您不需要了解它们,因为您 可能永远不会直接与他们合作。

Well, true, you might never work with them directly if you chose to remain an average Node.js developer.

好吧,没错,如果您选择保留普通的Node.js开发人员,则可能永远不会直接与他们合作。

However, if mysteries get you really curious, and you’ll stop at nothing to satisfy your curiosity, and if you want to take your Node.js understanding to the next level, then you really want to dig deeper to understand the many core features of Node.js, like Buffer, for example. And that’s exactly why I’m writing this piece — to help us demystify some of these features and take our Node.js learning to the next level.

但是,如果谜团让您真正感到好奇,并且您会一意孤行来满足您的好奇心,并且如果您想将Node.js的理解提高到一个新的水平,那么您真的想更深入地了解许多核心功能像Buffer这样的Node.js。 这就是为什么我要写这篇文章—来帮助我们揭开其中一些功能的神秘面纱,并将我们对Node.js的学习提高到一个新的水平。

When introducing Buffer, the official Node.js docs states in part…

引入Buffer时官方的Node.js文档部分说明……

… mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to make it possible to interact with octet streams in the context of things like TCP streams and file system operations.

…读取或处理二进制数据流的机制。 Buffer类是作为Node.js API的一部分引入的,它使得在TCP流和文件系统操作之类的环境中与八位字节流进行交互成为可能。

Hmmm, unless you had prior knowledge of all the words in the above sentences, they are probably just a bunch of jargon. Let’s try to simplify that a bit by rephrasing it, so we can have a clear focus and not be distracted by the many bells and whistles in there. Extracting from that introduction, we could safely say:

嗯,除非您对上述句子中的所有单词有先验知识,否则它们可能只是一堆术语。 让我们尝试通过重新措辞来简化一下,以便我们可以有一个清晰的焦点,而不会被其中的许多风吹哨打扰。 从该引言中摘录,我们可以放心地说:

The Buffer class was introduced as part of the Node.js API to make it possible to manipulate or interact with streams of binary data.

Buffer类是作为Node.js API的一部分引入的,以便可以处理二进制数据流或与之交互。

Now that’s simpler right? But… Buffer, streams, binary data… still many big words. Well, let’s try to tackle these big words from the last to the first.

现在简单了吗? 但是……缓冲区,流,二进制数据……仍然是很多大字。 好吧,让我们尝试从最后到第一个解决这些大问题。

二进制数据是什么? (Binary data, what’s that?)

You probably already know that computers store and represent data in binaries. Binary is simply a set or a collection of 1s and 0s. For example, the following are five different binaries, five different sets of 1s and 0s:

您可能已经知道计算机以二进制存储和表示数据。 二进制只是一个1或0的集合或集合。 例如,以下是五个不同的二进制文件,五个不同的1和0集:

10, 01, 001, 1110, 00101011

1001001111000101011

Each number in a binary, each 1 and 0 in a set are called a Bit, which is a short form of Binary digIT.

二进制中的每个数字,集合中的每个10称为Bit ,这是Binary digIT的缩写。

To store or represent a piece of data, a computer needs to convert that data to its binary representation. For example, to store the number 12, a computer needs to convert 12 to its binary representation which is 1100.

为了存储或表示一条数据,计算机需要将该数据转换为其二进制表示形式。 例如,要存储数字12,计算机需要将12转换为其二进制表示1100

How does a computer know how to do this conversion? Well, it’s pure math. It’s the simple binary numeral system we learned in basic math — expressing a number in the base-2 numeral system. Computers understand that math.

计算机如何知道如何进行此转换? 好吧,这纯粹是数学。 这是我们在基本数学中学到的简单二进制数字系统-在以2为底的数字系统中表示数字。 计算机了解数学。

But numbers are not the only data type we work with. We also have strings, images, and even videos. Computers know how to represent all types of data in binaries. Let’s take strings, for example. How will a computer represent the string “L” in binaries? To store any character in binaries, Computers will first convert that character to a number, then convert that number to its binary representation. So for the string “L”, computers will first convert L to a number that represents L. Let’s see how.

但是数字不是我们处理的唯一数据类型。 我们还提供字符串,图像甚至视频。 计算机知道如何用二进制表示所有类型的数据。 让我们以字符串为例。 计算机将如何在二进制文件中表示字符串“ L”? 为了将任何字符存储在二进制文件中,计算机将首先将该字符转换为数字,然后将该数字转换为二进制表示。 所以对于字符串“ L”, 计算机首先将L转换为代表L的数字。 让我们看看如何。

Open your browser console and paste the following code snippet and then hit enter:"L".charCodeAt(0). What did you see? The number 76? That is the number representation or Character Code or Code Point of the character L. But how does a computer know what exact number will represent each character? How does it know to use the number 76 to represent L?

打开浏览器控制台,粘贴以下代码段,然后按Enter: "L".charCodeAt(0) 。 你看见什么了? 数字76? 即字符L的数字表示形式或字符代码代码点 。 但是计算机如何知道什么数字代表每个字符? 如何知道使用数字76表示L

字符集 (Character Sets)

Character Sets are already defined rules of what exact number represents each character. We have different definitions of these rules.The very popular ones include Unicode and ASCII. JavaScript plays really well with Unicode Character Sets. In fact, it is the Unicode in your browser that states that 76 should represent L.

字符集是已经定义的规则,确切的数字代表每个字符。 我们对这些规则有不同的定义。非常流行的规则包括UnicodeASCII 。 JavaScript在Unicode字符集方面的表现非常出色。 实际上,浏览器中的Unicode指出76应该代表L。

So we’ve seen how computers represent characters in numbers. Now, the computer will, in turn, represent the number 76 to its binary representation. You might think, well, just convert 76 to the base-2 numeral system. Not so fast!

因此,我们已经了解了计算机如何用数字表示字符。 现在,计算机将依次将数字76表示为其二进制表示形式。 您可能会认为,只需将76转换为以2为底的数字系统。 没那么快!

字符编码 (Character Encoding)

Just as there are rules that define what number should represent a character, there are also rules that define how that number should be represented in binaries. Specifically, how many bits to use to represent the number. This is called Character Encoding.

就像有一些规则定义应代表字符的数字一样,也有一些规则定义如何在二进制文件中表示该数字。 具体来说,用多少位来表示数字。 这称为字符编码

One of the definitions for Character Encoding is the UTF-8. UTF-8 states that characters should be encoded in bytes. A byte is a set of eight bits — eight 1s and 0s. So eight 1s and 0s should be used to represent the Code Point of any character in binary.

字符编码的定义之一是UTF-8 。 UTF-8指出字符应以字节编码 一个字节是一组八位,即八个1和0。 因此,应使用八个1和0来表示二进制中任何字符的代码点。

To understand this, as we mentioned earlier, the binary representation of the number 12 is 1100. So when UTF-8 state that 12 should be in eight bits, UTF-8 is saying that a computer needs to add more bits to the left side of the actual base-2 representation of the number 12 to make it a byte. So 12 should be stored as 00001100. Makes sense?

为了理解这一点,正如我们前面提到的,数字12的二进制表示形式是1100 。 因此,当UTF-8指出12应该是八位时,UTF-8表示计算机需要在数字12的实际base-2表示法的左侧增加更多位,以使其成为一个字节。 所以12应该存储为00001100 。 说得通?

Therefore, 76 should be stored as 01001100.

因此,应将76存储为01001100

This, my friends, is how computers store strings or characters in binaries. Likewise, computers also have specified rules on how images and videos should be converted or encoded and stored in binaries. The point here is, computers stores all data types in binaries, and this is known as binary data.

朋友们,这就是计算机在二进制文件中存储字符串或字符的方式。 同样,计算机还对应如何转换或编码图像和视频以及将其存储在二进制文件中指定了规则。 这里的重点是,计算机将所有数据类型存储在二进制文件中,这被称为二进制数据。

If you’re super interested in the nitty-gritty of Character Encoding, you might like this gentle and detailed introduction.

如果您对Character Encoding的精髓非常感兴趣,那么您可能会喜欢这个温柔而详细的介绍

Now we understand what binary data is, but what are streams of binary data from our introduction to buffer?

现在我们了解了什么是二进制数据,但是什么是二进制数据流 从我们的介绍到缓冲?

(Stream)

Stream in Node.js simply means a sequence of data being moved from one point to the other over time. The whole concept is, you have a huge amount of data to process, but you don’t need to wait for all the data to be available before you start processing it.

Node.js中的流只是意味着随着时间的推移,一系列数据从一个点移到另一点。 整个概念是,您需要处理大量数据,但是您无需等待所有数据可用就可以开始处理它们。

Basically, this big data is broken down and sent in chunks. So from the original definition of a buffer (“streams of binary data… in the context of… file system”) this simply means binary data being moved in the file system. For example, moving the texts stored in file1.txt to file2.txt.

基本上,这些大数据被分解并分块发送。 因此,根据缓冲区的原始定义(“二进制数据流……在……文件系统中”),这仅意味着二进制数据正在文件系统中移动。 例如,将存储在file1.txt中的文本移动到file2.txt。

But how exactly does buffer help us interact with or manipulate binary data while streaming? What exactly is this buffer btw?

但是缓冲到底如何帮助我们在流传输时与二进制数据进行交互或操纵二进制数据? 缓冲区到底是什么?

缓冲 (Buffer)

We’ve seen that a stream of data is the movement of data from one point to the other, but how exactly are they moved?

我们已经看到,数据流就是数据从一个点到另一个点的移动,但是它们到底是如何移动的?

Typically, the movement of data is usually with the intention to process it, or read it, and make decisions based on it. But there is a minimum and a maximum amount of data a process could take over time. So if the rate the data arrives is faster than the rate the process consumes the data, the excess data need to wait somewhere for its turn to be processed.

通常,数据移动通常旨在处理或读取数据,并根据数据做出决策。 但是随着时间的推移,一个过程可能需要最少和最大的数据量。 因此,如果数据到达的速度快于进程使用数据的速度,则多余的数据需要在某处等待以处理该数据。

On the other hand, if the process is consuming the data faster than it arrives, the few data that arrive earlier need to wait for a certain amount of data to arrive before being sent out for processing.

另一方面,如果进程消耗数据的速度快于到达数据的速度,则较早到达的数据需要等待一定量的数据到达后再发送出去进行处理。

That “waiting areais the buffer! It is a small physical location in your computer, usually in the RAM, where data are temporally gathered, wait, and are eventually sent out for processing during streaming.

那“等候区就是缓冲区! 它是计算机中的一个很小的物理位置,通常在RAM中,在该位置临时收集,等待数据,并最终将其发送出去以进行流式处理。

We can think of the whole stream and buffer process as a bus station. In some bus stations, a bus is not allowed to depart until a certain amount of passengers arrive or until a specific departure time. Also, the passengers may arrive at different times with different speed. Neither the passengers nor the bus station has control over passengers’ arrival at the station.

我们可以将整个流和缓冲过程视为一个公交车站。 在某些公交车站,直到一定数量的乘客到达或直到特定的出发时间,才允许公交车离开。 同样,乘客可能以不同的速度到达不同的时间。 乘客和公交车站都没有控制乘客到达车站的控制权。

In any case, passengers who arrive earlier will need to wait until the bus station decides to send the bus on its way. While passengers who arrive when the bus is already loading or when the bus has already departed need to wait for the next bus.

无论如何,较早到达的乘客将需要等到公交车站决定在途中发车。 在公交车已经装载或公交车已经离开的时候到达的乘客需要等待下一辆公交车。

In whatever the case may be, there is always a waiting place. That is the Buffer to Node.js! Node.js can’t control the speed or time of data arrival, the speed of the stream. It only can decide when it’s time to send out the data. If it’s not yet time, Node.js will put them in the buffer — the “waiting area” — a small location in the RAM, until it’s time to send them out for processing.

无论如何,总会有一个等待的地方。 那就是Node.js的缓冲区 ! Node.js无法控制数据到达的速度或时间,即流的速度。 它只能决定何时发送数据。 如果还没来得及,Node.js会将它们放在缓冲区中的“等待区域”中-RAM中的一个小位置,直到需要将它们发送出去进行处理为止。

A typical example where you could see buffer in action is when you’re streaming a video online. If your internet connection is fast enough, the speed of the stream will be fast enough to instantly fill up the buffer and send it out for processing, then fill another one, and send it out, then another, and yet another… till the stream is finished.

在在线流式传输视频时,可以看到缓冲作用的一个典型示例。 如果您的互联网连接速度足够快,则流的速度将足够快,以立即填充缓冲区并将其发送出去进行处理,然后再填充另一个缓冲区,然后再将其发送出去,直到另一个,直到流完成。

But if your connection is slow, after processing the first set of data that arrived, the video player will display a loading icon, or display the text “buffering”, which means gathering more data, or waiting for more data to arrive. And when the buffer is filled up and processed, the player shows the data, the video. While playing that, more data will continue to arrive and wait in the buffer.

但是,如果您的连接速度很慢,则在处理了到达的第一组数据后,视频播放器将显示加载图标,或显示文本“缓冲”,这意味着收集更多的数据或等待更多的数据到达。 当缓冲区被填满并处理后,播放器将显示数据,视频。 在播放时,更多数据将继续到达并在缓冲区中等待。

If the player is done processing or playing the previous data, and the buffer is not yet filled up, the text “buffering” will be displayed again, waiting to gather more data to process.

如果播放器已完成处理或播放先前的数据,并且缓冲区尚未填满,则将再次显示“缓冲”文本,等待收集更多数据进行处理。

That is Buffer!

那就是缓冲!

From the original definition of a buffer, it shows that while in the buffer, we can manipulate or interact with the binary data being streamed. What kind of interaction could we possibly have with this raw binary data? The Buffer implementation in Node.js provides us with a whole list of what is doable. Let’s see some of them.

从缓冲区的原始定义来看,它表明在缓冲区中时,我们可以操纵或与流式二进制数据进行交互。 我们可能会与这种原始二进制数据进行什么样的交互? Node.js中的Buffer实现为我们提供了可行的完整列表。 让我们来看一些。

与缓冲区交互 (Interacting with a Buffer)

It is even possible to create your own buffer! Aside from the one Node.js will automatically create during a stream, it is possible to create and manipulate your own buffer. Interesting right? Let’s create one!

甚至可以创建自己的缓冲区! 除了在流中自动创建一个Node.js之外,还可以创建和操作自己的缓冲区。 有趣吧? 让我们创建一个!

Depending on what you want to achieve, there are different ways to create a buffer. Let’s see some.

根据要实现的目标,可以使用不同的方法来创建缓冲区。 让我们来看一些。

// Create an empty buffer of size 10. // A buffer that only can accommodate 10 bytes.
const buf1 = Buffer.alloc(10);
// Create a buffer with content
const buf2 = Buffer.from("hello buffer");

Once your buffer has been created, you can start interacting with it

创建缓冲区后,即可开始与其进行交互

// Examine the structure of a buffer
buf1.toJSON()// { type: 'Buffer', data: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] }// an empty buffer
buf2.toJSON()// { type: 'Buffer',     data: [        104, 101, 108, 108, 111, 32, 98, 117, 102, 102, 101, 114      ]    }
// the toJSON() method presents the data as the Unicode Code Points of the characters
// Examine the size of a buffer
buf1.length // 10
buf2.length // 12. Auto-assigned based on the initial content when created.
// Write to a bufferbuf1.write("Buffer really rocks!")
// Decode a buffer
buf1.toString() // 'Buffer rea'
//oops, because buf1 is created to contain only 10 bytes, it couldn't accommodate the rest of the characters
// Compare two buffers

There’s a whole lot of interactions we could have with a buffer. Head on to the official docs to play more with these methods.

我们可以使用缓冲区进行很多交互。 前往官方文档 ,进一步了解这些方法。

Finally, I’ll leave you with this little challenge: Go read through the source of zlib.js, one of the core libraries of Node.js, to see how it’s leveraging the power of buffer to manipulate streams of binary data. These turn out to be gziped files. As you read, document what you learn and kindly share with us here in the comments.

最后,我将面临这个小挑战: 仔细阅读Node.js的核心库之一zlib.js的源代码 ,以了解它如何利用缓冲区的功能来操纵二进制数据流。 这些原来是gzip文件。 阅读时,请在评论中记录您所学的内容,并在此与我们分享。

I hope this introduction helped you get a better understanding of Node.js Buffer.

希望本文能帮助您更好地了解Node.js Buffer。

If you feel like I’ve done a nice job, and that others deserve a chance to see this, kindly clap for the article to help spread a better understanding of Buffer in our Node.js community.

如果您觉得我做得很好,而其他人也有机会看到这一点,请拍手鼓掌,以帮助在我们的Node.js社区中更好地理解Buffer。

If you have a question that hasn’t been answered or you have a different understanding of some of the points here, feel free to drop in comments here or via Twitter.

如果您有未解决的问题,或者对此处的某些观点有不同的理解,请随时在此处或通过Twitter发表评论。

翻译自: https://www.freecodecamp.org/news/do-you-want-a-better-understanding-of-buffer-in-node-js-check-this-out-2e29de2968e8/

node中buffer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值