| <!doctype html> |
| <html lang="en"> |
| <head> |
| <meta charset="utf-8"> |
| <title>Buffer Node.js v0.10.24 Manual & Documentation</title> |
| <link rel="stylesheet" href="assets/style.css"> |
| <link rel="stylesheet" href="assets/sh.css"> |
| <link rel="canonical" href="http://nodejs.org/api/buffer.html"> |
| </head> |
| <body class="alt apidoc" id="api-section-buffer"> |
| <div id="intro" class="interior"> |
| <a href="/" title="Go back to the home page"> |
| <img id="logo" src="http://nodejs.org/images/logo-light.png" alt="node.js"> |
| </a> |
| </div> |
| <div id="content" class="clearfix"> |
| <div id="column2" class="interior"> |
| <ul> |
| <li><a href="/" class="home">Home</a></li> |
| <li><a href="/download/" class="download">Download</a></li> |
| <li><a href="/about/" class="about">About</a></li> |
| <li><a href="http://npmjs.org/" class="npm">npm Registry</a></li> |
| <li><a href="http://nodejs.org/api/" class="docs current">Docs</a></li> |
| <li><a href="http://blog.nodejs.org" class="blog">Blog</a></li> |
| <li><a href="/community/" class="community">Community</a></li> |
| <li><a href="/logos/" class="logos">Logos</a></li> |
| <li><a href="http://jobs.nodejs.org/" class="jobs">Jobs</a></li> |
| </ul> |
| <p class="twitter"><a href="http://twitter.com/nodejs">@nodejs</a></p> |
| </div> |
| |
| <div id="column1" class="interior"> |
| <header> |
| <h1>Node.js v0.10.24 Manual & Documentation</h1> |
| <div id="gtoc"> |
| <p> |
| <a href="index.html" name="toc">Index</a> | |
| <a href="all.html">View on single page</a> | |
| <a href="buffer.json">View as JSON</a> |
| </p> |
| </div> |
| <hr> |
| </header> |
| |
| <div id="toc"> |
| <h2>Table of Contents</h2> |
| <ul> |
| <li><a href="#buffer_buffer">Buffer</a><ul> |
| <li><a href="#buffer_class_buffer">Class: Buffer</a><ul> |
| <li><a href="#buffer_new_buffer_size">new Buffer(size)</a></li> |
| <li><a href="#buffer_new_buffer_array">new Buffer(array)</a></li> |
| <li><a href="#buffer_new_buffer_str_encoding">new Buffer(str, [encoding])</a></li> |
| <li><a href="#buffer_class_method_buffer_isencoding_encoding">Class Method: Buffer.isEncoding(encoding)</a></li> |
| <li><a href="#buffer_buf_write_string_offset_length_encoding">buf.write(string, [offset], [length], [encoding])</a></li> |
| <li><a href="#buffer_buf_tostring_encoding_start_end">buf.toString([encoding], [start], [end])</a></li> |
| <li><a href="#buffer_buf_tojson">buf.toJSON()</a></li> |
| <li><a href="#buffer_buf_index">buf[index]</a></li> |
| <li><a href="#buffer_class_method_buffer_isbuffer_obj">Class Method: Buffer.isBuffer(obj)</a></li> |
| <li><a href="#buffer_class_method_buffer_bytelength_string_encoding">Class Method: Buffer.byteLength(string, [encoding])</a></li> |
| <li><a href="#buffer_class_method_buffer_concat_list_totallength">Class Method: Buffer.concat(list, [totalLength])</a></li> |
| <li><a href="#buffer_buf_length">buf.length</a></li> |
| <li><a href="#buffer_buf_copy_targetbuffer_targetstart_sourcestart_sourceend">buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd])</a></li> |
| <li><a href="#buffer_buf_slice_start_end">buf.slice([start], [end])</a></li> |
| <li><a href="#buffer_buf_readuint8_offset_noassert">buf.readUInt8(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readuint16le_offset_noassert">buf.readUInt16LE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readuint16be_offset_noassert">buf.readUInt16BE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readuint32le_offset_noassert">buf.readUInt32LE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readuint32be_offset_noassert">buf.readUInt32BE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readint8_offset_noassert">buf.readInt8(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readint16le_offset_noassert">buf.readInt16LE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readint16be_offset_noassert">buf.readInt16BE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readint32le_offset_noassert">buf.readInt32LE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readint32be_offset_noassert">buf.readInt32BE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readfloatle_offset_noassert">buf.readFloatLE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readfloatbe_offset_noassert">buf.readFloatBE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readdoublele_offset_noassert">buf.readDoubleLE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_readdoublebe_offset_noassert">buf.readDoubleBE(offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeuint8_value_offset_noassert">buf.writeUInt8(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeuint16le_value_offset_noassert">buf.writeUInt16LE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeuint16be_value_offset_noassert">buf.writeUInt16BE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeuint32le_value_offset_noassert">buf.writeUInt32LE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeuint32be_value_offset_noassert">buf.writeUInt32BE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeint8_value_offset_noassert">buf.writeInt8(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeint16le_value_offset_noassert">buf.writeInt16LE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeint16be_value_offset_noassert">buf.writeInt16BE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeint32le_value_offset_noassert">buf.writeInt32LE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writeint32be_value_offset_noassert">buf.writeInt32BE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writefloatle_value_offset_noassert">buf.writeFloatLE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writefloatbe_value_offset_noassert">buf.writeFloatBE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writedoublele_value_offset_noassert">buf.writeDoubleLE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_writedoublebe_value_offset_noassert">buf.writeDoubleBE(value, offset, [noAssert])</a></li> |
| <li><a href="#buffer_buf_fill_value_offset_end">buf.fill(value, [offset], [end])</a></li> |
| </ul> |
| </li> |
| <li><a href="#buffer_buffer_inspect_max_bytes">buffer.INSPECT_MAX_BYTES</a></li> |
| <li><a href="#buffer_class_slowbuffer">Class: SlowBuffer</a></li> |
| </ul> |
| </li> |
| </ul> |
| |
| </div> |
| |
| <div id="apicontent"> |
| <h1>Buffer<span><a class="mark" href="#buffer_buffer" id="buffer_buffer">#</a></span></h1> |
| <pre class="api_stability_3">Stability: 3 - Stable</pre><p>Pure JavaScript is Unicode friendly but not nice to binary data. When |
| dealing with TCP streams or the file system, it's necessary to handle octet |
| streams. Node has several strategies for manipulating, creating, and |
| consuming octet streams. |
| |
| </p> |
| <p>Raw data is stored in instances of the <code>Buffer</code> class. A <code>Buffer</code> is similar |
| to an array of integers but corresponds to a raw memory allocation outside |
| the V8 heap. A <code>Buffer</code> cannot be resized. |
| |
| </p> |
| <p>The <code>Buffer</code> class is a global, making it very rare that one would need |
| to ever <code>require('buffer')</code>. |
| |
| </p> |
| <p>Converting between Buffers and JavaScript string objects requires an explicit |
| encoding method. Here are the different string encodings. |
| |
| </p> |
| <ul> |
| <li><p><code>'ascii'</code> - for 7 bit ASCII data only. This encoding method is very fast, and |
| will strip the high bit if set.</p> |
| <p>Note that when converting from string to buffer, this encoding converts a null |
| character (<code>'\0'</code> or <code>'\u0000'</code>) into <code>0x20</code> (character code of a space). If |
| you want to convert a null character into <code>0x00</code>, you should use <code>'utf8'</code>.</p> |
| </li> |
| <li><p><code>'utf8'</code> - Multibyte encoded Unicode characters. Many web pages and other |
| document formats use UTF-8.</p> |
| </li> |
| <li><p><code>'utf16le'</code> - 2 or 4 bytes, little endian encoded Unicode characters. |
| Surrogate pairs (U+10000 to U+10FFFF) are supported.</p> |
| </li> |
| <li><p><code>'ucs2'</code> - Alias of <code>'utf16le'</code>.</p> |
| </li> |
| <li><p><code>'base64'</code> - Base64 string encoding.</p> |
| </li> |
| <li><p><code>'binary'</code> - A way of encoding raw binary data into strings by using only |
| the first 8 bits of each character. This encoding method is deprecated and |
| should be avoided in favor of <code>Buffer</code> objects where possible. This encoding |
| will be removed in future versions of Node.</p> |
| </li> |
| <li><p><code>'hex'</code> - Encode each byte as two hexadecimal characters.</p> |
| </li> |
| </ul> |
| <p>A <code>Buffer</code> object can also be used with typed arrays. The buffer object is |
| cloned to an <code>ArrayBuffer</code> that is used as the backing store for the typed |
| array. The memory of the buffer and the <code>ArrayBuffer</code> is not shared. |
| |
| </p> |
| <p>NOTE: Node.js v0.8 simply retained a reference to the buffer in <code>array.buffer</code> |
| instead of cloning it. |
| |
| </p> |
| <p>While more efficient, it introduces subtle incompatibilities with the typed |
| arrays specification. <code>ArrayBuffer#slice()</code> makes a copy of the slice while |
| <code>Buffer#slice()</code> creates a view. |
| |
| </p> |
| <h2>Class: Buffer<span><a class="mark" href="#buffer_class_buffer" id="buffer_class_buffer">#</a></span></h2> |
| <p>The Buffer class is a global type for dealing with binary data directly. |
| It can be constructed in a variety of ways. |
| |
| </p> |
| <h3>new Buffer(size)<span><a class="mark" href="#buffer_new_buffer_size" id="buffer_new_buffer_size">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>size</code> Number</li> |
| </div></ul> |
| <p>Allocates a new buffer of <code>size</code> octets. |
| |
| </p> |
| <h3>new Buffer(array)<span><a class="mark" href="#buffer_new_buffer_array" id="buffer_new_buffer_array">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>array</code> Array</li> |
| </div></ul> |
| <p>Allocates a new buffer using an <code>array</code> of octets. |
| |
| </p> |
| <h3>new Buffer(str, [encoding])<span><a class="mark" href="#buffer_new_buffer_str_encoding" id="buffer_new_buffer_str_encoding">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>str</code> String - string to encode.</li> |
| <li><code>encoding</code> String - encoding to use, Optional.</li> |
| </div></ul> |
| <p>Allocates a new buffer containing the given <code>str</code>. |
| <code>encoding</code> defaults to <code>'utf8'</code>. |
| |
| </p> |
| <h3>Class Method: Buffer.isEncoding(encoding)<span><a class="mark" href="#buffer_class_method_buffer_isencoding_encoding" id="buffer_class_method_buffer_isencoding_encoding">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>encoding</code> <span class="type">String</span> The encoding string to test</li> |
| </div></ul> |
| <p>Returns true if the <code>encoding</code> is a valid encoding argument, or false |
| otherwise. |
| |
| </p> |
| <h3>buf.write(string, [offset], [length], [encoding])<span><a class="mark" href="#buffer_buf_write_string_offset_length_encoding" id="buffer_buf_write_string_offset_length_encoding">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>string</code> String - data to be written to buffer</li> |
| <li><code>offset</code> Number, Optional, Default: 0</li> |
| <li><code>length</code> Number, Optional, Default: <code>buffer.length - offset</code></li> |
| <li><code>encoding</code> String, Optional, Default: 'utf8'</li> |
| </div></ul> |
| <p>Writes <code>string</code> to the buffer at <code>offset</code> using the given encoding. |
| <code>offset</code> defaults to <code>0</code>, <code>encoding</code> defaults to <code>'utf8'</code>. <code>length</code> is |
| the number of bytes to write. Returns number of octets written. If <code>buffer</code> did |
| not contain enough space to fit the entire string, it will write a partial |
| amount of the string. <code>length</code> defaults to <code>buffer.length - offset</code>. |
| The method will not write partial characters. |
| |
| </p> |
| <pre><code>buf = new Buffer(256); |
| len = buf.write('\u00bd + \u00bc = \u00be', 0); |
| console.log(len + " bytes: " + buf.toString('utf8', 0, len));</code></pre> |
| <p>The number of characters written (which may be different than the number of |
| bytes written) is set in <code>Buffer._charsWritten</code> and will be overwritten the |
| next time <code>buf.write()</code> is called. |
| |
| |
| </p> |
| <h3>buf.toString([encoding], [start], [end])<span><a class="mark" href="#buffer_buf_tostring_encoding_start_end" id="buffer_buf_tostring_encoding_start_end">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>encoding</code> String, Optional, Default: 'utf8'</li> |
| <li><code>start</code> Number, Optional, Default: 0</li> |
| <li><code>end</code> Number, Optional, Default: <code>buffer.length</code></li> |
| </div></ul> |
| <p>Decodes and returns a string from buffer data encoded with <code>encoding</code> |
| (defaults to <code>'utf8'</code>) beginning at <code>start</code> (defaults to <code>0</code>) and ending at |
| <code>end</code> (defaults to <code>buffer.length</code>). |
| |
| </p> |
| <p>See <code>buffer.write()</code> example, above. |
| |
| |
| </p> |
| <h3>buf.toJSON()<span><a class="mark" href="#buffer_buf_tojson" id="buffer_buf_tojson">#</a></span></h3> |
| <p>Returns a JSON-representation of the Buffer instance, which is identical to the |
| output for JSON Arrays. <code>JSON.stringify</code> implicitly calls this function when |
| stringifying a Buffer instance. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer('test'); |
| var json = JSON.stringify(buf); |
| |
| console.log(json); |
| // '[116,101,115,116]' |
| |
| var copy = new Buffer(JSON.parse(json)); |
| |
| console.log(copy); |
| // <Buffer 74 65 73 74></code></pre> |
| <h3>buf[index]<span><a class="mark" href="#buffer_buf_index" id="buffer_buf_index">#</a></span></h3> |
| <!--type=property--> |
| <!--name=[index]--> |
| |
| <p>Get and set the octet at <code>index</code>. The values refer to individual bytes, |
| so the legal range is between <code>0x00</code> and <code>0xFF</code> hex or <code>0</code> and <code>255</code>. |
| |
| </p> |
| <p>Example: copy an ASCII string into a buffer, one byte at a time: |
| |
| </p> |
| <pre><code>str = "node.js"; |
| buf = new Buffer(str.length); |
| |
| for (var i = 0; i < str.length ; i++) { |
| buf[i] = str.charCodeAt(i); |
| } |
| |
| console.log(buf); |
| |
| // node.js</code></pre> |
| <h3>Class Method: Buffer.isBuffer(obj)<span><a class="mark" href="#buffer_class_method_buffer_isbuffer_obj" id="buffer_class_method_buffer_isbuffer_obj">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>obj</code> Object</li> |
| <li>Return: Boolean</li> |
| </div></ul> |
| <p>Tests if <code>obj</code> is a <code>Buffer</code>. |
| |
| </p> |
| <h3>Class Method: Buffer.byteLength(string, [encoding])<span><a class="mark" href="#buffer_class_method_buffer_bytelength_string_encoding" id="buffer_class_method_buffer_bytelength_string_encoding">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>string</code> String</li> |
| <li><code>encoding</code> String, Optional, Default: 'utf8'</li> |
| <li>Return: Number</li> |
| </div></ul> |
| <p>Gives the actual byte length of a string. <code>encoding</code> defaults to <code>'utf8'</code>. |
| This is not the same as <code>String.prototype.length</code> since that returns the |
| number of <em>characters</em> in a string. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>str = '\u00bd + \u00bc = \u00be'; |
| |
| console.log(str + ": " + str.length + " characters, " + |
| Buffer.byteLength(str, 'utf8') + " bytes"); |
| |
| // ½ + ¼ = ¾: 9 characters, 12 bytes</code></pre> |
| <h3>Class Method: Buffer.concat(list, [totalLength])<span><a class="mark" href="#buffer_class_method_buffer_concat_list_totallength" id="buffer_class_method_buffer_concat_list_totallength">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>list</code> <span class="type">Array</span> List of Buffer objects to concat</li> |
| <li><code>totalLength</code> <span class="type">Number</span> Total length of the buffers when concatenated</li> |
| </div></ul> |
| <p>Returns a buffer which is the result of concatenating all the buffers in |
| the list together. |
| |
| </p> |
| <p>If the list has no items, or if the totalLength is 0, then it returns a |
| zero-length buffer. |
| |
| </p> |
| <p>If the list has exactly one item, then the first item of the list is |
| returned. |
| |
| </p> |
| <p>If the list has more than one item, then a new Buffer is created. |
| |
| </p> |
| <p>If totalLength is not provided, it is read from the buffers in the list. |
| However, this adds an additional loop to the function, so it is faster |
| to provide the length explicitly. |
| |
| </p> |
| <h3>buf.length<span><a class="mark" href="#buffer_buf_length" id="buffer_buf_length">#</a></span></h3> |
| <div class="signature"><ul> |
| <li>Number</li> |
| </div></ul> |
| <p>The size of the buffer in bytes. Note that this is not necessarily the size |
| of the contents. <code>length</code> refers to the amount of memory allocated for the |
| buffer object. It does not change when the contents of the buffer are changed. |
| |
| </p> |
| <pre><code>buf = new Buffer(1234); |
| |
| console.log(buf.length); |
| buf.write("some string", 0, "ascii"); |
| console.log(buf.length); |
| |
| // 1234 |
| // 1234</code></pre> |
| <h3>buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd])<span><a class="mark" href="#buffer_buf_copy_targetbuffer_targetstart_sourcestart_sourceend" id="buffer_buf_copy_targetbuffer_targetstart_sourcestart_sourceend">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>targetBuffer</code> Buffer object - Buffer to copy into</li> |
| <li><code>targetStart</code> Number, Optional, Default: 0</li> |
| <li><code>sourceStart</code> Number, Optional, Default: 0</li> |
| <li><code>sourceEnd</code> Number, Optional, Default: <code>buffer.length</code></li> |
| </div></ul> |
| <p>Does copy between buffers. The source and target regions can be overlapped. |
| <code>targetStart</code> and <code>sourceStart</code> default to <code>0</code>. |
| <code>sourceEnd</code> defaults to <code>buffer.length</code>. |
| |
| </p> |
| <p>All values passed that are <code>undefined</code>/<code>NaN</code> or are out of bounds are set equal |
| to their respective defaults. |
| |
| </p> |
| <p>Example: build two Buffers, then copy <code>buf1</code> from byte 16 through byte 19 |
| into <code>buf2</code>, starting at the 8th byte in <code>buf2</code>. |
| |
| </p> |
| <pre><code>buf1 = new Buffer(26); |
| buf2 = new Buffer(26); |
| |
| for (var i = 0 ; i < 26 ; i++) { |
| buf1[i] = i + 97; // 97 is ASCII a |
| buf2[i] = 33; // ASCII ! |
| } |
| |
| buf1.copy(buf2, 8, 16, 20); |
| console.log(buf2.toString('ascii', 0, 25)); |
| |
| // !!!!!!!!qrst!!!!!!!!!!!!!</code></pre> |
| <h3>buf.slice([start], [end])<span><a class="mark" href="#buffer_buf_slice_start_end" id="buffer_buf_slice_start_end">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>start</code> Number, Optional, Default: 0</li> |
| <li><code>end</code> Number, Optional, Default: <code>buffer.length</code></li> |
| </div></ul> |
| <p>Returns a new buffer which references the same memory as the old, but offset |
| and cropped by the <code>start</code> (defaults to <code>0</code>) and <code>end</code> (defaults to |
| <code>buffer.length</code>) indexes. Negative indexes start from the end of the buffer. |
| |
| </p> |
| <p><strong>Modifying the new buffer slice will modify memory in the original buffer!</strong> |
| |
| </p> |
| <p>Example: build a Buffer with the ASCII alphabet, take a slice, then modify one |
| byte from the original Buffer. |
| |
| </p> |
| <pre><code>var buf1 = new Buffer(26); |
| |
| for (var i = 0 ; i < 26 ; i++) { |
| buf1[i] = i + 97; // 97 is ASCII a |
| } |
| |
| var buf2 = buf1.slice(0, 3); |
| console.log(buf2.toString('ascii', 0, buf2.length)); |
| buf1[0] = 33; |
| console.log(buf2.toString('ascii', 0, buf2.length)); |
| |
| // abc |
| // !bc</code></pre> |
| <h3>buf.readUInt8(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readuint8_offset_noassert" id="buffer_buf_readuint8_offset_noassert">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| <li>Return: Number</li> |
| </div></ul> |
| <p>Reads an unsigned 8 bit integer from the buffer at the specified offset. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>offset</code>. This means that <code>offset</code> |
| may be beyond the end of the buffer. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(4); |
| |
| buf[0] = 0x3; |
| buf[1] = 0x4; |
| buf[2] = 0x23; |
| buf[3] = 0x42; |
| |
| for (ii = 0; ii < buf.length; ii++) { |
| console.log(buf.readUInt8(ii)); |
| } |
| |
| // 0x3 |
| // 0x4 |
| // 0x23 |
| // 0x42</code></pre> |
| <h3>buf.readUInt16LE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readuint16le_offset_noassert" id="buffer_buf_readuint16le_offset_noassert">#</a></span></h3> |
| <h3>buf.readUInt16BE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readuint16be_offset_noassert" id="buffer_buf_readuint16be_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| <li>Return: Number</li> |
| </ul> |
| <p>Reads an unsigned 16 bit integer from the buffer at the specified offset with |
| specified endian format. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>offset</code>. This means that <code>offset</code> |
| may be beyond the end of the buffer. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(4); |
| |
| buf[0] = 0x3; |
| buf[1] = 0x4; |
| buf[2] = 0x23; |
| buf[3] = 0x42; |
| |
| console.log(buf.readUInt16BE(0)); |
| console.log(buf.readUInt16LE(0)); |
| console.log(buf.readUInt16BE(1)); |
| console.log(buf.readUInt16LE(1)); |
| console.log(buf.readUInt16BE(2)); |
| console.log(buf.readUInt16LE(2)); |
| |
| // 0x0304 |
| // 0x0403 |
| // 0x0423 |
| // 0x2304 |
| // 0x2342 |
| // 0x4223</code></pre> |
| <h3>buf.readUInt32LE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readuint32le_offset_noassert" id="buffer_buf_readuint32le_offset_noassert">#</a></span></h3> |
| <h3>buf.readUInt32BE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readuint32be_offset_noassert" id="buffer_buf_readuint32be_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| <li>Return: Number</li> |
| </ul> |
| <p>Reads an unsigned 32 bit integer from the buffer at the specified offset with |
| specified endian format. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>offset</code>. This means that <code>offset</code> |
| may be beyond the end of the buffer. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(4); |
| |
| buf[0] = 0x3; |
| buf[1] = 0x4; |
| buf[2] = 0x23; |
| buf[3] = 0x42; |
| |
| console.log(buf.readUInt32BE(0)); |
| console.log(buf.readUInt32LE(0)); |
| |
| // 0x03042342 |
| // 0x42230403</code></pre> |
| <h3>buf.readInt8(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readint8_offset_noassert" id="buffer_buf_readint8_offset_noassert">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| <li>Return: Number</li> |
| </div></ul> |
| <p>Reads a signed 8 bit integer from the buffer at the specified offset. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>offset</code>. This means that <code>offset</code> |
| may be beyond the end of the buffer. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Works as <code>buffer.readUInt8</code>, except buffer contents are treated as two's |
| complement signed values. |
| |
| </p> |
| <h3>buf.readInt16LE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readint16le_offset_noassert" id="buffer_buf_readint16le_offset_noassert">#</a></span></h3> |
| <h3>buf.readInt16BE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readint16be_offset_noassert" id="buffer_buf_readint16be_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| <li>Return: Number</li> |
| </ul> |
| <p>Reads a signed 16 bit integer from the buffer at the specified offset with |
| specified endian format. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>offset</code>. This means that <code>offset</code> |
| may be beyond the end of the buffer. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Works as <code>buffer.readUInt16*</code>, except buffer contents are treated as two's |
| complement signed values. |
| |
| </p> |
| <h3>buf.readInt32LE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readint32le_offset_noassert" id="buffer_buf_readint32le_offset_noassert">#</a></span></h3> |
| <h3>buf.readInt32BE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readint32be_offset_noassert" id="buffer_buf_readint32be_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| <li>Return: Number</li> |
| </ul> |
| <p>Reads a signed 32 bit integer from the buffer at the specified offset with |
| specified endian format. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>offset</code>. This means that <code>offset</code> |
| may be beyond the end of the buffer. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Works as <code>buffer.readUInt32*</code>, except buffer contents are treated as two's |
| complement signed values. |
| |
| </p> |
| <h3>buf.readFloatLE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readfloatle_offset_noassert" id="buffer_buf_readfloatle_offset_noassert">#</a></span></h3> |
| <h3>buf.readFloatBE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readfloatbe_offset_noassert" id="buffer_buf_readfloatbe_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| <li>Return: Number</li> |
| </ul> |
| <p>Reads a 32 bit float from the buffer at the specified offset with specified |
| endian format. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>offset</code>. This means that <code>offset</code> |
| may be beyond the end of the buffer. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(4); |
| |
| buf[0] = 0x00; |
| buf[1] = 0x00; |
| buf[2] = 0x80; |
| buf[3] = 0x3f; |
| |
| console.log(buf.readFloatLE(0)); |
| |
| // 0x01</code></pre> |
| <h3>buf.readDoubleLE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readdoublele_offset_noassert" id="buffer_buf_readdoublele_offset_noassert">#</a></span></h3> |
| <h3>buf.readDoubleBE(offset, [noAssert])<span><a class="mark" href="#buffer_buf_readdoublebe_offset_noassert" id="buffer_buf_readdoublebe_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| <li>Return: Number</li> |
| </ul> |
| <p>Reads a 64 bit double from the buffer at the specified offset with specified |
| endian format. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>offset</code>. This means that <code>offset</code> |
| may be beyond the end of the buffer. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(8); |
| |
| buf[0] = 0x55; |
| buf[1] = 0x55; |
| buf[2] = 0x55; |
| buf[3] = 0x55; |
| buf[4] = 0x55; |
| buf[5] = 0x55; |
| buf[6] = 0xd5; |
| buf[7] = 0x3f; |
| |
| console.log(buf.readDoubleLE(0)); |
| |
| // 0.3333333333333333</code></pre> |
| <h3>buf.writeUInt8(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeuint8_value_offset_noassert" id="buffer_buf_writeuint8_value_offset_noassert">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>value</code> Number</li> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| </div></ul> |
| <p>Writes <code>value</code> to the buffer at the specified offset. Note, <code>value</code> must be a |
| valid unsigned 8 bit integer. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>value</code> and <code>offset</code>. This means |
| that <code>value</code> may be too large for the specific function and <code>offset</code> may be |
| beyond the end of the buffer leading to the values being silently dropped. This |
| should not be used unless you are certain of correctness. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(4); |
| buf.writeUInt8(0x3, 0); |
| buf.writeUInt8(0x4, 1); |
| buf.writeUInt8(0x23, 2); |
| buf.writeUInt8(0x42, 3); |
| |
| console.log(buf); |
| |
| // <Buffer 03 04 23 42></code></pre> |
| <h3>buf.writeUInt16LE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeuint16le_value_offset_noassert" id="buffer_buf_writeuint16le_value_offset_noassert">#</a></span></h3> |
| <h3>buf.writeUInt16BE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeuint16be_value_offset_noassert" id="buffer_buf_writeuint16be_value_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>value</code> Number</li> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| </ul> |
| <p>Writes <code>value</code> to the buffer at the specified offset with specified endian |
| format. Note, <code>value</code> must be a valid unsigned 16 bit integer. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>value</code> and <code>offset</code>. This means |
| that <code>value</code> may be too large for the specific function and <code>offset</code> may be |
| beyond the end of the buffer leading to the values being silently dropped. This |
| should not be used unless you are certain of correctness. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(4); |
| buf.writeUInt16BE(0xdead, 0); |
| buf.writeUInt16BE(0xbeef, 2); |
| |
| console.log(buf); |
| |
| buf.writeUInt16LE(0xdead, 0); |
| buf.writeUInt16LE(0xbeef, 2); |
| |
| console.log(buf); |
| |
| // <Buffer de ad be ef> |
| // <Buffer ad de ef be></code></pre> |
| <h3>buf.writeUInt32LE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeuint32le_value_offset_noassert" id="buffer_buf_writeuint32le_value_offset_noassert">#</a></span></h3> |
| <h3>buf.writeUInt32BE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeuint32be_value_offset_noassert" id="buffer_buf_writeuint32be_value_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>value</code> Number</li> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| </ul> |
| <p>Writes <code>value</code> to the buffer at the specified offset with specified endian |
| format. Note, <code>value</code> must be a valid unsigned 32 bit integer. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>value</code> and <code>offset</code>. This means |
| that <code>value</code> may be too large for the specific function and <code>offset</code> may be |
| beyond the end of the buffer leading to the values being silently dropped. This |
| should not be used unless you are certain of correctness. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(4); |
| buf.writeUInt32BE(0xfeedface, 0); |
| |
| console.log(buf); |
| |
| buf.writeUInt32LE(0xfeedface, 0); |
| |
| console.log(buf); |
| |
| // <Buffer fe ed fa ce> |
| // <Buffer ce fa ed fe></code></pre> |
| <h3>buf.writeInt8(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeint8_value_offset_noassert" id="buffer_buf_writeint8_value_offset_noassert">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>value</code> Number</li> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| </div></ul> |
| <p>Writes <code>value</code> to the buffer at the specified offset. Note, <code>value</code> must be a |
| valid signed 8 bit integer. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>value</code> and <code>offset</code>. This means |
| that <code>value</code> may be too large for the specific function and <code>offset</code> may be |
| beyond the end of the buffer leading to the values being silently dropped. This |
| should not be used unless you are certain of correctness. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Works as <code>buffer.writeUInt8</code>, except value is written out as a two's complement |
| signed integer into <code>buffer</code>. |
| |
| </p> |
| <h3>buf.writeInt16LE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeint16le_value_offset_noassert" id="buffer_buf_writeint16le_value_offset_noassert">#</a></span></h3> |
| <h3>buf.writeInt16BE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeint16be_value_offset_noassert" id="buffer_buf_writeint16be_value_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>value</code> Number</li> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| </ul> |
| <p>Writes <code>value</code> to the buffer at the specified offset with specified endian |
| format. Note, <code>value</code> must be a valid signed 16 bit integer. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>value</code> and <code>offset</code>. This means |
| that <code>value</code> may be too large for the specific function and <code>offset</code> may be |
| beyond the end of the buffer leading to the values being silently dropped. This |
| should not be used unless you are certain of correctness. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Works as <code>buffer.writeUInt16*</code>, except value is written out as a two's |
| complement signed integer into <code>buffer</code>. |
| |
| </p> |
| <h3>buf.writeInt32LE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeint32le_value_offset_noassert" id="buffer_buf_writeint32le_value_offset_noassert">#</a></span></h3> |
| <h3>buf.writeInt32BE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writeint32be_value_offset_noassert" id="buffer_buf_writeint32be_value_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>value</code> Number</li> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| </ul> |
| <p>Writes <code>value</code> to the buffer at the specified offset with specified endian |
| format. Note, <code>value</code> must be a valid signed 32 bit integer. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>value</code> and <code>offset</code>. This means |
| that <code>value</code> may be too large for the specific function and <code>offset</code> may be |
| beyond the end of the buffer leading to the values being silently dropped. This |
| should not be used unless you are certain of correctness. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Works as <code>buffer.writeUInt32*</code>, except value is written out as a two's |
| complement signed integer into <code>buffer</code>. |
| |
| </p> |
| <h3>buf.writeFloatLE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writefloatle_value_offset_noassert" id="buffer_buf_writefloatle_value_offset_noassert">#</a></span></h3> |
| <h3>buf.writeFloatBE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writefloatbe_value_offset_noassert" id="buffer_buf_writefloatbe_value_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>value</code> Number</li> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| </ul> |
| <p>Writes <code>value</code> to the buffer at the specified offset with specified endian |
| format. Note, behavior is unspecified if <code>value</code> is not a 32 bit float. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>value</code> and <code>offset</code>. This means |
| that <code>value</code> may be too large for the specific function and <code>offset</code> may be |
| beyond the end of the buffer leading to the values being silently dropped. This |
| should not be used unless you are certain of correctness. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(4); |
| buf.writeFloatBE(0xcafebabe, 0); |
| |
| console.log(buf); |
| |
| buf.writeFloatLE(0xcafebabe, 0); |
| |
| console.log(buf); |
| |
| // <Buffer 4f 4a fe bb> |
| // <Buffer bb fe 4a 4f></code></pre> |
| <h3>buf.writeDoubleLE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writedoublele_value_offset_noassert" id="buffer_buf_writedoublele_value_offset_noassert">#</a></span></h3> |
| <h3>buf.writeDoubleBE(value, offset, [noAssert])<span><a class="mark" href="#buffer_buf_writedoublebe_value_offset_noassert" id="buffer_buf_writedoublebe_value_offset_noassert">#</a></span></h3> |
| <ul> |
| <li><code>value</code> Number</li> |
| <li><code>offset</code> Number</li> |
| <li><code>noAssert</code> Boolean, Optional, Default: false</li> |
| </ul> |
| <p>Writes <code>value</code> to the buffer at the specified offset with specified endian |
| format. Note, <code>value</code> must be a valid 64 bit double. |
| |
| </p> |
| <p>Set <code>noAssert</code> to true to skip validation of <code>value</code> and <code>offset</code>. This means |
| that <code>value</code> may be too large for the specific function and <code>offset</code> may be |
| beyond the end of the buffer leading to the values being silently dropped. This |
| should not be used unless you are certain of correctness. Defaults to <code>false</code>. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>var buf = new Buffer(8); |
| buf.writeDoubleBE(0xdeadbeefcafebabe, 0); |
| |
| console.log(buf); |
| |
| buf.writeDoubleLE(0xdeadbeefcafebabe, 0); |
| |
| console.log(buf); |
| |
| // <Buffer 43 eb d5 b7 dd f9 5f d7> |
| // <Buffer d7 5f f9 dd b7 d5 eb 43></code></pre> |
| <h3>buf.fill(value, [offset], [end])<span><a class="mark" href="#buffer_buf_fill_value_offset_end" id="buffer_buf_fill_value_offset_end">#</a></span></h3> |
| <div class="signature"><ul> |
| <li><code>value</code></li> |
| <li><code>offset</code> Number, Optional</li> |
| <li><code>end</code> Number, Optional</li> |
| </div></ul> |
| <p>Fills the buffer with the specified value. If the <code>offset</code> (defaults to <code>0</code>) |
| and <code>end</code> (defaults to <code>buffer.length</code>) are not given it will fill the entire |
| buffer. |
| |
| </p> |
| <pre><code>var b = new Buffer(50); |
| b.fill("h");</code></pre> |
| <h2>buffer.INSPECT_MAX_BYTES<span><a class="mark" href="#buffer_buffer_inspect_max_bytes" id="buffer_buffer_inspect_max_bytes">#</a></span></h2> |
| <div class="signature"><ul> |
| <li>Number, Default: 50</li> |
| </div></ul> |
| <p>How many bytes will be returned when <code>buffer.inspect()</code> is called. This can |
| be overridden by user modules. |
| |
| </p> |
| <p>Note that this is a property on the buffer module returned by |
| <code>require('buffer')</code>, not on the Buffer global, or a buffer instance. |
| |
| </p> |
| <h2>Class: SlowBuffer<span><a class="mark" href="#buffer_class_slowbuffer" id="buffer_class_slowbuffer">#</a></span></h2> |
| <p>This class is primarily for internal use. JavaScript programs should |
| use Buffer instead of using SlowBuffer. |
| |
| </p> |
| <p>In order to avoid the overhead of allocating many C++ Buffer objects for |
| small blocks of memory in the lifetime of a server, Node allocates memory |
| in 8Kb (8192 byte) chunks. If a buffer is smaller than this size, then it |
| will be backed by a parent SlowBuffer object. If it is larger than this, |
| then Node will allocate a SlowBuffer slab for it directly. |
| </p> |
| |
| </div> |
| </div> |
| </div> |
| <div id="footer"> |
| <a href="http://joyent.com" class="joyent-logo">Joyent</a> |
| <ul class="clearfix"> |
| <li><a href="/">Node.js</a></li> |
| <li><a href="/download/">Download</a></li> |
| <li><a href="/about/">About</a></li> |
| <li><a href="http://npmjs.org/">npm Registry</a></li> |
| <li><a href="http://nodejs.org/api/">Docs</a></li> |
| <li><a href="http://blog.nodejs.org">Blog</a></li> |
| <li><a href="/community/">Community</a></li> |
| <li><a href="/logos/">Logos</a></li> |
| <li><a href="http://jobs.nodejs.org/">Jobs</a></li> |
| <li><a href="http://twitter.com/nodejs" class="twitter">@nodejs</a></li> |
| </ul> |
| |
| <p>Copyright <a href="http://joyent.com/">Joyent, Inc</a>, Node.js is a <a href="/trademark-policy.pdf">trademark</a> of Joyent, Inc. View <a href="https://raw.github.com/joyent/node/v0.10.24/LICENSE">license</a>.</p> |
| </div> |
| |
| <script src="../sh_main.js"></script> |
| <script src="../sh_javascript.min.js"></script> |
| <script>highlight(undefined, undefined, 'pre');</script> |
| <script> |
| window._gaq = [['_setAccount', 'UA-10874194-2'], ['_trackPageview']]; |
| (function(d, t) { |
| var g = d.createElement(t), |
| s = d.getElementsByTagName(t)[0]; |
| g.src = '//www.google-analytics.com/ga.js'; |
| s.parentNode.insertBefore(g, s); |
| }(document, 'script')); |
| </script> |
| </body> |
| </html> |
| |