Buffer refers to space in memory which is used to store data temporarily. A buffer has traditionally been used between devices with speed mis-match so that they can keep on operating at their respective speeds without loss of data. In Node.js Buffers are used when dealing with file streams or tcp streams which are mainly octets of binary data.
fill
or 0
.
Buffer.alloc()
method is given below :
Buffer.alloc(size[, fill[, encoding]])
size
: Desired length of new Buffer. It accepts integer
type of data. fill
: The value to prefill the buffer. Default value is 0
.It accepts
any of the follwing : integer
, string
, buffer
type of data. encoding
Buffer.alloc()
method is given below :
//Name of the file : buffer.alloc.js
var buff = Buffer.alloc(20);
console.log(buff);
>node buffer.alloc.js
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>
Buffer.allocUnsafe()
method is given below :
Buffer.allocUnsafe(size)
size
: Desired length of new Buffer. It accepts integer
type of data. Buffer.allocUnsafe()
method is given below :
//Name of the file : buffer.allocUnsafe.js
var buff = Buffer.allocUnsafe(10);
console.log(buff);
>node buffer.allocUnsafe.js
<Buffer 00 00 00 00 08 00 00 00 07 00>
Buffer.from()
method is given below :
Buffer.from(string[, encoding])
string
: data is passed here. encoding
: The encoding of string. Default value is utf8
. This is
an optional parameter. Buffer.from()
method is given below :
//Name of the file : buffer.from.js
var buff1 = Buffer.from('Nodejsera');
console.log("buff1 : " + buff1);
>node buffer.from.js
buff1 : Nodejsera
0
: If both buffers are same 1
: If target buffer comes before the source buffer. -1
: If source buffer comes before the target buffer. buf.from()
method is given below :
buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])
target
: target buffer. targetStart
: Position from where comparion begins on target buffer. targetEnd
: Position on which comparison ends on target buffer. sourceStart
: Position from where comparion begins on source buffer. sourceEnd
: Position on which comparison ends on source buffer. buf.compare()
method is given below :
//Name of the file : buffer.compare.js
var buffer1 = Buffer.from('Nodejsera');
var buffer2 = Buffer.from('Nodejsera');
var output = buffer1.compare(buffer2);
console.log(output)
if(output < 0) {
console.log(buffer1 +" comes before " + buffer2);
}else if(output == 0){
console.log(buffer1 +" is same as " + buffer2);
}else {
console.log(output +" comes after " + buffer2);
}
>node buffer.compare.js
0
Nodejsera is same as Nodejsera
Buffer.concat()
method is given below :
Buffer.concat(list)
list
: List of buffers to be concatenated. Buffer.concat()
method is given below :
//Name of the file : buffer.concat.js
var buff1 = Buffer.from('Nodejsera for nodejs');
var buff2 = Buffer.from('- 30 days of node');
var buff3 = Buffer.concat([buff1,buff2]);
console.log("buff3 content: " + buff3.toString());
>node buffer.concat.js
buff3 content: Nodejsera for nodejs- 30 days of node
buf.copy()
method is given below :
buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
target
: buffer to which we need to copy. targetStart
: Position from where copy starts from source. sourceStart
: position from which copy starts. sourceEnd
: Position till which copy is done. buf.copy()
method is given below :
//Name of the file : buffer.copy.js
var buff = Buffer.from('Nodejsera');
var newbuff = Buffer.alloc(20);
buff.copy(newbuff);
console.log("Content of newbuff : " + newbuff.toString());
>node buffer.copy.js
Content of newbuff : Nodejsera
true
if buffers match, otherwise it will
return false
.
buf.equals()
method is given below :
buf.equals(otherBuffer)
otherBuffer
: Buffer to compare with. buf.equals()
method is given below :
//Name of the file : buffer.equals.js
var buff1 = Buffer.from('nodejsera');
var buff2 = Buffer.from('nodejsera');
console.log(buff1.equals(buff2));
>node buffer.equals.js
true
buf.fill()
method is given below :
buf.fill(value)
value
: The value with which we will fill the buffer. buf.fill()
method is given below :
//Name of the file : buffer.fill.js
var buff = Buffer.allocUnsafe(10).fill('nj');
console.log(buff.toString());
>node buffer.fill.js
njnjnjnjnj
-1
.
buf.indexOf()
method is given below :
buf.indexOf(value)
value
: the value we are looking for. buf.indexOf()
method is given below :
//Name of the file : buffer.indexOf.js
var buff1 = Buffer.from('Nodejsera');
console.log(buff1.indexOf('j'));
>node buffer.indexOf.js
4
buf.length
method is given below :
buf.length
buf.length
method is given below :
//Name of the file : buffer.length.js
var buf = Buffer.from('Nodejsera for nodejs');
console.log( buf.length);
>node buffer.length.js
20
buf.slice()
method is given below :
buf.slice([start[, end]])
start
: start offset of new buffer. end
: end offset of new buffer. buf.slice()
method is given below :
//Name of the file : buffer.slice.js
var buff1 = Buffer.from('Nodejsera');
var buff2 = buff1.slice(0,5);
console.log("content of buff2 : " + buff2.toString());
>node buffer.slice.js
content of buff2 : Nodej
buf.toJSON()
method is given below :
buf.toJSON()
buf.toJSON()
method is given below :
//Name of the file : bufferToJSON.js
var buf = Buffer.from('Nodejsera');
var json = buf.toJSON(buf);
console.log(json);
>node bufferToJSON.js
{ type: 'Buffer',
data: [ 78, 111, 100, 101, 106, 115, 101, 114, 97 ] }
buf.toString()
method is given below :
buf.toString([encoding[, start[, end]]])
encoding
: Character encoding to decode to. start
: starting offset. end
: ending offset. buf.toString()
method is given below :
//Name of the file : bufferToString.js
var buf = Buffer.from('Nodejsera for nodejs');
console.log( buf.toString('ascii'));
>node bufferToString.js
Nodejsera for nodejs
In this chapter of 30 days of node tutorial series, we learned about what are buffers and some
basic methods of buffer module of node.js
.