5 Pure JavaScript is Unicode friendly but not nice to binary data. When
6 dealing with TCP streams or the file system, it's necessary to handle octet
7 streams. Node has several strategies for manipulating, creating, and
8 consuming octet streams.
10 Raw data is stored in instances of the `Buffer` class. A `Buffer` is similar
11 to an array of integers but corresponds to a raw memory allocation outside
12 the V8 heap. A `Buffer` cannot be resized.
14 The `Buffer` class is a global, making it very rare that one would need
15 to ever `require('buffer')`.
17 Converting between Buffers and JavaScript string objects requires an explicit
18 encoding method. Here are the different string encodings.
20 * `'ascii'` - for 7 bit ASCII data only. This encoding method is very fast, and
21 will strip the high bit if set.
23 * `'utf8'` - Multibyte encoded Unicode characters. Many web pages and other
24 document formats use UTF-8.
26 * `'utf16le'` - 2 or 4 bytes, little endian encoded Unicode characters.
27 Surrogate pairs (U+10000 to U+10FFFF) are supported.
29 * `'ucs2'` - Alias of `'utf16le'`.
31 * `'base64'` - Base64 string encoding.
33 * `'binary'` - A way of encoding raw binary data into strings by using only
34 the first 8 bits of each character. This encoding method is deprecated and
35 should be avoided in favor of `Buffer` objects where possible. This encoding
36 will be removed in future versions of Node.
38 * `'hex'` - Encode each byte as two hexadecimal characters.
40 Creating a typed array from a `Buffer` works with the following caveats:
42 1. The buffer's memory is copied, not shared.
44 2. The buffer's memory is interpreted as an array, not a byte array. That is,
45 `new Uint32Array(new Buffer([1,2,3,4]))` creates a 4-element `Uint32Array`
46 with elements `[1,2,3,4]`, not an `Uint32Array` with a single element
47 `[0x1020304]` or `[0x4030201]`.
49 NOTE: Node.js v0.8 simply retained a reference to the buffer in `array.buffer`
50 instead of cloning it.
52 While more efficient, it introduces subtle incompatibilities with the typed
53 arrays specification. `ArrayBuffer#slice()` makes a copy of the slice while
54 `Buffer#slice()` creates a view.
58 The Buffer class is a global type for dealing with binary data directly.
59 It can be constructed in a variety of ways.
65 Allocates a new buffer of `size` octets. Note, `size` must be no more than
66 [kMaxLength](smalloc.html#smalloc_smalloc_kmaxlength). Otherwise, a `RangeError`
73 Allocates a new buffer using an `array` of octets.
75 ### new Buffer(buffer)
79 Copies the passed `buffer` data onto a new `Buffer` instance.
81 ### new Buffer(str[, encoding])
83 * `str` String - string to encode.
84 * `encoding` String - encoding to use, Optional.
86 Allocates a new buffer containing the given `str`.
87 `encoding` defaults to `'utf8'`.
89 ### Class Method: Buffer.isEncoding(encoding)
91 * `encoding` {String} The encoding string to test
93 Returns true if the `encoding` is a valid encoding argument, or false
96 ### Class Method: Buffer.isBuffer(obj)
101 Tests if `obj` is a `Buffer`.
103 ### Class Method: Buffer.byteLength(string[, encoding])
106 * `encoding` String, Optional, Default: 'utf8'
109 Gives the actual byte length of a string. `encoding` defaults to `'utf8'`.
110 This is not the same as `String.prototype.length` since that returns the
111 number of *characters* in a string.
115 str = '\u00bd + \u00bc = \u00be';
117 console.log(str + ": " + str.length + " characters, " +
118 Buffer.byteLength(str, 'utf8') + " bytes");
120 // ½ + ¼ = ¾: 9 characters, 12 bytes
122 ### Class Method: Buffer.concat(list[, totalLength])
124 * `list` {Array} List of Buffer objects to concat
125 * `totalLength` {Number} Total length of the buffers when concatenated
127 Returns a buffer which is the result of concatenating all the buffers in
130 If the list has no items, or if the totalLength is 0, then it returns a
133 If the list has exactly one item, then the first item of the list is
136 If the list has more than one item, then a new Buffer is created.
138 If totalLength is not provided, it is read from the buffers in the list.
139 However, this adds an additional loop to the function, so it is faster
140 to provide the length explicitly.
142 ### Class Method: Buffer.compare(buf1, buf2)
147 The same as [`buf1.compare(buf2)`](#buffer_buf_compare_otherbuffer). Useful
148 for sorting an Array of Buffers:
150 var arr = [Buffer('1234'), Buffer('0123')];
151 arr.sort(Buffer.compare);
158 The size of the buffer in bytes. Note that this is not necessarily the size
159 of the contents. `length` refers to the amount of memory allocated for the
160 buffer object. It does not change when the contents of the buffer are changed.
162 buf = new Buffer(1234);
164 console.log(buf.length);
165 buf.write("some string", 0, "ascii");
166 console.log(buf.length);
171 ### buf.write(string[, offset][, length][, encoding])
173 * `string` String - data to be written to buffer
174 * `offset` Number, Optional, Default: 0
175 * `length` Number, Optional, Default: `buffer.length - offset`
176 * `encoding` String, Optional, Default: 'utf8'
178 Writes `string` to the buffer at `offset` using the given encoding.
179 `offset` defaults to `0`, `encoding` defaults to `'utf8'`. `length` is
180 the number of bytes to write. Returns number of octets written. If `buffer` did
181 not contain enough space to fit the entire string, it will write a partial
182 amount of the string. `length` defaults to `buffer.length - offset`.
183 The method will not write partial characters.
185 buf = new Buffer(256);
186 len = buf.write('\u00bd + \u00bc = \u00be', 0);
187 console.log(len + " bytes: " + buf.toString('utf8', 0, len));
189 ### buf.writeUIntLE(value, offset, byteLength[, noAssert])
190 ### buf.writeUIntBE(value, offset, byteLength[, noAssert])
191 ### buf.writeIntLE(value, offset, byteLength[, noAssert])
192 ### buf.writeIntBE(value, offset, byteLength[, noAssert])
194 * `value` {Number} Bytes to be written to buffer
195 * `offset` {Number} `0 <= offset <= buf.length`
196 * `byteLength` {Number} `0 < byteLength <= 6`
197 * `noAssert` {Boolean} Default: false
200 Writes `value` to the buffer at the specified `offset` and `byteLength`.
201 Supports up to 48 bits of accuracy. For example:
203 var b = new Buffer(6);
204 b.writeUIntBE(0x1234567890ab, 0, 6);
205 // <Buffer 12 34 56 78 90 ab>
207 Set `noAssert` to `true` to skip validation of `value` and `offset`. Defaults
210 ### buf.readUIntLE(offset, byteLength[, noAssert])
211 ### buf.readUIntBE(offset, byteLength[, noAssert])
212 ### buf.readIntLE(offset, byteLength[, noAssert])
213 ### buf.readIntBE(offset, byteLength[, noAssert])
215 * `offset` {Number} `0 <= offset <= buf.length`
216 * `byteLength` {Number} `0 < byteLength <= 6`
217 * `noAssert` {Boolean} Default: false
220 A generalized version of all numeric read methods. Supports up to 48 bits of
221 accuracy. For example:
223 var b = new Buffer(6);
224 b.writeUint16LE(0x90ab, 0);
225 b.writeUInt32LE(0x12345678, 2);
226 b.readUIntLE(0, 6).toString(16); // Specify 6 bytes (48 bits)
227 // output: '1234567890ab'
229 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
230 may be beyond the end of the buffer. Defaults to `false`.
232 ### buf.toString([encoding][, start][, end])
234 * `encoding` String, Optional, Default: 'utf8'
235 * `start` Number, Optional, Default: 0
236 * `end` Number, Optional, Default: `buffer.length`
238 Decodes and returns a string from buffer data encoded with `encoding`
239 (defaults to `'utf8'`) beginning at `start` (defaults to `0`) and ending at
240 `end` (defaults to `buffer.length`).
242 See `buffer.write()` example, above.
247 Returns a JSON-representation of the Buffer instance. `JSON.stringify`
248 implicitly calls this function when stringifying a Buffer instance.
252 var buf = new Buffer('test');
253 var json = JSON.stringify(buf);
256 // '{"type":"Buffer","data":[116,101,115,116]}'
258 var copy = JSON.parse(json, function(key, value) {
259 return value && value.type === 'Buffer'
260 ? new Buffer(value.data)
265 // <Buffer 74 65 73 74>
272 Get and set the octet at `index`. The values refer to individual bytes,
273 so the legal range is between `0x00` and `0xFF` hex or `0` and `255`.
275 Example: copy an ASCII string into a buffer, one byte at a time:
278 buf = new Buffer(str.length);
280 for (var i = 0; i < str.length ; i++) {
281 buf[i] = str.charCodeAt(i);
288 ### buf.equals(otherBuffer)
290 * `otherBuffer` {Buffer}
292 Returns a boolean of whether `this` and `otherBuffer` have the same
295 ### buf.compare(otherBuffer)
297 * `otherBuffer` {Buffer}
299 Returns a number indicating whether `this` comes before or after or is
300 the same as the `otherBuffer` in sort order.
303 ### buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])
305 * `targetBuffer` Buffer object - Buffer to copy into
306 * `targetStart` Number, Optional, Default: 0
307 * `sourceStart` Number, Optional, Default: 0
308 * `sourceEnd` Number, Optional, Default: `buffer.length`
310 Does copy between buffers. The source and target regions can be overlapped.
311 `targetStart` and `sourceStart` default to `0`.
312 `sourceEnd` defaults to `buffer.length`.
314 All values passed that are `undefined`/`NaN` or are out of bounds are set equal
315 to their respective defaults.
317 Example: build two Buffers, then copy `buf1` from byte 16 through byte 19
318 into `buf2`, starting at the 8th byte in `buf2`.
320 buf1 = new Buffer(26);
321 buf2 = new Buffer(26);
323 for (var i = 0 ; i < 26 ; i++) {
324 buf1[i] = i + 97; // 97 is ASCII a
325 buf2[i] = 33; // ASCII !
328 buf1.copy(buf2, 8, 16, 20);
329 console.log(buf2.toString('ascii', 0, 25));
331 // !!!!!!!!qrst!!!!!!!!!!!!!
334 ### buf.slice([start][, end])
336 * `start` Number, Optional, Default: 0
337 * `end` Number, Optional, Default: `buffer.length`
339 Returns a new buffer which references the same memory as the old, but offset
340 and cropped by the `start` (defaults to `0`) and `end` (defaults to
341 `buffer.length`) indexes. Negative indexes start from the end of the buffer.
343 **Modifying the new buffer slice will modify memory in the original buffer!**
345 Example: build a Buffer with the ASCII alphabet, take a slice, then modify one
346 byte from the original Buffer.
348 var buf1 = new Buffer(26);
350 for (var i = 0 ; i < 26 ; i++) {
351 buf1[i] = i + 97; // 97 is ASCII a
354 var buf2 = buf1.slice(0, 3);
355 console.log(buf2.toString('ascii', 0, buf2.length));
357 console.log(buf2.toString('ascii', 0, buf2.length));
362 ### buf.readUInt8(offset[, noAssert])
365 * `noAssert` Boolean, Optional, Default: false
368 Reads an unsigned 8 bit integer from the buffer at the specified offset.
370 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
371 may be beyond the end of the buffer. Defaults to `false`.
375 var buf = new Buffer(4);
382 for (ii = 0; ii < buf.length; ii++) {
383 console.log(buf.readUInt8(ii));
391 ### buf.readUInt16LE(offset[, noAssert])
392 ### buf.readUInt16BE(offset[, noAssert])
395 * `noAssert` Boolean, Optional, Default: false
398 Reads an unsigned 16 bit integer from the buffer at the specified offset with
399 specified endian format.
401 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
402 may be beyond the end of the buffer. Defaults to `false`.
406 var buf = new Buffer(4);
413 console.log(buf.readUInt16BE(0));
414 console.log(buf.readUInt16LE(0));
415 console.log(buf.readUInt16BE(1));
416 console.log(buf.readUInt16LE(1));
417 console.log(buf.readUInt16BE(2));
418 console.log(buf.readUInt16LE(2));
427 ### buf.readUInt32LE(offset[, noAssert])
428 ### buf.readUInt32BE(offset[, noAssert])
431 * `noAssert` Boolean, Optional, Default: false
434 Reads an unsigned 32 bit integer from the buffer at the specified offset with
435 specified endian format.
437 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
438 may be beyond the end of the buffer. Defaults to `false`.
442 var buf = new Buffer(4);
449 console.log(buf.readUInt32BE(0));
450 console.log(buf.readUInt32LE(0));
455 ### buf.readInt8(offset[, noAssert])
458 * `noAssert` Boolean, Optional, Default: false
461 Reads a signed 8 bit integer from the buffer at the specified offset.
463 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
464 may be beyond the end of the buffer. Defaults to `false`.
466 Works as `buffer.readUInt8`, except buffer contents are treated as two's
467 complement signed values.
469 ### buf.readInt16LE(offset[, noAssert])
470 ### buf.readInt16BE(offset[, noAssert])
473 * `noAssert` Boolean, Optional, Default: false
476 Reads a signed 16 bit integer from the buffer at the specified offset with
477 specified endian format.
479 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
480 may be beyond the end of the buffer. Defaults to `false`.
482 Works as `buffer.readUInt16*`, except buffer contents are treated as two's
483 complement signed values.
485 ### buf.readInt32LE(offset[, noAssert])
486 ### buf.readInt32BE(offset[, noAssert])
489 * `noAssert` Boolean, Optional, Default: false
492 Reads a signed 32 bit integer from the buffer at the specified offset with
493 specified endian format.
495 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
496 may be beyond the end of the buffer. Defaults to `false`.
498 Works as `buffer.readUInt32*`, except buffer contents are treated as two's
499 complement signed values.
501 ### buf.readFloatLE(offset[, noAssert])
502 ### buf.readFloatBE(offset[, noAssert])
505 * `noAssert` Boolean, Optional, Default: false
508 Reads a 32 bit float from the buffer at the specified offset with specified
511 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
512 may be beyond the end of the buffer. Defaults to `false`.
516 var buf = new Buffer(4);
523 console.log(buf.readFloatLE(0));
527 ### buf.readDoubleLE(offset[, noAssert])
528 ### buf.readDoubleBE(offset[, noAssert])
531 * `noAssert` Boolean, Optional, Default: false
534 Reads a 64 bit double from the buffer at the specified offset with specified
537 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
538 may be beyond the end of the buffer. Defaults to `false`.
542 var buf = new Buffer(8);
553 console.log(buf.readDoubleLE(0));
555 // 0.3333333333333333
557 ### buf.writeUInt8(value, offset[, noAssert])
561 * `noAssert` Boolean, Optional, Default: false
563 Writes `value` to the buffer at the specified offset. Note, `value` must be a
564 valid unsigned 8 bit integer.
566 Set `noAssert` to true to skip validation of `value` and `offset`. This means
567 that `value` may be too large for the specific function and `offset` may be
568 beyond the end of the buffer leading to the values being silently dropped. This
569 should not be used unless you are certain of correctness. Defaults to `false`.
573 var buf = new Buffer(4);
574 buf.writeUInt8(0x3, 0);
575 buf.writeUInt8(0x4, 1);
576 buf.writeUInt8(0x23, 2);
577 buf.writeUInt8(0x42, 3);
581 // <Buffer 03 04 23 42>
583 ### buf.writeUInt16LE(value, offset[, noAssert])
584 ### buf.writeUInt16BE(value, offset[, noAssert])
588 * `noAssert` Boolean, Optional, Default: false
590 Writes `value` to the buffer at the specified offset with specified endian
591 format. Note, `value` must be a valid unsigned 16 bit integer.
593 Set `noAssert` to true to skip validation of `value` and `offset`. This means
594 that `value` may be too large for the specific function and `offset` may be
595 beyond the end of the buffer leading to the values being silently dropped. This
596 should not be used unless you are certain of correctness. Defaults to `false`.
600 var buf = new Buffer(4);
601 buf.writeUInt16BE(0xdead, 0);
602 buf.writeUInt16BE(0xbeef, 2);
606 buf.writeUInt16LE(0xdead, 0);
607 buf.writeUInt16LE(0xbeef, 2);
611 // <Buffer de ad be ef>
612 // <Buffer ad de ef be>
614 ### buf.writeUInt32LE(value, offset[, noAssert])
615 ### buf.writeUInt32BE(value, offset[, noAssert])
619 * `noAssert` Boolean, Optional, Default: false
621 Writes `value` to the buffer at the specified offset with specified endian
622 format. Note, `value` must be a valid unsigned 32 bit integer.
624 Set `noAssert` to true to skip validation of `value` and `offset`. This means
625 that `value` may be too large for the specific function and `offset` may be
626 beyond the end of the buffer leading to the values being silently dropped. This
627 should not be used unless you are certain of correctness. Defaults to `false`.
631 var buf = new Buffer(4);
632 buf.writeUInt32BE(0xfeedface, 0);
636 buf.writeUInt32LE(0xfeedface, 0);
640 // <Buffer fe ed fa ce>
641 // <Buffer ce fa ed fe>
643 ### buf.writeInt8(value, offset[, noAssert])
647 * `noAssert` Boolean, Optional, Default: false
649 Writes `value` to the buffer at the specified offset. Note, `value` must be a
650 valid signed 8 bit integer.
652 Set `noAssert` to true to skip validation of `value` and `offset`. This means
653 that `value` may be too large for the specific function and `offset` may be
654 beyond the end of the buffer leading to the values being silently dropped. This
655 should not be used unless you are certain of correctness. Defaults to `false`.
657 Works as `buffer.writeUInt8`, except value is written out as a two's complement
658 signed integer into `buffer`.
660 ### buf.writeInt16LE(value, offset[, noAssert])
661 ### buf.writeInt16BE(value, offset[, noAssert])
665 * `noAssert` Boolean, Optional, Default: false
667 Writes `value` to the buffer at the specified offset with specified endian
668 format. Note, `value` must be a valid signed 16 bit integer.
670 Set `noAssert` to true to skip validation of `value` and `offset`. This means
671 that `value` may be too large for the specific function and `offset` may be
672 beyond the end of the buffer leading to the values being silently dropped. This
673 should not be used unless you are certain of correctness. Defaults to `false`.
675 Works as `buffer.writeUInt16*`, except value is written out as a two's
676 complement signed integer into `buffer`.
678 ### buf.writeInt32LE(value, offset[, noAssert])
679 ### buf.writeInt32BE(value, offset[, noAssert])
683 * `noAssert` Boolean, Optional, Default: false
685 Writes `value` to the buffer at the specified offset with specified endian
686 format. Note, `value` must be a valid signed 32 bit integer.
688 Set `noAssert` to true to skip validation of `value` and `offset`. This means
689 that `value` may be too large for the specific function and `offset` may be
690 beyond the end of the buffer leading to the values being silently dropped. This
691 should not be used unless you are certain of correctness. Defaults to `false`.
693 Works as `buffer.writeUInt32*`, except value is written out as a two's
694 complement signed integer into `buffer`.
696 ### buf.writeFloatLE(value, offset[, noAssert])
697 ### buf.writeFloatBE(value, offset[, noAssert])
701 * `noAssert` Boolean, Optional, Default: false
703 Writes `value` to the buffer at the specified offset with specified endian
704 format. Note, behavior is unspecified if `value` is not a 32 bit float.
706 Set `noAssert` to true to skip validation of `value` and `offset`. This means
707 that `value` may be too large for the specific function and `offset` may be
708 beyond the end of the buffer leading to the values being silently dropped. This
709 should not be used unless you are certain of correctness. Defaults to `false`.
713 var buf = new Buffer(4);
714 buf.writeFloatBE(0xcafebabe, 0);
718 buf.writeFloatLE(0xcafebabe, 0);
722 // <Buffer 4f 4a fe bb>
723 // <Buffer bb fe 4a 4f>
725 ### buf.writeDoubleLE(value, offset[, noAssert])
726 ### buf.writeDoubleBE(value, offset[, noAssert])
730 * `noAssert` Boolean, Optional, Default: false
732 Writes `value` to the buffer at the specified offset with specified endian
733 format. Note, `value` must be a valid 64 bit double.
735 Set `noAssert` to true to skip validation of `value` and `offset`. This means
736 that `value` may be too large for the specific function and `offset` may be
737 beyond the end of the buffer leading to the values being silently dropped. This
738 should not be used unless you are certain of correctness. Defaults to `false`.
742 var buf = new Buffer(8);
743 buf.writeDoubleBE(0xdeadbeefcafebabe, 0);
747 buf.writeDoubleLE(0xdeadbeefcafebabe, 0);
751 // <Buffer 43 eb d5 b7 dd f9 5f d7>
752 // <Buffer d7 5f f9 dd b7 d5 eb 43>
754 ### buf.fill(value[, offset][, end])
757 * `offset` Number, Optional
758 * `end` Number, Optional
760 Fills the buffer with the specified value. If the `offset` (defaults to `0`)
761 and `end` (defaults to `buffer.length`) are not given it will fill the entire
764 var b = new Buffer(50);
767 ## buffer.INSPECT_MAX_BYTES
769 * Number, Default: 50
771 How many bytes will be returned when `buffer.inspect()` is called. This can
772 be overridden by user modules.
774 Note that this is a property on the buffer module returned by
775 `require('buffer')`, not on the Buffer global, or a buffer instance.
779 Returns an un-pooled `Buffer`.
781 In order to avoid the garbage collection overhead of creating many individually
782 allocated Buffers, by default allocations under 4KB are sliced from a single
783 larger allocated object. This approach improves both performance and memory
784 usage since v8 does not need to track and cleanup as many `Persistent` objects.
786 In the case where a developer may need to retain a small chunk of memory from a
787 pool for an indeterminate amount of time it may be appropriate to create an
788 un-pooled Buffer instance using SlowBuffer and copy out the relevant bits.
790 // need to keep around a few small chunks of memory
793 socket.on('readable', function() {
794 var data = socket.read();
795 // allocate for retained data
796 var sb = new SlowBuffer(10);
797 // copy the data into the new allocation
798 data.copy(sb, 0, 0, 10);
802 Though this should used sparingly and only be a last resort *after* a developer
803 has actively observed undue memory retention in their applications.