Home >Web Front-end >JS Tutorial >How to use the Buffer class in node.js to process binary data
Foreword
In Node.js, a Buffer class is defined, which is used to create a buffer area specifically for storing binary data. This article introduces in detail how the Buffer class in node.js handles binary data. Without further ado, let’s take a look at the detailed introduction.
Create a Buffer object
The first method: directly use an array to initialize the buffer area
var arr = [0,1,2] var buf = new Buffer(arr) console.log(buf)
Execution effect:
The second method: directly use a string to initialize the buffer area
var str = 'hello' var buf = new Buffer(str) console.log(buf)
Execution effect:
In Node.js, encoding and decoding of string input and output will be automatically performed, and UTF8 encoding is used by default.
Conversion between Buffer objects and string objects
(1) toString
You can use the toString method of the Buffer object to convert the data saved in the Buffer object into a string. The usage method is as follows:
buf.toString([encoding],[start],[end])
Optional parameter 1: used to specify the text encoding format saved in the Buffer object. The default parameter is UTF8.
Optional parameter 2: Specify the starting position of the converted data, in bytes.
Optional parameter 3: Specify the end position of the converted data, in bytes.
The sample code is as follows:
var buf = new Buffer('阁下何不随风起') console.log(buf) console.log(buf.toString('utf8',9,12)) console.log(buf.toString('utf8',12,buf.length))
(2) write
Sometimes we need to write a string to an already created Buffer object. In this case, we can use the write method of the Buffer object.
buf.write(string,[offset],[length],[encodign])
Required parameter 1: Specify the string that needs to be written
Optional parameter 2-3: Specify the writing position after converting the string into byte data. The writing position of byte data is Starting from the 1+offset byte and ending at offset+length bytes.
Optional parameter 4: Encoding format, default utf8
The code is as follows:
var buf = new Buffer('阁下何不随风起') buf.write('扶摇直上千万里',0,buf.length) console.log(buf.toString())
(3) StringDecoder object
In Node.js, you can also use the StringDecoder object will The data in the Buffer object is converted to a string. This object has the same function as the toString method of the Buffer object, but provides better support for strings in the UTF8 encoding format.
When using the StringDecoder object, you first need to load the string_decoder module in Node.js. The method is as follows:
var StringDecoder = require('string_decoder').StringDecoder
After loading the string_decoder module, you can create a StringDecoder object as follows:
var decoder = new StringDecoder([encoding])
When you need to convert the data in the Buffer object into a string, you can use the write method of the StringDecoder object. A parameter can be used in the write method to specify the Buffer object that needs to be converted. This method will return the converted The string, the code is as follows:
decoder.write(buffer)
Okay, let’s compare the toString method of the Buffer object and the write method of StringDecoder, which one has better support for utf8.
Buffer’s toString method is as follows:
//在UTF-8编码格式下,将1个文字转换为多个字节 var buf = new Buffer('阁下何不随风起') console.log(buf) //0x是我手动加的 var str1 = new Buffer([0xe9, 0x98, 0x81, 0xe4, 0xb8, 0x8b, 0xe4, 0xbd]) console.log(str1.toString()) var str2 = new Buffer([0x95, 0xe4, 0xb8, 0x8d, 0xe9, 0x9a, 0x8f, 0xe9, 0xa3, 0x8e, 0xe8, 0xb5, 0xb7]) console.log(str2.toString())
Of course, you can also use the following method to connect two buffer objects and then use the toString method to convert the data into a string, but when the length of the Buffer object is large , the performance of this operation will become relatively underground.
Buffer.concat([str1,str2]).toString()
StringDecoder’s write method is as follows:
//在UTF-8编码格式下,将1个文字转换为多个字节 var buf = new Buffer('阁下何不随风起') console.log(buf) //StringDecoder var StringDecoder = require('string_decoder').StringDecoder var decoder = new StringDecoder() //0x是我手动加的 var str1 = new Buffer([0xe9, 0x98, 0x81, 0xe4, 0xb8, 0x8b, 0xe4, 0xbd]) console.log(decoder.write(str1)) var str2 = new Buffer([0x95, 0xe4, 0xb8, 0x8d, 0xe9, 0x9a, 0x8f, 0xe9, 0xa3, 0x8e, 0xe8, 0xb5, 0xb7]) console.log(decoder.write(str2))
You can tell who has better support at a glance.
Conversion between Buffer objects and JSON objects
In Node.js, you can use the JSON.stringify method to convert the data saved in the Buffer object into a string, or you can use the JSON.parse method to convert a passed The converted string is restored to an array.
Copy cache data
When you need to copy the binary data saved in the Buffer object to another Buffer object, you can use the copy method of the Buffer object. The copy method is used as follows:
buf.copy(targetBuffer,[targetStart],[sourceStart],[sourceEnd])
Required parameter 1: Specify the target Buffer object to be copied
Optional parameter 2: Specify the byte from which data should be written in the target Buffer object
Optional parameter 3: Specify to obtain data from the copy source Buffer object The starting position of the time, the default is 0
Optional parameter 4: Specify the end position when obtaining data from the copy source Buffer object, the default value is the length of the copy source object
The sample code is as follows:
//目标:将buf1 Buffer对象中的内容复制到buf2 Buffer对象中 //复制的目标起始位置为buf2 Buffer对象中的第11个字节处(第11个字节出开始写入) var buf1 = new Buffer('阁下何不随风起') console.log(buf1) var buf2 = new Buffer(128) buf2.fill(0) console.log(buf2) //关键代码 buf1.copy(buf2,10) //输出复制后的buf2 console.log(buf2) //以字符串的形式打印buf2 var StringDecoder = require('string_decoder').StringDecoder var decoder = new StringDecoder() console.log(decoder.write(buf2))
Buffer Class method
(1) isBuffer method
is used to determine whether an object is a Buffer object. The usage method is as follows:
Buffer.isBuffer(objc)
The sample code is as follows:
var a = 'abcde' var b = new Buffer(10) console.log(typeof a) console.log(typeof b) console.log('---我是奇妙的分隔符----') console.log(Buffer.isBuffer(a)) console.log(Buffer.isBuffer(b))
Console
(2) byteLength method
You can use the ByteLength method to calculate the number of bytes of a specified string. The usage method is as follows:
Buffer.byteLength(string,[encoding])
Required parameter 1: Specify the string for which the number of bytes needs to be calculated
Optional parameter 2: encoding format, default utf8
The sample code is as follows:
var str = '阁下何不随风起' console.log(str.length) console.log(Buffer.byteLength(str,'utf8')) console.log(Buffer.byteLength(str,'base64')) console.log(Buffer.byteLength(str,'utf16le'))
(3)isEncoding方法
isEncoding方法用于检测一个字符串是否为一个有效的编码格式字符串。使用方法如下:
Buffer.isEncoding(encoding)
在isEncoding方法中,使用一个参数,用于指定需要被检测的字符串,如果该字符串为有效的编码格式字符串,则方法返回true,如果该字符串不是一个有效的编码格式字符串,则方法返回false。
示例代码如下:
var str1 = 'utf8' console.log(Buffer.isEncoding(str1)) var str2 = 'utf16le' console.log(Buffer.isEncoding(str2)) var str3= 'utf16e' console.log(Buffer.isEncoding(str3))