Buffer (buffer)
- Buffer for processing the binary data stream
- Examples of similar integer array, a fixed size (after instantiation, is how much it can not be changed)
- C ++ code in physical memory allocation of foreign V8
- Buffer is a global variable, no need to require the use of references
// Common a predetermined length, the default is filled with zeros the console.log (Buffer.alloc (10 )); the console.log (Buffer.alloc ( 20 is )); // with 2 filled console.log (Buffer.alloc (5, 2 )); // create a length of 5, uninitialized Buffer. This method than calling Buffer.alloc () faster, // but Buffer returned instance may contain old data, so need to use fill () or write () rewritten. the console.log (Buffer.allocUnsafe (5,2 )); the console.log (Buffer.from ([ l, 2,3 ])); the console.log (Buffer.from ( 'Test' )); // specified encoding console.log (Buffer.from ( 'test', 'base64'));
operation result
Static properties and methods (Buffer own class attributes and methods) [common]
Buffer.byteLength & Buffer.isBuffer() & Buffer.concat()
// 实际占了几个字节 console.log(Buffer.byteLength('test')); //一个中文三个字节 console.log(Buffer.byteLength('测试')); console.log(Buffer.isBuffer({})); console.log(Buffer.isBuffer(Buffer.from([1,2,3]))); const buf1 = Buffer.from('This') const buf2 = Buffer.from(' is ') const buf3 = Buffer.from('a ') const buf4 = Buffer.from('test ') const buf5 = Buffer.from('!') const buf = Buffer.concat([buf1,buf2,buf3,buf4,buf5]) console.log(buf.toString());
operation result:
Examples of common attributes and methods (six kinds)
buf.length buf.toString() buf.fill()
buf.equals() buf.indexOf() buf.copy()
// 1.buf.length const Buffer.from buf = ( 'Test A This IS!' ) The console.log (to buf.length); // 15 const = BUF2 Buffer.alloc (10 ); const buf3 = Buffer.allocUnsafe (10); // default is not filled BUF2 [0] = 2 // demonstrated below, even if the default is not filled, but the length is controlled according to the size of your start created, regardless of the contents is not filled with filling console.log (buf2. length); // 10 the console.log (buf3.length); // 10 // 2.buf.toString () the console.log (buf.toString ()); // default. 8 This IS-A UTF Test Console. log (buf.toString ( 'Base64')); // VGhpcyBpcyBhIHRlc3Qh // 3.buf.fill () alloc 0 are populated automatically, but can fill () to fill another value const = buf4 is Buffer.allocUnsafe (10 ) the console.log (buf4 is); the console.log (buf4.fill ( 10, 2,6)); // fill (filled image content, where to start, where to end) // 4.buf.equals () Buffer two contents are equal const = BUF5 Buffer.from ( 'Test' ) buf6 const = Buffer.from ( 'Test' ) const buf7 = Buffer.from ( 'Test!' ) the console.log (buf5.equals (buf6)); // to true the console.log (buf5.equals (buf7)); // to false // 5.buf.indexOf () Test = BUF5 the console.log (buf5.indexOf ( 'S')); // find the location of the index returns (starting from 0) the console.log (buf5.indexOf ( 'A')); // can not find it returns -1 // 6.buf.copy () const = BUF8 Buffer.allocUnsafe (10) .fill ( '*' ) const buf9 Buffer.allocUnsafe = (10) .fill ( '!' ) // copy 'buf8' to the first data byte 3-6 'buf9' fourth offset start buf8.copy (buf9,4,3,6 ) console.log (buf9.toString ()); // !!!! *** !!!
// garbled
const StringDecoder = require('string_decoder').StringDecoder
// decoder Custom
const decoder = new StringDecoder('utf8')
const buf = Buffer.from ( 'Chinese string!');
for(let i =0 ;i<buf.length ; i+=5){
const b = Buffer.allocUnsafe(5)
buf.copy(b,0,i)
console.log (b.toString ()); // distortion
}
for(let i =0 ;i<buf.length ; i+=5){
const b = Buffer.allocUnsafe(5)
buf.copy(b,0,i)
// print with a decoder to determine the content of print
console.log (decoder.write (b)); // no distortion
}
// decoder does not realize that they deal with Chinese (byte width),