Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
73 lines (50 sloc) 3.06 KB
<div class='mp'>
<p> To handle binary data, node provides us with the global <code>Buffer</code> object. <code>Buffer</code> instances represent memory allocated independently of V8's heap. There are several ways to construct a <code>Buffer</code> instance, and many ways you can manipulate its data.</p>
<p>The simplest way to construct a <code>Buffer</code> from a string is to simply pass a string as the first argument. As you can see in the log output, we now have a buffer object containing 5 bytes of data represented in hexadecimal.</p>
<pre><code>var hello = new Buffer('Hello');
// =&gt; &lt;Buffer 48 65 6c 6c 6f>
// =&gt; "Hello"
<p>By default, the encoding is "utf8", but this can be overridden by passing a string as the second argument. For example, the ellipsis below will be printed to stdout as the "&amp;" character when in "ascii" encoding.</p>
<pre><code>var buf = new Buffer('…');
// =&gt;
var buf = new Buffer('…', 'ascii');
// =&gt; &amp;
<p>An alternative (but in this case functionality equivalent) method is to pass an array of integers representing the octet stream.</p>
<pre><code>var hello = new Buffer([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
<p>Buffers can also be created with an integer representing the number of bytes allocated, after which we can call the <code>write()</code> method, providing an optional offset and encoding. Below, we provide an offset of 2 bytes to our second call to <code>write()</code> (buffering "Hel") and then write another two bytes with an offset of 3 (completing "Hello").</p>
<pre><code>var buf = new Buffer(5);
buf.write('l', 2);
buf.write('lo', 3);
// =&gt; "Hello"
<p>The <code>.length</code> property of a buffer instance contains the byte length of the stream, as opposed to native strings, which simply return the number of characters. For example, the ellipsis character '…' consists of three bytes, so the buffer will respond with the byte length (3), and not the character length (1).</p>
<pre><code>var ellipsis = new Buffer('…', 'utf8');
console.log('… string length: %d', '…'.length);
// =&gt; … string length: 1
console.log('… byte length: %d', ellipsis.length);
// =&gt; … byte length: 3
// =&gt; &lt;Buffer e2 80 a6>
<p>To determine the byte length of a native string, pass it to the <code>Buffer.byteLength()</code> method.</p>
<p>The API is written in such a way that it is String-like. For example, we can work with "slices" of a <code>Buffer</code> by passing offsets to the <code>slice()</code> method:</p>
<pre><code>var chunk = buf.slice(4, 9);
// =&gt; "some"
<p>Alternatively, when expecting a string, we can pass offsets to <code>Buffer#toString()</code>:</p>
<pre><code>var buf = new Buffer('just some data');
console.log(buf.toString('ascii', 4, 9));
// =&gt; "some"
Something went wrong with that request. Please try again.