Question: What is buffer module?
Buffer class provides instances to store binary data similar to an array of integers. Buffer class is a global class that can be accessed in an application.
Question: How to create an empty buffer object?
var bufferData=Buffer.alloc(1024); //1024 bytes
Question: How to write in buffer object?
var bufferData.write('this is string'); //write only string
Question: How to create an array of buffer?
var buf1 = Buffer.from('1'); var buf2 = Buffer.from('2'); var buf3 = Buffer.from('3'); var bufferData = [buf1, buf2, buf3]; console.log(bufferData);//
OR
arrayObject=[]; arrayObject.push(Buffer.from('1')); arrayObject.push(Buffer.from('2')); arrayObject.push(Buffer.from('3')); console.log(arrayObject);
Question: What is concat? How to concate the buffer data?
Concatenates an array of Buffer objects into one Buffer object
var buf1 = Buffer.from('1'); var buf2 = Buffer.from('2'); var buf3 = Buffer.from('3'); var bufferData = [buf1, buf2, buf3]; bufferData=Buffer.concat(bufferData);
OR
arrayObject=[]; arrayObject.push(Buffer.from('1')); arrayObject.push(Buffer.from('2')); arrayObject.push(Buffer.from('3')); bufferData=Buffer.alloc(1024); bufferData = Buffer.concat(arrayObject); console.log(bufferData);
Question: How to compare two buffer?
var buf1 = Buffer.from('abc'); var buf2 = Buffer.from('abc'); var compare1 = Buffer.compare(buf1, buf2); console.log(compare1); //0 var buf1 = Buffer.from('a'); var buf2 = Buffer.from('b'); var compare2 = Buffer.compare(buf1, buf2); console.log(compare2); //-1 var buf1 = Buffer.from('b'); var buf2 = Buffer.from('a'); var compare3 = Buffer.compare(buf1, buf2); console.log(compare3); //1
Question: How to check if data is buffer?
Buffer.isBuffer(obj);//true|false