NodeJS Buffer Explained
Posted By : Hotam Singh | 29-Apr-2018
Before TypedArray didn't come into the picture, the JavaScript had no mechanism for manipulating or reading binary streams. Binary streams are the streams for processing binary data i.e 0 and 1.
In this article, I am going to explain about Buffers in NodeJS. NodeJS introduced buffers module which provides a way of handling streams of binary data.
Before going to discuss in deep about Buffer, Let's understand first What is Buffer?
What is Buffer?
Buffers are the mechanism for reading and manipulating streams of binary data i.e. 0 and 1. Buffers(The Buffers are the global object in
Typical example and definition
We know that a stream is the movement of data from one end to another end. but how exactly they are moved in
Suppose, the rate at which data arrives is faster than the rate which a process consumes the data, the excess data needs to be stored somewhere for this data to be processed.
On the other side, if the process is consuming data faster than the rate it arrives,
The waiting area, where this excess data waits, is the buffer!. It is a small physical location in our computer(usually in the RAM), where data are temporally stored, wait, and are sent out for processing eventually during streaming.
A typical example we generally see. You could see buffer in action when you’re streaming a video online. If your internet speed is fast enough, the speed of the streams will be fast to instantly fill the buffer and send it out for processing. On the other side, if the internet speed is slow then your streaming video paused for some time and display a loading icon until a buffer is full and sent out for processing. This process continuously goes on until the streams are finished.
Where You See Buffers
Buffers are generally seen when we talk about binary data coming from binary streams, such as fs.createReadStream. fs.createReadStream is used to read the content of a file.
Usage
There are a few ways to create new buffers:
var buffer = new Buffer(8);
This buffer is uninitialized and contains 8 bytes.
var buffer = new Buffer([ 8, 6, 7, 5, 3, 0, 9]);
The above piece of code initializes the buffer to the contents
var buffer = new Buffer("I'm a string!", "
This initializes the buffer to a binary encoding of the first string as specified by the second argument (in this case,
Buffer also supports the following:
ucs2: A two-byte, little-endian encoding. Can encode a subset of Unicode.
base64: Base64 string encoding.
binary: This is the "binary string" format mentioned earlier, and is in the process of being deprecated. Avoid its use.
Writing to Buffers
Given that there is already a buffer created:
var buffer = new Buffer(16);
we can start writing strings to it:
buffer.write("Hello", "utf-8")
5
The first argument of
buffer.write returned 5. This means that we wrote to five bytes of the buffer.
buffer.write(" world!", 5, "utf-8")
7
When we provide three arguments to buffer.write, the second argument is an
Reading from Buffers
toString:
Probably the most common way to read buffers is to use the toString()
buffer.toString('utf-8')
'Hello world!\u0000k\t'
Again, the first argument is the encoding. In this case, we can see that the entire buffer is not used! because we know that how many bytes we have written to this buffer.
buffer.toString("utf-8", 0, 12)
'Hello world!'
Buffers Methods
Buffer.isBuffer(object)
Returns true if
Buffer.byteLength(string, encoding)
Check the number of bytes required to encode a string with a given encoding(which defaults to
var snowman = "?";
snowman.length
1
Buffer.byteLength(snowman)
3
Buffer.length
This is the length of your
var buffer = new Buffer(16)
buffer.write(snowman)
3
buffer.length
16
In this example, the contents
Buffer.copy
buffer.copy(target, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
var frosty = new Buffer(24)
var snowman = new Buffer("?", "utf-8")
frosty.write("Happy birthday! ", "utf-8")
16
snowman.copy(frosty, 16)
3
frosty.toString("utf-8", 0, 19)
'Happy birthday! ?'
Buffer.slice
buffer.slice(start, end=buffer.length)
Buffer.slice provides the same functionality as Array.prototype.slice. The slice is not a new buffer and it references a subset of the memory space. For example:
var puddle = frosty.slice(16, 19)
puddle.toString()
'?'
puddle.write("___")
3
frosty.toString("utf-8", 0, 19)
'Happy birthday! ___'
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Hotam Singh
Hotam has 1.5 years of experience in Node.JS. He has worked on Front End, JavaScript, Jquery, Backbone.JS, Database: MySQL, MongoDB. His hobbies are playing Sudoku/Puzzles and watching movies.