Wechat applet Node.js (Basis 6) Buffer (Buffer)

Posted by mysterbx on Sun, 16 Jun 2019 00:14:44 +0200

Wechat applet Node.js  Buffer:

JavaScript Language itself has only string data type and no binary data type.  
But when dealing with streams like TCP or files, binary data must be used. Therefore, in the _____________ Node.js In this paper, a Buffer class is defined, which is used to create a special buffer for binary data.  
In node.js The Buffer class is the core library published with the Node kernel. The Buffer library is Node. js A method of storing raw data is introduced, which allows Node.js to process binary data. Whenever data moving in I/O operations needs to be processed in Node.js, it is possible to use Buffer libraries. The raw data is stored in an instance of the Buffer class. A Buffer is similar to an integer array, but it corresponds to a piece of raw memory outside of V8 heap memory.

Create Buffer

1. Examples of 10 lengths
var buf = new Buffer(10)
2. Create from a given array
var buf = new Buffer([10,20,30,40,50])
3. Create by strings
var buf = new Buffer("www.csdn.net","utf-8")
utf-8 is the default encoding
 The following codes are supported: "ascii", "utf8", "utf16le", "ucs2", "base64" and "hex".
  •  

Write to the buffer. write()

var buf = new Buffer(200)
var len = buf.write("www.csdn.net")
console.log("Number of bytes written:" + len)
  •  
  • String - A string written to a buffer.
  • offset - The index value that the buffer begins to write is 0 by default.
  • length - The number of bytes written by default is buffer.
  • Encoding - The encoding used. The default is `utf8'.

Buffer read. toString()

buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
  buf[i] = i + 97;
}

console.log( buf.toString('ascii'));       // Output: abcdefghijklmnopqrstuvwxyz
console.log( buf.toString('ascii',0,5));   // Output: abcde
console.log( buf.toString('utf8',0,5));    // Output: abcde
console.log( buf.toString(undefined,0,5)); // Use'utf8'encoding and output: abcde
  •  
  • Encoding - The encoding used. The default is `utf8'.
  • Start - Specifies the index location to start reading, defaulting to 0.
  • End - End position, default to the end of the buffer.

Buffer to JSON

var buf = new Buffer('www.runoob.com');
var json = buf.toJSON(buf);
console.log(json);
  •  

Merge Buffer.concat([,])

var buf = new Buffer("Beijing")
var buf1 = new Buffer("Shanghai")
var buf2 = Buffer.concat([buf,buf1],12)
console.log(buf2.toString())
  •  
  • List is used to merge the list of Buffer object arrays.
  • TotLength specifies the total length of the merged Buffer object.

Comparison (other Buffer)

Introduced in Node.js v0.12.2

var buffer1 = new Buffer("AVCX")
var buffer2 = new Buffer("ACVV")

var result = buffer1.compare(buffer2)

if(result < 0) {
   console.log(buffer1 + " stay " + buffer2 + "before");
}else if(result == 0){
   console.log(buffer1 + " and " + buffer2 + "identical");
}else {
   console.log(buffer1 + " stay " + buffer2 + "after");
}
  •  

Copy. compare (buffer 2)

buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]])
  •  
  • targetBuffer - The Buffer object to be copied.
  • targetStart - Number, optional, default: 0
  • SorceStart - Number, optional, default: 0
  • SorceEnd - Number, optional, default: buffer.length
var buf1 = new Buffer("beiJing")
var buf2 = new Buffer(8)
buf1.copy(buf2)
console.log("buf2 content:"+buf2.toString())
  •  

Cut. slice (buffer 2)

buf.slice([start[, end]])
  •  
  • start - Number, optional, default: 0
  • end - Number, optional, default: buffer.length
    Return value
    Returns a new buffer that points to the same block of memory as the old buffer, but is cut from the index start to end.
var buf1 = new Buffer("beiJing")
var buf2 = buf1.slice(0,8)
console.log("buf2 content:"+buf2)
// beiJing
  •  

length.

buf.length;
  •  

// Returns the bytes number of this buffer. Note that this is not necessarily the size of the contents in the buffer. length is the amount of memory allocated by the buffer object, and it does not change with the content of the buffer object.

Topics: encoding ascii JSON Javascript