নোড.জেএস : Node.js – বাফার

রিদওয়ান বিন শামীম

 

পিউর জাভাস্ক্রিপ্ট ইউনিকোড বান্ধব কিন্তু বাইনারি ডাটায় তেমন নিখুঁত নয়। টিসিপি সিস্টেম বা ফাইল স্ট্রিম নিয়ে কাজ করার সময় অক্টেট স্ট্রিম নিয়ন্ত্রণের প্রয়োজন হয়। নড বাফার ক্লাস প্রভাইড করে যা কিনা পূর্ণসংখ্যার অ্যারির সদৃশ ‘র’ ডাটা সংরক্ষণের সুযোগ দেয় কিন্তু ভি৮ হিপের(V8 heap)বাইরে ‘র’ মেমোরি এলোকেশনের সাথে সুসংহত থাকে।

বাফার ক্লাস গ্লোবাল ক্লাস, এটি বাফার মডিউল ইমপোর্ট করা ছাড়াই এপ্লিকেশনে ব্যবহার করা সম্ভব।

বাফার তৈরি করা

কয়েকটি উপায়ে বাফার তৈরি করা যেতে পারে,

প্রথম পদ্ধতিঃ ১০ অক্টেটের আনইনিসিয়েটেড বাফার তৈরির সিনট্যাক্স,


var buf = new Buffer(10);


 

দ্বিতীয় পদ্ধতিঃ প্রদত্ত অ্যারি থেকে বাফার তৈরির সিনট্যাক্স,


var buf = new Buffer([10, 20, 30, 40, 50]);


 

তৃতীয় পদ্ধতিঃ প্রদত্ত স্ট্রিং থেকে বাফার তৈরির সিনট্যাক্স ও অপশনাল এনকোডিং টাইপ,


var buf = new Buffer(“Simply Easy Learning”, “utf-8”);


 

যদিও ডিফল্ট এনকোডিং হল “utf8” , আমরা “ascii”, “utf8”, “utf16le”, “ucs2”, “base64” বা “hex” ইত্যাদিও ব্যবহার করতে পারি।

বাফারে লেখা

নড বাফারে লেখার জন্য সিনট্যাক্স,


buf.write(string[, offset][, length][, encoding])


 

প্যারামিটারঃ সংশ্লিষ্ট প্যারামিটারগুলো হল,

  • স্ট্রিং
  • অফসেট
  • লেন্থ
  • এনকোডিং

রিটার্ন ভ্যালু

এই প্রক্রিয়া লিখিত অক্টেটের নাম্বার রিটার্ন করায়। বাফারে পুরো স্ট্রিংএর জায়গা না থাকলে আংশিক লেখা হয়। যেমন,


buf = new Buffer(256);
len = buf.write(“Simply Easy Learning”);

console.log(“Octets written : “+ len);


 

এটি সঠিকভাবে সম্পাদিত হলে তা যে ফলাফল দেখাবে তা হল,


Octets written : 20


 

বাফার থেকে পড়াঃ এজন্য প্রয়োজনীয় সিনট্যাক্স হল,
buf.toString([encoding][, start][, end])

এর প্যারামিটারগুলো হল,

  • এনকোডিং
  • স্টার্ট
  • এন্ড

উদাহরণ,


 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
 এটি সঠিকভাবে সম্পাদিত হলে তা যে ফলাফল দেখাবে তা হল,
 abcdefghijklmnopqrstuvwxyz
 abcde
 abcde
 abcde

 

বাফার থেকে জেএসওএনে রূপান্তরের সিনট্যাক্স,


buf.toJSON()


 

এটির উদাহরণ,


var buf = new Buffer('Simply Easy Learning');
 var json = buf.toJSON(buf);
console.log(json);

 

এটি সঠিকভাবে সম্পাদিত হলে তা যে ফলাফল দেখাবে তা হল,


[ 83, 105, 109, 112, 108, 121, 32, 69, 97, 115, 121, 32, 76, 101, 97, 114, 110, 105, 110, 103 ]


 

কনকাটেনাট বাফারঃ কনকাটেনাট নড বাফার থেকে সিঙ্গেল নড বাফারে রূপান্তর করার সিনট্যাক্স,


Buffer.concat(list[, totalLength])


 

এর প্যারামিটারগুলো হল,

  • লিস্ট
  • টোটাললেন্থ

রিটার্ন ভ্যালুঃ বাফার ইন্সটেন্স রিটার্ন করায়।
উদাহরণ,


var buffer1 = new Buffer('TutorialsPoint ');
 var buffer2 = new Buffer('Simply Easy Learning');
 var buffer3 = Buffer.concat([buffer1,buffer2]);
 console.log("buffer3 content: " + buffer3.toString());

 

এটি সঠিকভাবে সম্পাদিত হলে তা যে ফলাফল দেখাবে তা হল,


buffer3 content: TutorialsPoint Simply Easy Learning

 

বাফার তুলনাঃ এর জন্য ব্যবহৃত সিনট্যাক্স,


buf.compare(otherBuffer);

 

উদাহরণ,


var buffer1 = new Buffer('ABC');
 var buffer2 = new Buffer('ABCD');
 var result = buffer1.compare(buffer2);
if(result < 0) {
 console.log(buffer1 +" comes before " + buffer2);
 }else if(result == 0){
 console.log(buffer1 +" is same as " + buffer2);
 }else {
 console.log(buffer1 +" comes after " + buffer2);
 }

 

এটি সঠিকভাবে সম্পাদিত হলে তা যে ফলাফল দেখাবে তা হল,


ABC comes before ABCD


 

বাফার কপি করাঃ এর সিনট্যাক্স হল,


buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])


 

এর প্যারামিটারগুলো হল,

  • টার্গেট বাফার
  • টার্গেট স্টার্ট
  • সোর্স স্টার্ট
  • সোর্স এন্ড

উদাহরণ,


var buffer1 = new Buffer('ABC');
 //copy a buffer
 var buffer2 = new Buffer(3);
 buffer1.copy(buffer2);
 console.log("buffer2 content: " + buffer2.toString());

 

সঠিকভাবে সম্পাদিত হলে এটি ফলাফল দেখাবে,


buffer2 content: ABC

 

স্লাইস বাফারঃ এর সিনট্যাক্স হল,


buf.slice([start][, end])

 

প্যারামিটারঃ এর প্যারামিটার কোল,

  • স্টার্ট
  • এন্ড

উদাহরণ,


var buffer1 = new Buffer('TutorialsPoint');
 //slicing a buffer
 var buffer2 = buffer1.slice(0,9);
 console.log("buffer2 content: " + buffer2.toString());

 

সঠিকভাবে সম্পাদিত হলে এটি ফলাফল দেখাবে,


buffer2 content: Tutorials


 

বাফার লেন্থঃ এর জন্য আমাদের যে সিনট্যাক্স ব্যবহার করতে হবে তা হল,


buf.length;


 

উদাহরণ,


var buffer = new Buffer(‘TutorialsPoint’);
//length of the buffer
console.log(“buffer length: ” + buffer.length);


 

সঠিকভাবে সম্পাদিত হলে এটি ফলাফল দেখাবে,


buffer length: 14


 

রেফারেন্স

নড জেএসে প্রাপ্ত বাফার মডিউলের রেফারেন্স নিচে দেয়া হল,

SN মেথড

  1. new Buffer(size)
  2. new Buffer(buffer)
  3. new Buffer(str[, encoding])
  4. buf.length
  5. buf.write(string[, offset][, length][, encoding])
  6. buf.writeUIntLE(value, offset, byteLength[, noAssert])
  7. buf.writeUIntBE(value, offset, byteLength[, noAssert])
  8. buf.writeIntLE(value, offset, byteLength[, noAssert])
  9. buf.writeIntBE(value, offset, byteLength[, noAssert])
  10. buf.readUIntLE(offset, byteLength[, noAssert])
  11. buf.readUIntBE(offset, byteLength[, noAssert])
  12. buf.readIntLE(offset, byteLength[, noAssert])
  13. buf.readIntBE(offset, byteLength[, noAssert])
  14. buf.toString([encoding][, start][, end])
  15. buf.toJSON()
  16. buf[index]
  17. buf.equals(otherBuffer)
  18. buf.compare(otherBuffer)
  19. buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])
  20. buf.slice([start][, end])
  21. buf.readUInt8(offset[, noAssert])
  22. buf.readUInt16LE(offset[, noAssert])
  23. buf.readUInt16BE(offset[, noAssert])
  24. buf.readUInt32LE(offset[, noAssert])
  25. buf.readUInt32BE(offset[, noAssert])
  26. buf.readInt8(offset[, noAssert])
  27. buf.readInt16LE(offset[, noAssert])
  28. buf.readInt16BE(offset[, noAssert])
  29. buf.readInt32LE(offset[, noAssert])
  30. buf.readInt32BE(offset[, noAssert])
  31. buf.readFloatLE(offset[, noAssert])
  32. buf.readFloatBE(offset[, noAssert])
  33. buf.readDoubleLE(offset[, noAssert])
  34. buf.readDoubleBE(offset[, noAssert])
  35. buf.writeUInt8(value, offset[, noAssert])
  36. buf.writeUInt16LE(value, offset[, noAssert])
  37. buf.writeUInt16BE(value, offset[, noAssert])
  38. buf.writeUInt32LE(value, offset[, noAssert])
  39. buf.writeUInt32BE(value, offset[, noAssert])
  40. buf.writeInt8(value, offset[, noAssert])
  41. buf.writeInt16LE(value, offset[, noAssert])
  42. buf.writeInt16BE(value, offset[, noAssert])
  43. buf.writeInt32LE(value, offset[, noAssert])
  44. buf.writeInt32BE(value, offset[, noAssert])
  45. buf.writeFloatLE(value, offset[, noAssert])
  46. buf.writeFloatBE(value, offset[, noAssert])
  47. buf.writeDoubleLE(value, offset[, noAssert])
  48. buf.writeDoubleBE(value, offset[, noAssert])
  49. buf.fill(value[, offset][, end])

ক্লাস মেথড

SN মেথড

  1.   Buffer.isEncoding(encoding)
  2. Buffer.isBuffer(obj)
  3. Buffer.byteLength(string[, encoding])
  4. Buffer.concat(list[, totalLength])
  5. Buffer.compare(buf1, buf2)

 

Print minimum, maximum, and sum of the valarray

/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <valarray>
using namespace std;
 
// print valarray
template <class T>
void printValarray (const valarray<T>& va)
{
    for (int i=0; i<va.size(); i++) {
        cout << va[i] << ' ';
    }
    cout << endl;
}
 
int main()
{
    // define two valarrays with ten elements
    valarray<double> va1(10), va2(10);
 
    // assign values 0.0, 1.1, up to 9.9 to the first valarray
    for (int i=0; i<10; i++) {
        va1[i] = i * 1.1;
    }
 
    // assign -1 to all elements of the second valarray
    va2 = -1;
 
    // print both valarrays
    printValarray(va1);
    printValarray(va2);
 
    // print minimum, maximum, and sum of the first valarray
    cout << "min(): " << va1.min() << endl;
    cout << "max(): " << va1.max() << endl;
    cout << "sum(): " << va1.sum() << endl;
 
    // assign values of the first to the second valarray
    va2 = va1;
 
    // remove all elements of the first valarray
    va1.resize(0);
 
    // print both valarrays again
    printValarray(va1);
    printValarray(va2);
}
 
/*
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
min(): 0
max(): 9.9
sum(): 49.5
 
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
 
 */

valarray slice

/* The following code example is taken from the book * “The C++ Standard Library – A Tutorial and Reference” * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided “as is” without express or implied * warranty, and with no claim as to its suitability for any purpose. */

#include <iostream>
#include <valarray>
using namespace std; 

// print valarray line-by-line
template<class T>
void printValarray (const valarray<T>& va, int num)
{
   for (int i=0; i<va.size()/num; ++i) {
       for (int j=0; j<num; ++j) {
           cout << va[i*num+j] << ' ';
       }
     cout << endl;
   }
   cout << endl;
} 

int main()
{
   /* valarray with 12 elements
     * - four rows
     * - three columns
     */
   valarray<double> va(12); 

   // fill valarray with values
   for (int i=0; i<12; i++) {
       va[i] = i;
   } 

   printValarray (va, 3);
   // first column = second column raised to the third column
   va[slice(0,4,3)] = pow (valarray<double>(va[slice(1,4,3)]),
                           valarray<double>(va[slice(2,4,3)])); 

   printValarray (va, 3); 
   // create valarray with three times the third element of va
   valarray<double> vb(va[slice(2,4,0)]);
 
   // multiply the third column by the elements of vb
   va[slice(2,4,3)] *= vb;
   printValarray (va, 3); 
   // print the square root of the elements in the second row
   //printValarray (sqrt(valarray<double>(va[slice(3,3,1)])));
   // double the elements in the third row
   va[slice(2,4,3)] = valarray<double>(va[slice(2,4,3)]) * 2.0;
   printValarray (va, 3);
}
/*
0 1 2
3 4 5
6 7 8
9 10 11
1 1 2
1024 4 5
5.7648e+006 7 8
1e+011 10 11
1 1 4
1024 4 10
5.7648e+006 7 16
1e+011 10 22
1 1 8
1024 4 20
5.7648e+006 7 32
1e+011 10 44
*/

 

Call member function for each element in vector

/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
//#define mem_fun1 mem_fun
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
class Person {
private:
   std::string name;
public:
   //...
   void print () const {
       std::cout << name << std::endl;
   }
   void printWithPrefix (std::string prefix) const {
       std::cout << prefix << name << std::endl;
   }
};
void foo (const std::vector<Person>& coll)
{
   using std::for_each;
   using std::bind2nd;
   using std::mem_fun_ref;
   // call member function print() for each element
   for_each (coll.begin(), coll.end(), mem_fun_ref(&Person::print));
   // call member function printWithPrefix() for each element
   // - "person: " is passed as an argument to the member function
   for_each (coll.begin(), coll.end(),bind2nd(mem_fun_ref(&Person::printWithPrefix),"person: "));
}

void ptrfoo (const std::vector<Person*>& coll)
                                   // ^^^ pointer !
{
   using std::for_each;
   using std::bind2nd;
   using std::mem_fun;
   // call member function print() for each referred object
   for_each (coll.begin(), coll.end(),
             mem_fun(&Person::print));
   // call member function printWithPrefix() for each referred object
   // - "person: " is passed as an argument to the member function
   for_each (coll.begin(), coll.end(),bind2nd(mem_fun(&Person::printWithPrefix),"person: "));
} 
 
int main()
{
   std::vector<Person> coll(5);
   foo(coll); 
   std::vector<Person*> coll2;
   coll2.push_back(new Person);
   ptrfoo(coll2);
} 
/*
person:
person:
person:
person:
person:
person: 

*/


						
						
						
		

Use bitset with enum together

/* The following code example is taken from the book
  * "The C++ Standard Library - A Tutorial and Reference"
  * by Nicolai M. Josuttis, Addison-Wesley, 1999
  *
  * (C) Copyright Nicolai M. Josuttis 1999.
  * Permission to copy, use, modify, sell and distribute this software
  * is granted provided this copyright notice appears in all copies.
  * This software is provided "as is" without express or implied
  * warranty, and with no claim as to its suitability for any purpose.
  */
 #include <bitset>
 #include <iostream>
 using namespace std;
 
 int main()
 {
     /* enumeration type for the bits
      * - each bit represents a color
      */
     enum Color { red, yellow, green, blue, white, black, //...,
                  numColors };
 
     // create bitset for all bits/colors
     bitset<numColors> usedColors;
 
     // set bits for two colors
     usedColors.set(red);
     usedColors.set(blue);
 
     // print some bitset data
     cout << "bitfield of used colors:   " << usedColors
          << endl;
     cout << "number   of used colors:   " << usedColors.count()
          << endl;
     cout << "bitfield of unused colors: " << ~usedColors
          << endl;
 
     // if any color is used
     if (usedColors.any()) {
         // loop over all colors
         for (int c = 0; c < numColors; ++c) {
             // if the actual color is used
             if (usedColors[(Color)c]) {
                 //...
             }
         }
     }
 }
 
 /* 
 bitfield of used colors:   001001
 number   of used colors:   2
 bitfield of unused colors: 110110
 
  */

Transform binary representation into integral number using bitset

 /* The following code example is taken from the book
  * "The C++ Standard Library - A Tutorial and Reference"
  * by Nicolai M. Josuttis, Addison-Wesley, 1999
  *
  * (C) Copyright Nicolai M. Josuttis 1999.
  * Permission to copy, use, modify, sell and distribute this software
  * is granted provided this copyright notice appears in all copies.
  * This software is provided "as is" without express or implied
  * warranty, and with no claim as to its suitability for any purpose.
  */
 #include <bitset>
 #include <iostream>
 #include <string>
 #include <limits>
 using namespace std;
 
 int main()
 {
     /* print some numbers in binary representation
      */
     cout << "267 as binary short:     "
          << bitset<numeric_limits<unsigned short>::digits>(267)
          << endl;
 
     cout << "267 as binary long:      "
          << bitset<numeric_limits<unsigned long>::digits>(267)
          << endl;
 
     cout << "10,000,000 with 24 bits: "
          << bitset<24>(1e7) << endl;
 
     /* transform binary representation into integral number
      */
     cout << "\"1000101011\" as number:  "
          << bitset<100>(string("1000101011")).to_ulong() << endl;
 }
 
  /* 
 267 as binary short:     0000000100001011
 267 as binary long:      00000000000000000000000100001011
 10,000,000 with 24 bits: 100110001001011010000000
 1000101011" as number:  555
 
  */       
    // create a bitset that is 8 bits long bitset<8> bs; // display that bitset for( int i = (int) bs.size()-1; i >= 0; i-- ) { cout << bs[i] << " "; } cout << endl; // create a bitset out of a number bitset<8> bs2( (long) 131 ); // display that bitset, too for( int i = (int) bs2.size()-1; i >= 0; i-- ) { cout << bs2[i] << " "; } cout << endl;

Comparisons among Dot Net Based CMS solutions

Comparisons among Dot Net Based CMS solutions

Brief Overview on Kentico CMS

Brief Overview on Kentico CMS

Kentico Hosted Trial Kentico Administrator Backend Overview

Kentico Hosted Trial Kentico Administrator Backend Overview

নোড . জে এস । Node.js – ইভেন্ট ইমিটার (Event Emitter)

নড জেএসঃ ইভেন্ট ইমিটার
রিদওয়ান বিন শামীম

নড ইমিট ইভেন্টের অনেক অবজেক্ট যেমন net.Server এর সাথে কোনও সদৃশ পিয়ার যুক্ত হলে কোনও ইভেন্টকে ইমিট করে। fs.readStream কোনও ইভেন্টকে ইমিট করে যখন এর ফাইল খোলা থাকে। events.EventEmitter সব ইভেন্ট ইমিট হওয়ার উদাহরণ।

ইভেন্ট ইমিটারের শ্রেণীঃ ইভেন্ট ইমিটারের শ্রেণী events module তে ব্যপিত থাকে।

নিচে উদাহরণ দেয়া হল,

// Import events module
var events = require(‘events’);
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

ইভেন্ট ইমিটারের কোনও পর্যায়ে ভুল দেখা দিলে ‘error’ ইভেন্ট দেখানো হয়। নতুন লিসনার যোগ হলে ‘newListener’ ইভেন্ট আর কোনও লিসনার মুছে ফেললে ‘removeListener’ ইভেন্ট প্রদর্শিত হয়। ইভেন্ট ইমিটার on ও emit. এর মত অনেক বৈশিষ্ট্য ধারণ করে।

প্রক্রিয়াঃ

S.N. প্রক্রিয়া
1 addListener(event, listener)

2 on(event, listener)

3 once(event, listener)

4 removeListener(event, listener)

5 removeAllListeners([event])

6 setMaxListeners(n)

7 listeners(event)

8 emit(event, [arg1], [arg2], […])

ক্লাস মেথডঃ
S.N. প্রক্রিয়া
1 listenerCount(emitter, event)

ইভেন্টসঃ
S.No. ইভেন্ট
1 newListener
• event – ইভেন্ট নেমের স্ট্রিং
• listener – ইভেন্ট হ্যান্ডেলার ফাংশন
2 removeListener
• event – ইভেন্ট নেমের স্ট্রিং
• listener – ইভেন্ট হ্যান্ডেলার ফাংশন

উদাহরণঃ নিচের Node.js কোড দ্বারা main.js নামের ফাইল তৈরি করি,

var events = require(‘events’);
var eventEmitter = new events.EventEmitter();

// listener #1
var listner1 = function listner1() {
console.log(‘listner1 executed.’);
}

// listener #2
var listner2 = function listner2() {
console.log(‘listner2 executed.’);
}

// Bind the connection event with the listner1 function
eventEmitter.addListener(‘connection’, listner1);

// Bind the connection event with the listner2 function
eventEmitter.on(‘connection’, listner2);

var eventListeners = require(‘events’).EventEmitter.listenerCount(eventEmitter,’connection’);
console.log(eventListeners + ” Listner(s) listening to connection event”);

// Fire the connection event
eventEmitter.emit(‘connection’);

// Remove the binding of listner1 function
eventEmitter.removeListener(‘connection’, listner1);
console.log(“Listner1 will not listen now.”);

// Fire the connection event
eventEmitter.emit(‘connection’);

eventListeners = require(‘events’).EventEmitter.listenerCount(eventEmitter,’connection’);
console.log(eventListeners + ” Listner(s) listening to connection event”);

console.log(“Program Ended.”);

এখন main.js রান করালে,

$ node main.js

আউটপুট ভেরিফাই করে দেখতে পারি,

2 Listner(s) listening to connection event
listner1 executed.
listner2 executed.
Listner1 will not listen now.
listner2 executed.
1 Listner(s) listening to connection event
Program Ended.

তথ্যসূত্রঃ http://www.tutorialspoint.com/nodejs/nodejs_event_emitter.htm