SlideShare una empresa de Scribd logo
1 de 19
Web Frameworks
Node JS Buffers
1
Monica Deshmane(H.V.Desai College,Pune)
Topics
• Creating Buffer
• Writing buffer
• Read from buffers
• Concatinating buffers
• Compare buffers
• Copy buffers
• Buffer length
• Buffer slice
Monica Deshmane(H.V.Desai College,Pune) 2
Buffer
Monica Deshmane(H.V.Desai College,Pune) 3
• Buffer is a global object which behaves
like array of octets effectively letting
you represent binary data in javascript.
• Buffers/index.js
var buffer = new Buffer(“Aatish”);
console.log("buffer length: " + buffer.length);
• //6
• Run it as-
$ node index
Creating Buffers
• Node Buffer can be constructed in a
variety of ways.
• Method 1: Following is the syntax to create an
uninitiated Buffer of 10 octets −
var buf = new Buffer(10);
• Method 2 : Following is syntax to create Buffer from a
given array −
var buf = new Buffer([10, 20, 30, 40, 50]);
//buf length=5
• Method 3 : Following is the syntax to create a Buffer from a
given string and optionally encoding type −
var buf = new Buffer("Simply Easy Learning", "utf-8");
//buf length=20
• Though "utf8" is the default encoding, you can use any of
the following encodings "ascii", "utf8", "utf16le", "ucs2",
"base64" or "hex". Monica Deshmane(H.V.Desai College,Pune) 4
Writing to Buffers
Syntax:
buf.write(string[, offset][, length][, encoding])
• Parameters
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.
• Return Value
This method returns the number of octets written.
Monica Deshmane(H.V.Desai College,Pune) 5
Writing to Buffers
Example-
buf = new Buffer(256);
len = buf.write("Simply Easy Learning");
console.log("Octets written : "+ len);
output −
Octets written : 20
Monica Deshmane(H.V.Desai College,Pune) 6
Reading from Buffers
Syntax : −
buf.toString([encoding][, start][, end])
• Parameters
encoding − Encoding to use. 'utf8' is the default
encoding.
start − Beginning index to start reading, defaults to 0.
end − End index to end reading, defaults is complete
buffer.
• Return Value
This method decodes and returns a string from buffer
data encoded using the mentioned character set
encoding.
Monica Deshmane(H.V.Desai College,Pune)
7
Reading from Buffers
Example
buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++)
{ buf[i] = i + 97; }
console.log( buf.toString('ascii'));
// outputs: abcdefghijklmnopqrstuvwxyz
console.log( buf.toString('ascii',0,5));
// outputs: abcde
console.log( buf.toString('utf8',0,5));
// outputs: abcde
console.log( buf.toString(undefined,0,5));
// encoding defaults to 'utf8', outputs abcde
Monica Deshmane(H.V.Desai College,Pune)
8
Concatenate Buffers
• Syntax:
Buffer.concat(list of buffers[, totalLength])
• Parameters
list − Array List of Buffer objects to be
concatenated.
totalLength − This is the total length of the
buffers when concatenated.
• Return Value : This method returns a Buffer
instance.
Monica Deshmane(H.V.Desai College,Pune) 9
Concatenate Buffers
Example-
var buffer1 = new Buffer(“hello”);
var buffer2 = new Buffer(“world”);
var buffer3 = Buffer.concat([buffer1,buffer2]);
console.log("buffer3 content: " +
buffer3.toString());
buffer3 content: hello world
Monica Deshmane(H.V.Desai College,Pune) 10
Compare Buffers
• Syntax :
buf.compare(otherBuffer);
• otherBuffer − This is the other
buffer which will be compared
with buf
• Returns a number indicating
whether it comes before or after
or is the same as the otherBuffer
in sort order.
Monica Deshmane(H.V.Desai College,Pune) 11
Compare Buffers
Example-
var buffer1 = new Buffer('ABC');
var buffer2 = new Buffer('ABCD');
var result = buffer1.compare(buffer2);
if(result < 0) //-1
{ console.log(buffer1 +" comes before " + buffer2); }
else if(result === 0)
{ console.log(buffer1 +" is same as " + buffer2); }
else //1
{ console.log(buffer1 +" comes after " + buffer2); }
• output −
ABC comes before ABCD
Monica Deshmane(H.V.Desai College,Pune) 12
Copy Buffer
• Syntax :
buf.copy(targetBuffer[, targetStart]
[, sourceStart][, sourceEnd])
• Parameters
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.
Monica Deshmane(H.V.Desai College,Pune) 13
Copy Buffer
Example-
• var buffer1 = new Buffer('ABC');
//copy a buffer
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log("buffer2 content: " + buffer2.toString());
• output−
buffer2 content: ABC
Monica Deshmane(H.V.Desai College,Pune) 14
Slice Buffer
• Syntax
buf.slice([start][, end])
• Parameters
start − Number, Optional, Default: 0
end − Number, Optional, Default: buffer.length
• Returns a new buffer which references the same
memory as the old one,
Monica Deshmane(H.V.Desai College,Pune)
15
Slice Buffer
Example-
var buffer1 = new Buffer(“this is node JS”);
var buffer2 = buffer1.slice(0,7);
//slicing a buffer
console.log("buffer2 content: " + buffer2.toString());
• When the above program is executed, it produces the
following result −
buffer2 content: this is
Monica Deshmane(H.V.Desai College,Pune)
16
Buffer Length
• Syntax : Following is the syntax of the
method to get a size of a node
buffer in bytes −
buf.length;
• Returns the size of a buffer in bytes.
var buffer = new Buffer(“node”);
console.log("buffer length: " + buffer.length);
//length of the buffer
• output−
buffer length: 4
Monica Deshmane(H.V.Desai College,Pune)
17
Assignment..
• Write menu driven program or
performing operations on buffer.
1. Create
2. read
3. Write
4. Length
5. Copy
6. Slice
7. Compare
8. Concat
Monica Deshmane(H.V.Desai College,Pune) 18
19
Monica Deshmane(H.V.Desai College,Pune)
Questions?
What is Buffer?Write any 4 functions of
buffers in deep with example.
What is Buffer?
Which is default encoding type of buffer?

Más contenido relacionado

La actualidad más candente

Node.js Express
Node.js  ExpressNode.js  Express
Node.js ExpressEyal Vardi
 
Basic Concept of Node.js & NPM
Basic Concept of Node.js & NPMBasic Concept of Node.js & NPM
Basic Concept of Node.js & NPMBhargav Anadkat
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react formYao Nien Chung
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node jsAkshay Mathur
 
Node JS Crash Course
Node JS Crash CourseNode JS Crash Course
Node JS Crash CourseHaim Michael
 
CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media QueriesRuss Weakley
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 
JavaScript - Chapter 3 - Introduction
 JavaScript - Chapter 3 - Introduction JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 3 - IntroductionWebStackAcademy
 
Introduction to Bootstrap
Introduction to BootstrapIntroduction to Bootstrap
Introduction to BootstrapSeble Nigussie
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
introduction to Vue.js 3
introduction to Vue.js 3 introduction to Vue.js 3
introduction to Vue.js 3 ArezooKmn
 
Node Architecture and Getting Started with Express
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Expressjguerrero999
 
Laravel presentation
Laravel presentationLaravel presentation
Laravel presentationToufiq Mahmud
 

La actualidad más candente (20)

Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
jQuery Ajax
jQuery AjaxjQuery Ajax
jQuery Ajax
 
Basic Concept of Node.js & NPM
Basic Concept of Node.js & NPMBasic Concept of Node.js & NPM
Basic Concept of Node.js & NPM
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
 
Bootstrap 5 basic
Bootstrap 5 basicBootstrap 5 basic
Bootstrap 5 basic
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
Node JS Crash Course
Node JS Crash CourseNode JS Crash Course
Node JS Crash Course
 
CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media Queries
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
JavaScript - Chapter 3 - Introduction
 JavaScript - Chapter 3 - Introduction JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 3 - Introduction
 
Introduction to Bootstrap
Introduction to BootstrapIntroduction to Bootstrap
Introduction to Bootstrap
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
01 Php Introduction
01 Php Introduction01 Php Introduction
01 Php Introduction
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
introduction to Vue.js 3
introduction to Vue.js 3 introduction to Vue.js 3
introduction to Vue.js 3
 
Javascript
JavascriptJavascript
Javascript
 
Node Architecture and Getting Started with Express
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Express
 
Laravel presentation
Laravel presentationLaravel presentation
Laravel presentation
 

Similar a Node JS Buffers Guide: Creating, Writing, Reading, Comparing & More

Concurrency at the Database Layer
Concurrency at the Database Layer Concurrency at the Database Layer
Concurrency at the Database Layer mcwilson1
 
Native or External?
Native or External?Native or External?
Native or External?ESUG
 
NativeBoost
NativeBoostNativeBoost
NativeBoostESUG
 
SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++Uilian Ries
 
Offensive cyber security: Smashing the stack with Python
Offensive cyber security: Smashing the stack with PythonOffensive cyber security: Smashing the stack with Python
Offensive cyber security: Smashing the stack with PythonMalachi Jones
 
Tips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeTips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeKenneth Geisshirt
 
Instruction addressing and execution
Instruction addressing and executionInstruction addressing and execution
Instruction addressing and executionSilvia
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdfTigabu Yaya
 
Hacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitationHacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitationOWASP Hacker Thursday
 
Creating a Mesos python framework
Creating a Mesos python frameworkCreating a Mesos python framework
Creating a Mesos python frameworkOlivier Sallou
 
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don GriffinSenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don GriffinSencha
 
Programming Languages Implementation and Design. .docx
  Programming Languages Implementation and Design. .docx  Programming Languages Implementation and Design. .docx
Programming Languages Implementation and Design. .docxaryan532920
 
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...Charles Nutter
 
Machine learning on source code
Machine learning on source codeMachine learning on source code
Machine learning on source codesource{d}
 

Similar a Node JS Buffers Guide: Creating, Writing, Reading, Comparing & More (20)

NodeJs Modules.pdf
NodeJs Modules.pdfNodeJs Modules.pdf
NodeJs Modules.pdf
 
Concurrency at the Database Layer
Concurrency at the Database Layer Concurrency at the Database Layer
Concurrency at the Database Layer
 
Native or External?
Native or External?Native or External?
Native or External?
 
Blockchain - a simple implementation
Blockchain - a simple implementationBlockchain - a simple implementation
Blockchain - a simple implementation
 
Licão 14 debug script
Licão 14 debug scriptLicão 14 debug script
Licão 14 debug script
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
 
SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++
 
Offensive cyber security: Smashing the stack with Python
Offensive cyber security: Smashing the stack with PythonOffensive cyber security: Smashing the stack with Python
Offensive cyber security: Smashing the stack with Python
 
Tips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeTips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native code
 
Instruction addressing and execution
Instruction addressing and executionInstruction addressing and execution
Instruction addressing and execution
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
 
Hacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitationHacker Thursdays: An introduction to binary exploitation
Hacker Thursdays: An introduction to binary exploitation
 
JAVA NIO
JAVA NIOJAVA NIO
JAVA NIO
 
Why learn Internals?
Why learn Internals?Why learn Internals?
Why learn Internals?
 
Creating a Mesos python framework
Creating a Mesos python frameworkCreating a Mesos python framework
Creating a Mesos python framework
 
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don GriffinSenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
 
Programming Languages Implementation and Design. .docx
  Programming Languages Implementation and Design. .docx  Programming Languages Implementation and Design. .docx
Programming Languages Implementation and Design. .docx
 
Python
PythonPython
Python
 
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
 
Machine learning on source code
Machine learning on source codeMachine learning on source code
Machine learning on source code
 

Más de monikadeshmane

Más de monikadeshmane (19)

Intsllation & 1st program nodejs
Intsllation & 1st program nodejsIntsllation & 1st program nodejs
Intsllation & 1st program nodejs
 
Nodejs basics
Nodejs basicsNodejs basics
Nodejs basics
 
Chap 5 php files part-2
Chap 5 php files   part-2Chap 5 php files   part-2
Chap 5 php files part-2
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
 
Chap4 oop class (php) part 2
Chap4 oop class (php) part 2Chap4 oop class (php) part 2
Chap4 oop class (php) part 2
 
Chap4 oop class (php) part 1
Chap4 oop class (php) part 1Chap4 oop class (php) part 1
Chap4 oop class (php) part 1
 
Chap 3php array part4
Chap 3php array part4Chap 3php array part4
Chap 3php array part4
 
Chap 3php array part 3
Chap 3php array part 3Chap 3php array part 3
Chap 3php array part 3
 
Chap 3php array part 2
Chap 3php array part 2Chap 3php array part 2
Chap 3php array part 2
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
PHP function
PHP functionPHP function
PHP function
 
php string part 4
php string part 4php string part 4
php string part 4
 
php string part 3
php string part 3php string part 3
php string part 3
 
php string-part 2
php string-part 2php string-part 2
php string-part 2
 
PHP string-part 1
PHP string-part 1PHP string-part 1
PHP string-part 1
 
java script
java scriptjava script
java script
 
ip1clientserver model
 ip1clientserver model ip1clientserver model
ip1clientserver model
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
 
Chap1introppt1php basic
Chap1introppt1php basicChap1introppt1php basic
Chap1introppt1php basic
 

Último

Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 

Último (20)

Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 

Node JS Buffers Guide: Creating, Writing, Reading, Comparing & More

  • 1. Web Frameworks Node JS Buffers 1 Monica Deshmane(H.V.Desai College,Pune)
  • 2. Topics • Creating Buffer • Writing buffer • Read from buffers • Concatinating buffers • Compare buffers • Copy buffers • Buffer length • Buffer slice Monica Deshmane(H.V.Desai College,Pune) 2
  • 3. Buffer Monica Deshmane(H.V.Desai College,Pune) 3 • Buffer is a global object which behaves like array of octets effectively letting you represent binary data in javascript. • Buffers/index.js var buffer = new Buffer(“Aatish”); console.log("buffer length: " + buffer.length); • //6 • Run it as- $ node index
  • 4. Creating Buffers • Node Buffer can be constructed in a variety of ways. • Method 1: Following is the syntax to create an uninitiated Buffer of 10 octets − var buf = new Buffer(10); • Method 2 : Following is syntax to create Buffer from a given array − var buf = new Buffer([10, 20, 30, 40, 50]); //buf length=5 • Method 3 : Following is the syntax to create a Buffer from a given string and optionally encoding type − var buf = new Buffer("Simply Easy Learning", "utf-8"); //buf length=20 • Though "utf8" is the default encoding, you can use any of the following encodings "ascii", "utf8", "utf16le", "ucs2", "base64" or "hex". Monica Deshmane(H.V.Desai College,Pune) 4
  • 5. Writing to Buffers Syntax: buf.write(string[, offset][, length][, encoding]) • Parameters 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. • Return Value This method returns the number of octets written. Monica Deshmane(H.V.Desai College,Pune) 5
  • 6. Writing to Buffers Example- buf = new Buffer(256); len = buf.write("Simply Easy Learning"); console.log("Octets written : "+ len); output − Octets written : 20 Monica Deshmane(H.V.Desai College,Pune) 6
  • 7. Reading from Buffers Syntax : − buf.toString([encoding][, start][, end]) • Parameters encoding − Encoding to use. 'utf8' is the default encoding. start − Beginning index to start reading, defaults to 0. end − End index to end reading, defaults is complete buffer. • Return Value This method decodes and returns a string from buffer data encoded using the mentioned character set encoding. Monica Deshmane(H.V.Desai College,Pune) 7
  • 8. Reading from Buffers Example buf = new Buffer(26); for (var i = 0 ; i < 26 ; i++) { buf[i] = i + 97; } console.log( buf.toString('ascii')); // outputs: abcdefghijklmnopqrstuvwxyz console.log( buf.toString('ascii',0,5)); // outputs: abcde console.log( buf.toString('utf8',0,5)); // outputs: abcde console.log( buf.toString(undefined,0,5)); // encoding defaults to 'utf8', outputs abcde Monica Deshmane(H.V.Desai College,Pune) 8
  • 9. Concatenate Buffers • Syntax: Buffer.concat(list of buffers[, totalLength]) • Parameters list − Array List of Buffer objects to be concatenated. totalLength − This is the total length of the buffers when concatenated. • Return Value : This method returns a Buffer instance. Monica Deshmane(H.V.Desai College,Pune) 9
  • 10. Concatenate Buffers Example- var buffer1 = new Buffer(“hello”); var buffer2 = new Buffer(“world”); var buffer3 = Buffer.concat([buffer1,buffer2]); console.log("buffer3 content: " + buffer3.toString()); buffer3 content: hello world Monica Deshmane(H.V.Desai College,Pune) 10
  • 11. Compare Buffers • Syntax : buf.compare(otherBuffer); • otherBuffer − This is the other buffer which will be compared with buf • Returns a number indicating whether it comes before or after or is the same as the otherBuffer in sort order. Monica Deshmane(H.V.Desai College,Pune) 11
  • 12. Compare Buffers Example- var buffer1 = new Buffer('ABC'); var buffer2 = new Buffer('ABCD'); var result = buffer1.compare(buffer2); if(result < 0) //-1 { console.log(buffer1 +" comes before " + buffer2); } else if(result === 0) { console.log(buffer1 +" is same as " + buffer2); } else //1 { console.log(buffer1 +" comes after " + buffer2); } • output − ABC comes before ABCD Monica Deshmane(H.V.Desai College,Pune) 12
  • 13. Copy Buffer • Syntax : buf.copy(targetBuffer[, targetStart] [, sourceStart][, sourceEnd]) • Parameters 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. Monica Deshmane(H.V.Desai College,Pune) 13
  • 14. Copy Buffer Example- • var buffer1 = new Buffer('ABC'); //copy a buffer var buffer2 = new Buffer(3); buffer1.copy(buffer2); console.log("buffer2 content: " + buffer2.toString()); • output− buffer2 content: ABC Monica Deshmane(H.V.Desai College,Pune) 14
  • 15. Slice Buffer • Syntax buf.slice([start][, end]) • Parameters start − Number, Optional, Default: 0 end − Number, Optional, Default: buffer.length • Returns a new buffer which references the same memory as the old one, Monica Deshmane(H.V.Desai College,Pune) 15
  • 16. Slice Buffer Example- var buffer1 = new Buffer(“this is node JS”); var buffer2 = buffer1.slice(0,7); //slicing a buffer console.log("buffer2 content: " + buffer2.toString()); • When the above program is executed, it produces the following result − buffer2 content: this is Monica Deshmane(H.V.Desai College,Pune) 16
  • 17. Buffer Length • Syntax : Following is the syntax of the method to get a size of a node buffer in bytes − buf.length; • Returns the size of a buffer in bytes. var buffer = new Buffer(“node”); console.log("buffer length: " + buffer.length); //length of the buffer • output− buffer length: 4 Monica Deshmane(H.V.Desai College,Pune) 17
  • 18. Assignment.. • Write menu driven program or performing operations on buffer. 1. Create 2. read 3. Write 4. Length 5. Copy 6. Slice 7. Compare 8. Concat Monica Deshmane(H.V.Desai College,Pune) 18
  • 19. 19 Monica Deshmane(H.V.Desai College,Pune) Questions? What is Buffer?Write any 4 functions of buffers in deep with example. What is Buffer? Which is default encoding type of buffer?