NodeJs of Buffer (buffer)

Foreword

JavaScript language itself only string data type, no binary data type.

But when dealing with streams like TCP stream or file, you must use the binary data. So Node.js, the definition of a Buffer class that is used to create a specialized storage buffer binary data.

In the Node.js, Buffer class is with Node kernel distributed with the core library. Buffer Node.js library brings a raw data storage methods, allowing Node.js binary data.

Meet Buffer, exemplified by the creation of several common Bufferclasses, please see the official documentation more

//使用new Buffer(size)方式创建一个Buffer实例:
var buf = new Buffer(10);
console.log("buf: ", buf);

//使用new Buffer(array)方式创建一个Buffer实例:
var buf2 = new Buffer(['runoob', 'ascii']);
console.log("buf2: ", buf2);

//使用new Buffer(str[,encoding])方式创建一个Buffer实例:
var buf3 = new Buffer('hisen', 'utf-8');
console.log('buf3: ', buf3)

Output

PS C:\HiSen\myWorkDemo\node_demo> node buffer.js
buf:  <Buffer 00 00 00 00 00 00 00 00 00 00>
buf2:  <Buffer 00 00>
buf3:  <Buffer 68 69 73 65 6e>
PS C:\HiSen\myWorkDemo\node_demo>

Conclusion:
一个Buffer对象的大小,在创建时就固定下来,创建之后不可改变 .

Buffer Common method

write buffer is written Node

buf.write(string[, offset[, length]][, encoding])

Parameters:
string- Write string buffer.
offset- index buffer to begin writing, default is 0.
length- number of bytes written, default buffer.length
encoding- encoding used. The default is 'utf8'.

// 写入缓冲区
var buf5 = Buffer.alloc(123);
len = buf5.write("https://blog.csdn.net/HiSen_CSDN");

console.log("写入字节数 : " + len); // 写入字节 32
console.log(buf5) // <Buffer 68 74 74 70 73 3a 2f 2f 62 6c 6f 67 2e 63 73 64 6e 2e 6e 65 74 2f 48 69 53 65 6e 5f 43 53 44 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... >

Buf string is written to the offset location of the encoding in accordance with a character encoding. length parameter is the number of bytes written. If buf is not enough space to save the entire string, the string will only write part. Only partially decoded characters are not written.

toString () read buffer Node

buf.toString([encoding[, start[, end]]])

Decode buffer data and returns a string using the specified encoding.

Parameters:
encoding- encoding used. The default is 'utf8'.
start- Specifies the index position to start reading, the default is 0.
end- end position, the default is the end of the buffer.

// buffer 字符串编码,转换不同格式
const buf4 = Buffer.from('Hisen', 'ascii');
console.log(buf4.toString('hex'));  // 输出 486973656e
console.log(buf4.toString('base64')); // 输出 SGlzZW4=

Of course, now there is a Js atob|| btoaway to carry out base64and binary conversion. It is also very convenient.

NodeJs currently supports string encoding

ascii - supports only 7-bit ASCII data. If the high set is removed, then this code is very fast.
utf8 - Unicode multi-byte character encodings. Many web pages and other document formats use UTF-8.
utf16le - 2 or 4 bytes, Unicode character little-endian encoding. Support for proxy (U + 10000 to U + 10FFFF).
ucs2 - alias utf16le of.
base64 - Base64 encoding.
latin1 - Buffer one kind of the way of encoding a string of bytes encoding.
binary - the alias of latin1.
hex - each byte encoded as two hexadecimal characters.

In fact, Bufferthe class as well as format conversion, consolidation, compare, copy, and so on, do not do this extra sample, in fact, I personally feel scenarios might be: working with binary data flow, data format conversion. Subsequent ha ~ a deeper understanding, and then makeup. Children's shoes forgive me ha.

Published 20 original articles · won praise 40 · views 5870

Guess you like

Origin blog.csdn.net/HiSen_CSDN/article/details/103484711