java中的缓冲流和节点流
什么是缓冲区? (What is a buffer?)
A buffer is an area of memory. JavaScript developers are not familiar with this concept, much less than C, C++ or Go developers (or any programmer that uses a system programming language), which interact with memory every day.
缓冲区是内存区域。 JavaScript开发人员对这个概念并不熟悉,比每天与内存交互的C,C ++或Go开发人员(或使用系统编程语言的任何程序员)要少得多。
It represents a fixed-size chunk of memory (can’t be resized) allocated outside of the V8 JavaScript engine.
它表示在V8 JavaScript引擎外部分配的固定大小的内存块(无法调整大小)。
You can think of a buffer like an array of integers, which each represent a byte of data.
您可以将缓冲区视为一个整数数组,每个整数代表一个数据字节。
It is implemented by the Node Buffer class.
它由Node Buffer类实现 。
我们为什么需要缓冲液? (Why do we need a buffer?)
Buffers were introduced to help developers deal with binary data, in an ecosystem that traditionally only dealt with strings rather than binaries.
在传统上仅处理字符串而不是二进制文件的生态系统中,引入了缓冲区以帮助开发人员处理二进制数据。
Buffers are deeply linked with streams. When a stream processor receives data faster than it can digest, it puts the data in a buffer.
缓冲区与流紧密相连。 当流处理器接收数据的速度快于其消化速度时,它将数据放入缓冲区。
A simple visualization of a buffer is when you are watching a YouTube video and the red line goes beyond your visualization point: you are downloading data faster than you’re viewing it, and your browser buffers it.
当您观看YouTube视频时,缓冲区的简单可视化是红线超出了可视化点:您下载数据的速度比查看数据的速度快,并且浏览器会对数据进行缓冲。
如何创建缓冲区 (How to create a buffer)
A buffer is created using the Buffer.from()
, Buffer.alloc()
, and Buffer.allocUnsafe()
methods.
使用Buffer.from()
, Buffer.alloc()
和Buffer.allocUnsafe()
方法创建一个缓冲区。
const buf = Buffer.from('Hey!')
You can also just initialize the buffer passing the size. This creates a 1KB buffer:
您也可以只初始化传递大小的缓冲区。 这将创建一个1KB的缓冲区:
const buf = Buffer.alloc(1024)
//or
const buf = Buffer.allocUnsafe(1024)
使用缓冲区 (Using a buffer)
访问缓冲区的内容 (Access the content of a buffer)
A buffer, being an array of bytes, can be accessed like an array:
缓冲区(字节数组)可以像数组一样进行访问:
const buf = Buffer.from('Hey!')
console.log(buf[0]) //72
console.log(buf[1]) //101
console.log(buf[2]) //121
Those numbers are the Unicode Code that identifies the character in the buffer position (H => 72, e => 101, y => 121)
这些数字是Unicode代码,用于标识缓冲区位置中的字符(H => 72,e => 101,y => 121)
You can print the full content of the buffer using the toString()
method:
您可以使用toString()
方法打印缓冲区的全部内容:
console.log(buf.toString())
Notice that if you initialize a buffer with a number that sets its size, you’ll get access to pre-initialized memory that will contain random data, not an empty buffer!
请注意,如果使用设置大小的数字初始化缓冲区,您将可以访问将包含随机数据而不是空缓冲区的预初始化内存!
获取缓冲区的长度 (Get the length of a buffer)
Use the length
property:
使用length
属性:
const buf = Buffer.from('Hey!')
console.log(buf.length)
遍历缓冲区的内容 (Iterate over the contents of a buffer)
const buf = Buffer.from('Hey!')
for (const item of buf) {
console.log(item) //72 101 121 33
}
更改缓冲区的内容 (Changing the content of a buffer)
You can write to a buffer a whole string of data by using the write()
method:
您可以使用write()
方法将整个数据字符串写入缓冲区:
const buf = Buffer.alloc(4)
buf.write('Hey!')
Just like you can access a buffer with an array syntax, you can also set the contents of the buffer in the same way:
就像可以使用数组语法访问缓冲区一样,您也可以使用相同的方式设置缓冲区的内容:
const buf = Buffer.from('Hey!')
buf[1] = 111 //o
console.log(buf.toString()) //Hoy!
复制缓冲区 (Copy a buffer)
Copying a buffer is possible using the copy()
method:
使用copy()
方法可以复制缓冲区:
const buf = Buffer.from('Hey!')
let bufcopy = Buffer.alloc(4) //allocate 4 bytes
buf.copy(bufcopy)
By default you copy the whole buffer. 3 more parameters let you define the starting position, the ending position, and the new buffer length:
默认情况下,您将复制整个缓冲区。 另外3个参数可让您定义开始位置,结束位置和新的缓冲区长度:
const buf = Buffer.from('Hey!')
let bufcopy = Buffer.alloc(2) //allocate 2 bytes
buf.copy(bufcopy, 0, 2, 2)
bufcopy.toString() //'He'
切片缓冲区 (Slice a buffer)
If you want to create a partial visualization of a buffer, you can create a slice. A slice is not a copy: the original buffer is still the source of truth. If that changes, your slice changes.
如果要创建缓冲区的部分可视化,则可以创建切片。 切片不是副本:原始缓冲区仍然是真相的来源。 如果那改变了,您的切片也会改变。
Use the slice()
method to create it. The first parameter is the starting position, and you can specify an optional second parameter with the end position:
使用slice()
方法创建它。 第一个参数是起始位置,您可以指定第二个参数作为结束位置:
const buf = Buffer.from('Hey!')
buf.slice(0).toString() //Hey!
const slice = buf.slice(0, 2)
console.log(slice.toString()) //He
buf[1] = 111 //o
console.log(slice.toString())
java中的缓冲流和节点流