{"id":77,"date":"2019-09-22T13:34:17","date_gmt":"2019-09-22T13:34:17","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/nodejs\/?p=77"},"modified":"2019-09-22T13:34:17","modified_gmt":"2019-09-22T13:34:17","slug":"working-with-buffers","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/nodejs\/working-with-buffers\/","title":{"rendered":"Working with Buffers"},"content":{"rendered":"<p>In this tutorial, we would cover Buffers in Node.js under the following sub-topics:<\/p>\n<ol>\n<li><a href=\"#t1\">Introduction to Buffers<\/a><\/li>\n<li><a href=\"#t2\">How to Create Buffers<\/a><\/li>\n<li><a href=\"#t3\">Writing to Buffers<\/a><\/li>\n<li><a href=\"#t4\">Reading from Buffers<\/a><\/li>\n<li><a href=\"#t5\">Converting Buffers To JSON<\/a><\/li>\n<li><a href=\"#t6\">Concatenating Buffers<\/a><\/li>\n<li><a href=\"#t7\">Comparing Buffers<\/a><\/li>\n<li><a href=\"#t8\">Copying Buffers<\/a><\/li>\n<li><a href=\"#t9\">Slicing Buffers<\/a><\/li>\n<li><a href=\"#t10\">Other Buffer Class Methods<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong id=\"t1\">1. Introduction to Buffers<\/strong><\/p>\n<p>The buffer class is provided by Node.js to data from streams. For instance, streams of data from file system or from network.<\/p>\n<p>This is raw data similar to an array of integers. However, they corresponds to a raw memory allocation that is outside the<a href=\"https:\/\/www.youtube.com\/watch?v=_73HMegzpqM\" target=\"_blank\" rel=\"noopener noreferrer\"> V8<\/a> memory heap. <a href=\"https:\/\/www.youtube.com\/watch?v=_73HMegzpqM\" target=\"_blank\" rel=\"noopener noreferrer\">Learn about V8 here<\/a>.<\/p>\n<p>The Buffer class in Node.js is a global class. I can be accessed in your application without having to import the buffer module.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. How to Create Buffers<\/strong><\/p>\n<p>Some of the methods of creating a buffer is given below<\/p>\n<p>The code below creates a buffer of 10 octets that is uninitialized<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> mybuffer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Buffer(<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Here, a buffer is created from an array<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> mybuffer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Buffer([<span style=\"color: #0000dd; font-weight: bold;\">20<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">30<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">40<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">50<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">60<\/span>]);\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally, in this code, a buffer is created from a string. You can pass the encoding as optional argument<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> mybuffer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Buffer(<span style=\"background-color: #fff0f0;\">\"The Tech Pro\"<\/span>, <span style=\"background-color: #fff0f0;\">\"utf-8\"<\/span>);\r\n<\/pre>\n<p>Other encoding can be used as well. For example, &#8220;ascii&#8221;, &#8220;utf16e&#8221;, &#8220;hex&#8221; and &#8220;base64&#8221;.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t3\">3. Writing to Buffers<\/strong><\/p>\n<p>Once a buffer is created, then you can write to it. The syntax for writing to buffer is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mybuffer.write(string[, offset][, length][, encoding]);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The parameters to the write() method are explained as follows:<\/p>\n<ul class=\"list\">\n<li><b>string<\/b> \u2212 The string data that is to be written to buffer.<\/li>\n<li><b>offset<\/b> \u2212 The index of the buffer from where to start writing at. The default value is 0.<\/li>\n<li><b>length<\/b> \u2212 The number of bytes to write into the buffer. Defaults to the length of the buffer, buffer.length.<\/li>\n<li><b>encoding<\/b> \u2212 The encoding to use. The default encoding is\u00a0&#8216;utf8&#8217; is.<\/li>\n<\/ul>\n<p>The write method() returns the number of octets written. If there are no enough space in the buffer, then only a part of the string is written.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example\u00a0 of Writing to Buffer<\/strong><\/p>\n<p>The code below writes a string to a buffer and outputs number of octets written.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mybuffer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Buffer(<span style=\"color: #0000dd; font-weight: bold;\">256<\/span>);\r\nwritten <span style=\"color: #333333;\">=<\/span> mybuffer.write(<span style=\"background-color: #fff0f0;\">\"The Tech Pro\"<\/span>);\r\n\r\nconsole.log(<span style=\"background-color: #fff0f0;\">\"Number of Octets written : \"<\/span><span style=\"color: #333333;\">+<\/span>  written);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"t4\">4. Reading from Buffers<\/strong><\/p>\n<p>You can also read from a buffer using the syntax below.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mybuffer.toString([encoding][, start][, end])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The parameters are described as follows:<\/p>\n<ul>\n<li><b>encoding<\/b> \u2212 The encoding to use. &#8216;utf8&#8217; is the default.<\/li>\n<li><b>start<\/b> \u2212 The beginning index to start reading from, defaults is 0.<\/li>\n<li><b>end<\/b> \u2212 End index to end reading, defaults is the complete buffer.<\/li>\n<\/ul>\n<p>The return value of the read method is a string form the buffer encoded with the given character encoding<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mybuffer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Buffer(<span style=\"color: #0000dd; font-weight: bold;\">26<\/span>);\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> (<span style=\"color: #008800; font-weight: bold;\">var<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> ; i <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">26<\/span> ; i<span style=\"color: #333333;\">++<\/span>) {\r\n    mybuffer[i] <span style=\"color: #333333;\">=<\/span> i <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">97<\/span>;\r\n}\r\n\r\nconsole.log( mybuffer.toString(<span style=\"background-color: #fff0f0;\">'ascii'<\/span>));       <span style=\"color: #888888;\">\/\/ output: abcdefghijklmnopqrstuvwxyz<\/span>\r\nconsole.log( mybuffer.toString(<span style=\"background-color: #fff0f0;\">'ascii'<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>));   <span style=\"color: #888888;\">\/\/ output: abcde<\/span>\r\nconsole.log( mybuffer.toString(<span style=\"background-color: #fff0f0;\">'utf8'<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>));    <span style=\"color: #888888;\">\/\/ output: abcde<\/span>\r\nconsole.log( mybuffer.toString(<span style=\"color: #008800; font-weight: bold;\">undefined<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>)); <span style=\"color: #888888;\">\/\/ encoding defaults to 'utf8', outputs abcde<\/span>\r\n<\/pre>\n<p>I recommend you run the code to see the outputs.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t5\">5. Converting Buffer to JSON<\/strong><\/p>\n<p>To convert buffer to json simply use the <strong><em>toJSON()<\/em> <\/strong>method of the buffer object. This is as given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">mybuffer.toJSON()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"t6>6. Concatenating Buffers<\/strong><\/p>\n<p>You can concatenate a two or more buffers using the <strong><em>concat()<\/em><\/strong> class method. The buffers to be concatenated are provided as an array list.<\/p>\n<p>The syntax is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Buffer.concat(list[, totalLength])\r\n<\/pre>\n<p>The parameters are explained as follows:<\/p>\n<ul class=\"list\">\n<li><b>list<\/b> \u2212 An Array List of Buffer objects that is to be concatenated.<\/li>\n<li><b>totalLength<\/b> \u2212 The total length of the buffers when concatenated.<\/li>\n<\/ul>\n<p>The return value is a buffer object made up of a combination of the buffers concatenated.<\/p>\n<p>The code below shows a concatenation of two buffers, buffer1 and buffer2<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> buffer1 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Buffer(<span style=\"background-color: #fff0f0;\">'The Tech Pro'<\/span>);\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> buffer2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Buffer(<span style=\"background-color: #fff0f0;\">'Learning made easy!'<\/span>);\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> buffer3 <span style=\"color: #333333;\">=<\/span> Buffer.concat([buffer1,buffer2]);\r\n\r\nconsole.log(<span style=\"background-color: #fff0f0;\">\"buffer3 content: \"<\/span> <span style=\"color: #333333;\">+<\/span> buffer3.toString());\r\n<\/pre>\n<p>Try to run to code yourself to see the output.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t7\">7. Comparing two Buffers<\/strong><\/p>\n<p>You can compare two buffers using the compare method of the buffer object. For example:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">buffer1.compare(buffer2)\r\n<\/pre>\n<p>The return value is either a positive number, negative number or zero.<\/p>\n<p>Positive number indicates that buffer1 comes before buffer2<\/p>\n<p>Negative number indicates buffer1 comes after buffer2<\/p>\n<p>Zero means the two buffers are the same<\/p>\n<p>The program below illustrates the result of comparing two buffers:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> buffer1 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Buffer(<span style=\"background-color: #fff0f0;\">'AB'<\/span>);\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> buffer2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Buffer(<span style=\"background-color: #fff0f0;\">'ABCDE'<\/span>);\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> result <span style=\"color: #333333;\">=<\/span> buffer1.compare(buffer2);\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">if<\/span>(ret_val<span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>) {\r\n   console.log(buffer1 <span style=\"color: #333333;\">+<\/span><span style=\"background-color: #fff0f0;\">\" comes before \"<\/span> <span style=\"color: #333333;\">+<\/span> buffer2);\r\n} <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #008800; font-weight: bold;\">if<\/span>(ret_val <span style=\"color: #333333;\">===<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>) {\r\n   console.log(buffer1 <span style=\"color: #333333;\">+<\/span><span style=\"background-color: #fff0f0;\">\" is same as \"<\/span> <span style=\"color: #333333;\">+<\/span> buffer2);\r\n} <span style=\"color: #008800; font-weight: bold;\">else<\/span> {\r\n   console.log(buffer1 <span style=\"color: #333333;\">+<\/span><span style=\"background-color: #fff0f0;\">\" comes after \"<\/span> <span style=\"color: #333333;\">+<\/span> buffer2);\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"t8\">8. Copying Buffers<\/strong><\/p>\n<p>You can copy buffer using the copy instance method of the buffer object. The syntax is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">buffer.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])\r\n<\/pre>\n<p>where:<\/p>\n<ul>\n<li><b>targetBuffer<\/b> \u2212 The buffer object where buffer will be copied to.<\/li>\n<li><b>targetStart<\/b> \u2212 Number, Optional, Default is 0<\/li>\n<li><b>sourceStart<\/b> \u2212 Number, Optional, Default is0<\/li>\n<li><b>sourceEnd<\/b>\u00a0\u2212 Number, Optional, Default: buffer.length<\/li>\n<\/ul>\n<p>There is no return value.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t9\">9. Slicing a Buffer<\/strong><\/p>\n<p>Slicing a buffer means extracting a part of the buffer. The syntax is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">buf.slice([start][, end])\r\n<\/pre>\n<p>It returns a new buffer which is part of the original buffer but references the same memory location as the original buffer.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t9\">10. Other Buffer Class Methods<\/strong><\/p>\n<p>There are yet other buffer methods I would like you to try out. Find them in the table below:<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>SN.<\/th>\n<th>Method and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>Buffer.isEncoding(encoding)<\/b><\/p>\n<p>Returns true if the encoding is a valid encoding argument, otherwise it returns false.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>Buffer.isBuffer(obj)<\/b><\/p>\n<p>Checks if obj is a Buffer.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>Buffer.byteLength(string[, encoding])<\/b><\/p>\n<p>Gives the actual length of a string in byte. encoding default is &#8216;utf8&#8217;. This is not the same as String.prototype.length, becaise String.prototype.length returns the number of characters in a string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>Buffer.concat(list[, totalLength])<\/b><\/p>\n<p>We already mentioned this. It returns a buffer which is the result of concatenating all the buffers in the list together.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>Buffer.compare(buffer1, buffer2)<\/b><\/p>\n<p>The same as buffer1.compare(buffer2). Used for sorting an array of buffers.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we would cover Buffers in Node.js under the following sub-topics: Introduction to Buffers How to Create Buffers Writing to Buffers Reading from &hellip; <\/p>\n","protected":false},"author":1,"featured_media":81,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[10],"class_list":["post-77","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-buffers"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":82,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/77\/revisions\/82"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/media\/81"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}