This class is used to create a cache area for storing binary data. Create a Buffer class with a length of 10 bytes. learning points:
Write Buffer
Read data from the buffer zone
Converts a Buffer to a JSON object.
Merge Buffer
Copy Buffer
Buffer pruning
Buffer Length
Node. js Buffer (Buffer)
This class is used to create a cache area for storing binary data.
Create a Buffer class
Create a 10-byte Buffer instance
[code]var buffer = new Buffer(10);
Create a Buffer instance using the given Array
[code]var buffer = new Buffer([10, 20, 30, 40]);
Use a string to create a Buffer instance
[Code] var buffer = new Buffer ('blog of the bright bings', 'utf-8 ');
Write Buffer
[Code] buffer. the write (string [, offset, length, encoding]) parameter string is required. The string offset of the buffer zone is the index value written in the buffer zone. The default value is 0 length, the default value is buffer. length encoding: Optional encoding. The default value is 'utf8'. Return Value: the actual write size. If the buffer space is insufficient, only some strings are written.
Case: buffer. js
[Code] var buffer = new Buffer (100); var len = buffer. write ('HTTP: // www. lamport. me/club '); console. log ('event input character: '+ len );
Read data from the buffer zone
[Code] encoding used by the buffer. toString ([ending, start, end]) parameter encoding. The default value is 'utf8 '. Start-specifies the index location to start reading. The default value is 0. End-end position. The default value is the end of the buffer. Return Value decodes the buffer data and returns the string with the specified Encoding
Case: buffer2.js
[code]var buffer = new Buffer(26);for (var i = 0; i < 26; i++) { buffer[i] = i + 97;}console.log(buffer.toString('ascii'));console.log(buffer.toString('ascii', 0, 4));console.log(buffer.toString('utf8', 0, 4));console.log(buffer.toString(undefined, 0, 4));
Case: buffer3.js
[code]var buffer = new Buffer('http://www.lamport.me/club');var data =buffer.toJSON(buffer);console.log(data);
Merge Buffer
[Code] Buffer. concat (list [, totalLength]) parameter list-list of arrays of Buffer objects used for merging. TotalLength-specifies the total length of the merged Buffer object.
Case: buffer4.js
[code]var bf1 = new Buffer("Hello ");var bf2 = new Buffer("World");var bf3 = Buffer.concat([bf1, bf2]);console.log(bf3.toString());
Copy Buffer
[Code] buf. copy (targetBuffer [, targetStart [, sourceStart [, sourceEnd]) parameter targetBuffer-Buffer object to be copied. TargetStart-number. Optional. Default Value: 0 sourceStart-number. Optional. Default Value: 0 sourceEnd-number. Optional. Default Value: buffer. length. No return value is returned.
Case: buffer5.js
[code]var buffer1 = new Buffer('ABC');var buffer2 = new Buffer(3);buffer1.copy(buffer2);console.log(buffer2.toString());
Buffer pruning
[Code] buf. slice ([start [, end]) parameter start-number, optional, default: 0 end-number, optional, default: buffer. the return value of length returns a new buffer, which points to the same memory as the old buffer, but is cut from the index start to end.
Example: buffer6.js
[code]var buffer = new Buffer("ABCD");var buffer2 = buffer.slice(0, 2);console.log(buffer2.toString());
Buffer Length
[Code] buf. length; Return Value Returns the Memory length occupied by the Buffer object.
Example: buffer7.js
[code]var buffer = new Buffer("abcd");console.log(buffer.length);
The above is what Node. js Buffer has learned. For more information, see PHP Chinese Network (www.php1.cn )!