Node JS Buffer Example

JavaScript object can only store character sequence value and can not store binary data. But you may need to read and store binary data when make I/O operations such as read / write image file, read / write binary stream through network etc. To resolve this issue, Node JS provide Buffer module to store and manipulate binary data. This is a built-in module, you can use it directly without require any modules.

1. Buffer And Character Encoding.

The Buffer instance is typically used to represent the sequence of encoded characters, such as utf-8, ucs2, base64, or hexadecimal encoded data. By using explicit character encoding, you can switch between the Buffer instance and the normal JavaScript string.

console.log("string1 is a english string totally.")

var string1 = "hello dev2qa.com";

var buffer1 = Buffer.from(string1, 'ascii');

console.log("buffer1 = " + buffer1);

console.log("buffer1 to ascii = " + buffer1.toString('ascii'));

console.log("buffer1 to utf-8 = " + buffer1.toString('utf-8'));

console.log("buffer1 to base64 = " + buffer1.toString('base64'));

console.log("string2 contains chinese character, so need to use utf-8 encoding to create the Buffer object.")

var string2 = "你好 dev2qa.com";

var buffer2 = Buffer.from(string2, 'utf-8');

console.log("buffer2 = " + buffer2);

console.log("buffer2 to ascii = " + buffer2.toString('ascii'));

console.log("buffer2 to utf-8 = " + buffer2.toString('utf-8'));

console.log("buffer2 to base64 = " + buffer2.toString('base64'));

Below is the output when execute above code.

string1 is a english string totally.
buffer1 = hello dev2qa.com
buffer1 to ascii = hello dev2qa.com
buffer1 to utf-8 = hello dev2qa.com
buffer1 to base64 = aGVsbG8gZGV2MnFhLmNvbQ==
string2 contains chinese character.
buffer2 = 你好 dev2qa.com
buffer2 to ascii = d= e%= dev2qa.com
buffer2 to utf-8 = 你好 dev2qa.com
buffer2 to base64 = 5L2g5aW9IGRldjJxYS5jb20=

2. Create Buffer Object.

Below code shows example about how to create Buffer object in Node JS.

// Create a Buffer object with size 10, filled with 0.
var buffer1 = Buffer.alloc(10);
console.log("buffer1 = " + buffer1.toString("utf8"));
console.log("buffer1 length = " + buffer1.length);

// Create a Buffer object with size 100, filled with 0x1.
var buffer2 = Buffer.alloc(100, 1);
console.log("buffer2 = " + buffer1.toString("hex"));
console.log("buffer2 length = " + buffer2.length);

/* Create a uninitialized Buffer object with size 10. This method is faster than Buffer.alloc(),But the return Buffer may contain old data,So need to use fill() or write() method to overwrite. */
var buffer3 = Buffer.allocUnsafe(10);
console.log("buffer3 = " + buffer1.toString("utf-8"));
console.log("buffer3 length = " + buffer3.length);


// Create a Buffer object contains [0x1, 0x2, 0x3].
var buffer4 = Buffer.from([1, 2, 3]);
console.log("buffer4 = " + buffer4.toString());
console.log("buffer4 length = " + buffer4.length);

// Create a Buffer object contains UTF-8 encoding character [0x74, 0xc3, 0xa9, 0x73, 0x74].
var buffer5 = Buffer.from('tést');
console.log("buffer5 = " + buffer5.toString());
console.log("buffer5 length = " + buffer5.length);

// Create a Buffer object contains Latin-1 encoding character [0x74, 0xe9, 0x73, 0x74].
var buffer6 = Buffer.from('tést', 'latin1');
console.log("buffer6 = " + buffer6);
console.log("buffer6 length = " + buffer6.length);

Below is the output of above code.

buffer1 length = 10
buffer2 = 00000000000000000000
buffer2 length = 100
buffer3 = 
buffer3 length = 10
buffer4 = 
buffer4 length = 3
buffer5 = tést
buffer5 length = 5
buffer6 = t�st
buffer6 length = 4

3. Write / Read Data To / From Buffer.

// Create a buffer, allocate 25 character space.
var buffer = Buffer.alloc(25);

// Write a string to the buffer and return the buffer length.
var length = buffer.write("http://www.dev2qa.com");

// Output the buffer.
console.log("buffer : " + buffer);

// Output the buffer character length.
console.log("buffer length : " + length);

// Read the first 10 character of the buffer.
console.log("buffer first 10 character : " + buffer.toString('utf8', 0, 10));

Below is the output of above code.

buffer : http://www.dev2qa.com
buffer length : 21
buffer first 10 character : http://www

4. Buffer To JSON.

var buffer = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
console.log(buffer.toJSON());

Below is the output.

{ type: 'Buffer', data: [ 1, 2, 3, 4, 5 ] }

5. Concat Buffer.

// Create the first buffer.
var buffer1 = Buffer.from(('dev2qa.com'));
// Create the second buffer.
var buffer2 = Buffer.from((' is a very good website.'));
// Concat two buffer to a new one.
var buffer3 = Buffer.concat([buffer1,buffer2]);
// Print out the new buffer.
console.log("buffer3 : " + buffer3.toString());

Below is the output.

buffer3 : dev2qa.com is a very good website.

6. Compare Buffer.

// Create the first buffer with a number string.
var buffer1 = Buffer.from('100');

// Create the second buffer with a character string.
var buffer2 = Buffer.from('www.dev2qa.com');

// Compare two buffer value. The result is a number.
var result = buffer1.compare(buffer2);

var message = "";

if(result < 0) {
    message = "buffer1 is smaller than buffer2";
}else if(result == 0){
    message = "buffer1 is equal to buffer2";
}else {
    message = "buffer1 is bigger than buffer2.";
}

console.log(message);

Below is the output of above code.

buffer1 is smaller than buffer2

7. Copy Buffer.

var buffer1 = Buffer.from('hello        www.dev2qa.com ');

var buffer2 = Buffer.from('http://');

// Copy buffer2's value to the specified position in buffer1.
buffer2.copy(buffer1, 6);

console.log(buffer1.toString());

Below is the output.

hello http://www.dev2qa.com

8. Slice Buffer.

var buffer1 = Buffer.from('http://www.dev2qa.com');

// Slice buffer1 and return buffer2.
var buffer2 = buffer1.slice(7, buffer1.length);

console.log("buffer2 : " + buffer2.toString());

Below is the output.

buffer2 : www.dev2qa.com
(Visited 33 times, 1 visits today)
READ :   Implement Queue Using Node JS Async Module Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.