Buffers

Buffers
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.

Raw data is stored in instances of the Buffer class. A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.

The Buffer object is global.

Converting between Buffers and JavaScript string objects requires an explicit encoding method. Here are the different string encodings;


 * 'ascii' - for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set.
 * 'utf8' - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8.
 * 'ucs2' - 2-bytes, little endian encoded Unicode characters. It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF).
 * 'base64' - Base64 string encoding.
 * 'binary' - 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 Buffer objects where possible. This encoding will be removed in future versions of Node.

new Buffer(size)
Allocates a new buffer of size octets.

new Buffer(array)
Allocates a new buffer using an array of octets.

new Buffer(str, encoding='utf8')
Allocates a new buffer containing the given str.

buffer.write(string, offset=0, encoding='utf8')
Writes string to the buffer at offset using the given encoding. Returns number of octets written. If buffer did not contain enough space to fit the entire string, it will write a partial amount of the string. In the case of 'utf8'encoding, the method will not write partial characters.

Example: write a utf8 string into a buffer, then print it buf = new Buffer(256); len = buf.write('\u00bd + \u00bc = \u00be', 0); console.log(len + " bytes: " + buf.toString('utf8', 0, len));

buffer.toString(encoding, start=0, end=buffer.length)
Decodes and returns a string from buffer data encoded with encodingbeginning at start and ending at end.

See buffer.write example, above.

buffer[index]
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Get and set the octet at index. The values refer to individual bytes, so the legal range is between 0x00 and 0xFF hex or 0 and 255.

<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Example: copy an ASCII string into a buffer, one byte at a time: 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

Buffer.isBuffer(obj)
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Tests if obj is a Buffer.

Buffer.byteLength(string, encoding='utf8')
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Gives the actual byte length of a string. This is not the same asString.prototype.length since that returns the number of characters in a string.

<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Example: str = '\u00bd + \u00bc = \u00be'; console.log(str + ": " + str.length + " characters, " +  Buffer.byteLength(str, 'utf8') + " bytes"); // Â½ + Â¼ = Â¾: 9 characters, 12 bytes

buffer.length
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">The size of the buffer in bytes. Note that this is not necessarily the size of the contents. length refers to the amount of memory allocated for the buffer object. It does not change when the contents of the buffer are changed. buf = new Buffer(1234); console.log(buf.length); buf.write("some string", "ascii", 0); console.log(buf.length); // 1234 // 1234

buffer.copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Does a memcpy between buffers.

<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Example: build two Buffers, then copy buf1 from byte 16 through byte 19 intobuf2, starting at the 8th byte in buf2. 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!!!!!!!!!!!!!

buffer.slice(start, end=buffer.length)
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Returns a new buffer which references the same memory as the old, but offset and cropped by the start and end indexes.

<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Modifying the new buffer slice will modify memory in the original buffer!

<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.4667em; margin-left: 0px; position: relative; text-rendering: optimizelegibility; ">Example: build a Buffer with the ASCII alphabet, take a slice, then modify one byte from the original Buffer. 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