NODEjs lecture 3
NODEjs lecture 3
Introduction
Module:5
Contents
• Buffer
• Streams
Buffer
• Node provides Buffer class which provides instances to store raw data similar
to an array of integers but corresponds to a raw memory allocation outside
the V8 heap.
• Buffer class is a global class that can be accessed in an application without
importing the buffer module.
• Creating Buffers
• Node Buffer can be constructed in a variety of ways.
• Syntax
Following is the syntax of the method to write into a Node Buffer −
buf.write(string[, offset][, length][, encoding])
• Parameters
• Here is the description of the parameters used −
• string − This is the string data to be written to buffer.
• offset − This is the index of the buffer to start writing at. Default value is 0.
• length − This is the number of bytes to write. Defaults to buffer.length.
• encoding − Encoding to use. 'utf8' is the default encoding.
Buffer
• Return Value
• This method returns the number of octets written. If there is not enough
space in the buffer to fit the entire string, it will write a part of the string.
• Example
• Return Value
• This method decodes and returns a string from buffer data encoded
using the specified character set encoding.
Buffer
• Example
buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++)
{ buf[i] = i + 97; }
console.log( buf.toString('ascii'));
console.log( buf.toString('ascii',0,5));
console.log( buf.toString('utf8',0,5));
console.log( buf.toString(undefined,0,5));
• Return Value
• This method returns a Buffer instance.
Buffer
• Example
• Parameters
• Here is the description of the parameters used −
• otherBuffer − This is the other buffer which will be compared with buf
• Return Value
• Returns a number indicating whether it comes before or after or is the same
as the otherBuffer in sort order.
Buffer
• Example
• Parameters
• Here is the description of the parameters used −
• targetBuffer − Buffer object where buffer will be copied.
• targetStart − Number, Optional, Default: 0
• sourceStart − Number, Optional, Default: 0
• sourceEnd − Number, Optional, Default: buffer.length
• Return Value
• No return value. Copies data from a region of this buffer to a region in
the target buffer even if the target memory region overlaps with the
source. If undefined, the targetStart and sourceStart parameters
default to 0, while sourceEnd defaults to buffer.length.
Buffer
• Example
var buffer1 = new Buffer('ABC');
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log("buffer2 content: " + buffer2.toString());
• Parameters
• Here is the description of the parameters used −
• start − Number, Optional, Default: 0
• end − Number, Optional, Default: buffer.length
• Return Value
• Returns a new buffer which references the same memory as the old one, but
offset and cropped by the start (defaults to 0) and end (defaults to
buffer.length) indexes. Negative indexes start from the end of the buffer.
Buffer
• Example
var buffer1 = new Buffer('TutorialsPoint');
var buffer2 = buffer1.slice(0,9);
console.log("buffer2 content: " + buffer2.toString());
• Return Value
• Returns the size of a buffer in bytes.
streams
• What are Streams?
• Streams are objects that let you read data from a source or write
data to a destination in continuous fashion. In Node.js, there are
four types of streams −
• Readable − Stream which is used for read operation.
• Writable − Stream which is used for write operation.
• Duplex − Stream which can be used for both read and write
operation.
• Transform − A type of duplex stream where the output is computed
based on input.
streams
• Each type of Stream is an EventEmitter instance and throws several
events at different instance of times. For example, some of the
commonly used events are −
• data − This event is fired when there is data is available to read.
• end − This event is fired when there is no more data to read.
• error − This event is fired when there is any error receiving or writing
data.
• finish − This event is fired when all the data has been flushed to
underlying system.
streams
• Reading from a Stream : demo readstream.js
• Writing to a Stream : demo writestream.js
• Piping the Streams : demo pipe.js