Node.js buffer

Keywords: Javascript encoding JSON ascii

JavaScript language has only string data type and no binary data type.

But binary data must be used when processing streams such as TCP or file streams.

Therefore, in Node.js, a Buffer class is defined, which is used to create a special Buffer for storing binary data.

 

Before v6.0, the new Buffer() constructor is used to create the object instance directly when creating Buffer object. However, Buffer has a large permission Operation on memory, which can directly capture some sensitive information.

So after v6.0, it is recommended to use the Buffer.from() interface to create Buffer objects in the official documents.

 

Buffer instances are generally used to represent sequences of encoded characters, such as UTF-8, UCS2, Base64, or hexadecimal encoded data.

By using explicit character encoding, you can convert Buffer instances to regular JavaScript strings.

const buf=Buffer.from("cyy","ascii");

//Character encoding conversion
console.log(buf.toString("ascii"));
console.log(buf.toString("utf8"));
console.log(buf.toString("utf16le"));
console.log(buf.toString("base64"));
console.log(buf.toString("latin1"));
console.log(buf.toString("hex"));

 

 

 

The character encoding currently supported by Node.js includes:

  • ASCII - only 7-Bit ASCII data is supported. This encoding is very fast if the high bit is removed from the setting.

  • utf8 - multibyte encoded Unicode character. Many web pages and other document formats use UTF-8.

  • utf16le - 2 or 4 bytes, small byte order encoded Unicode characters. Support agent pair (U+10000 to U+10FFFF).

  • ucs2 - an alias for utf16le.

  • Base64 - Base64 encoding.

  • latin1 - a way to encode Buffer into a byte encoded string.

  • binary - alias for latin1.

  • hex - encodes each byte into two hexadecimal characters.

 

Buffer provides the following API s to create a buffer class:

  • Buffer.alloc(size[, fill[, encoding]]): returns a buffer instance of the specified size. If fill is not set, 0 will be filled by default
  • Buffer.allocUnsafe(size): returns a buffer instance of the specified size, but it will not be initialized, so it may contain sensitive data
  • Buffer.allocUnsafeSlow(size)
  • Buffer.from(array): returns a new buffer instance initialized by the value of array (the element of the incoming array can only be a number, otherwise it will be automatically overwritten by 0)
  • Buffer.from(arrayBuffer[, byteOffset[, length]]): returns a new buffer that shares the same memory as the given ArrayBuffer.
  • Buffer.from(buffer): copies the data of the incoming buffer instance and returns a new buffer instance
  • Buffer.from(string[, encoding]): returns a new buffer instance initialized by the value of string

 

// Create a 5-length, 0-filled Buffer. 
const buf1=Buffer.alloc(5);
// Create a length of 5 with 0 x1 Filled Buffer. 
const buf2=Buffer.alloc(5,1);
console.log(buf1);//<Buffer 00 00 00 00 00>
console.log(buf2);//<Buffer 01 01 01 01 01>

// Create a 10 length uninitialized Buffer. 
// This method is better than the call Buffer.alloc() Faster.
// But returned Buffer The instance may contain old data,
// So we need to use fill() or write() Rewrite.
const buf3 = Buffer.allocUnsafe(10);
console.log(buf3);//<Buffer 15 08 00 00 08 03 00 00 10 d0>

// Create a containing [0x1, 0x2, 0x3] Of Buffer. 
const buf4=Buffer.from([1,2,3]);
console.log(buf4);//<Buffer 01 02 03>

// Create a containing UTF-8 Byte Buffer. 
const buf5=Buffer.from("cyy");
console.log(buf5);//<Buffer 63 79 79>

// Create a containing Latin-1 Byte Buffer. 
const buf6=Buffer.from("cyy","latin1");
console.log(buf6);//<Buffer 63 79 79>

 

Write to Node buffer

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

The parameters are described as follows:

  • String - the string written to the buffer.

  • offset - the index value at which the buffer starts to write, which is 0 by default.

  • Length - the number of bytes written, the default is buffer.length

  • Encoding - encoding used. The default is' utf8 '.

Write string to offset position in buf according to character encoding of encoding. The length parameter is the number of bytes written. If buf does not have enough space to hold the entire string, only a part of the string is written. Only partially decoded characters are not written.

Return value

Returns the size of the actual write. If there is not enough buffer space, only part of the string will be written.

 

var buf=Buffer.alloc(256);//Length 256, filled with 0
var len=buf.write("i am cyy~");//Write cache, return the length of storage
console.log(len);

 

 

 

Read Node buffer data

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

The parameters are described as follows:

  • Encoding - encoding used. The default is' utf8 '.

  • Start - specifies the index location to start reading, which is 0 by default.

  • End - end position, default to the end of the buffer.

Return value

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

var buf=Buffer.alloc(26);

for(var i=0;i<26;i++){
    buf[i]=i+97;//Store 26 letters
}

console.log(buf.toString("ascii"));//abcdefghijklmnopqrstuvwxyz

console.log(buf.toString("ascii",0,5));//abcde

console.log(buf.toString("utf8",0,5));//abcde

console.log(buf.toString(undefined,0,5));//abcde

 

Convert Node Buffer to JSON object

buf.toJSON()

When stringing a Buffer instance, JSON.stringify() This toJSON() is implicitly called.

var buf=Buffer.from([0x1,0x2,0x3,0x4,0x5]);//Decimal 0-5
var json=JSON.stringify(buf);//String rotation json
console.log(json);//{"type":"Buffer","data":[1,2,3,4,5]}


//Buffer json Turn string
var data=JSON.parse(json,(key,value)=>{
    return value&&value.type=="Buffer"?Buffer.from(value.data):value;
});
console.log(data);//<Buffer 01 02 03 04 05>

 

 

 

Node buffer merge

Buffer.concat(list[, totalLength])
  • List - list of Buffer object arrays used for merging.

  • totalLength - specifies the total length of the merged Buffer object.

var buf1=Buffer.from("hello~");
var buf2=Buffer.from("i am cyy~");
var buf3=Buffer.concat([buf1,buf2]);
console.log(buf3.toString());

 

 

 

The function syntax of Node Buffer comparison is as follows. This method is introduced in Node.js v0.12.2:

buf.compare(otherBuffer);

Returns a number indicating that the buffer is before, after, or the same as other buffer.

var buf1=Buffer.from("abcd");
var buf2=Buffer.from("abcdefg");
var res=buf1.compare(buf2);

if(res<0){
    console.log("buf1 less than buf2");
}else if(buf1==buf2){
    console.log("buf1 Be equal to buf2");
}else{
    console.log("buf1 greater than buf2");
}

 

 

This method is a bit by bit comparison. buffer1.compare(buffer2), which is a bit by bit comparison. The first bit of buffer1 is compared with the first bit of buffer2. If it is equal, the second bit is compared and so on until the result is obtained.

var buffer1 = Buffer.from('ABCDEF99');
var buffer2 = Buffer.from('ABCDEF98765');
console.log(buffer1.compare(buffer2));//1

The result of the number and letter comparison is - 1

Node buffer copy

buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]])

The parameters are described as follows:

  • targetBuffer - the Buffer object to copy.

  • targetStart - number, optional, default: 0

  • sourceStart - number, optional, default: 0

  • sourceEnd - number, optional, default: buffer.length

 

var buf1=Buffer.from("hello");
var buf2=Buffer.from("cyy ah");
buf2.copy(buf1,3);

console.log(buf1.toString());

 

 

 

Node buffer clipping

buf.slice([start[, end]])

The parameters are described as follows:

  • start - number, optional, default: 0

  • end - number, optional, default: buffer.length

Returns a new buffer that points to the same block of memory as the old buffer, but is clipped from the index start to end

var buf1=Buffer.from("hello,cyy");
var buf2=buf1.slice(0,6);

console.log(buf2.toString());

 

 

 

Node buffer length calculation

buf.length;
var buf1=Buffer.from("hello,cyy");
console.log(buf1.length);

 

 

 

 

It points to the same block of memory as the old buffer, and operates on the same block of memory as the original buffer.

// Tailoring
var oldBuf=Buffer.from('cyycyy');
var newBuf=oldBuf.slice(0,3);
console.log("old: "+oldBuf.toString());
console.log("new: "+newBuf.toString());
newBuf.write("new!"); // Write buffer slice

// Changes to the original string before clipping
console.log("old: "+oldBuf.toString());
console.log("new: "+newBuf.toString());

Posted by gregor171 on Mon, 09 Mar 2020 00:08:14 -0700