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 A `Buffer` object can also be used with typed arrays. The buffer object is
41 cloned to an `ArrayBuffer` that is used as the backing store for the typed
42 array. The memory of the buffer and the `ArrayBuffer` is not shared.
44 NOTE: Node.js v0.8 simply retained a reference to the buffer in `array.buffer`
45 instead of cloning it.
47 While more efficient, it introduces subtle incompatibilities with the typed
48 arrays specification. `ArrayBuffer#slice()` makes a copy of the slice while
49 `Buffer#slice()` creates a view.
53 The Buffer class is a global type for dealing with binary data directly.
54 It can be constructed in a variety of ways.
60 Allocates a new buffer of `size` octets.
66 Allocates a new buffer using an `array` of octets.
68 ### new Buffer(str, [encoding])
70 * `str` String - string to encode.
71 * `encoding` String - encoding to use, Optional.
73 Allocates a new buffer containing the given `str`.
74 `encoding` defaults to `'utf8'`.
76 ### Class Method: Buffer.isEncoding(encoding)
78 * `encoding` {String} The encoding string to test
80 Returns true if the `encoding` is a valid encoding argument, or false
83 ### Class Method: Buffer.isBuffer(obj)
88 Tests if `obj` is a `Buffer`.
90 ### Class Method: Buffer.byteLength(string, [encoding])
93 * `encoding` String, Optional, Default: 'utf8'
96 Gives the actual byte length of a string. `encoding` defaults to `'utf8'`.
97 This is not the same as `String.prototype.length` since that returns the
98 number of *characters* in a string.
102 str = '\u00bd + \u00bc = \u00be';
104 console.log(str + ": " + str.length + " characters, " +
105 Buffer.byteLength(str, 'utf8') + " bytes");
107 // ½ + ¼ = ¾: 9 characters, 12 bytes
109 ### Class Method: Buffer.concat(list, [totalLength])
111 * `list` {Array} List of Buffer objects to concat
112 * `totalLength` {Number} Total length of the buffers when concatenated
114 Returns a buffer which is the result of concatenating all the buffers in
117 If the list has no items, or if the totalLength is 0, then it returns a
120 If the list has exactly one item, then the first item of the list is
123 If the list has more than one item, then a new Buffer is created.
125 If totalLength is not provided, it is read from the buffers in the list.
126 However, this adds an additional loop to the function, so it is faster
127 to provide the length explicitly.
129 ### Class Method: Buffer.alloc(length, [receiver])
132 * `receiver` Object, Optional, Default: `new Object`
135 **(EXPERIMENTAL)** Returns object with allocated external array data.
137 Buffers are backed by a simple allocator that only handles the assignation of
138 external raw memory. This exposes that functionality.
140 No pooling is performed for these allocations. So there's no form of memory
143 This can be used to create your own Buffer-like classes.
145 function SimpleData(n) {
147 Buffer.alloc(this.length, this);
150 SimpleData.prototype = { /* ... */ };
152 ### Class Method: Buffer.dispose(obj)
157 **(EXPERIMENTAL)** Free memory that has been allocated to an object via
163 // { '0': 0, '1': 0, '2': 0 }
173 The size of the buffer in bytes. Note that this is not necessarily the size
174 of the contents. `length` refers to the amount of memory allocated for the
175 buffer object. It does not change when the contents of the buffer are changed.
177 buf = new Buffer(1234);
179 console.log(buf.length);
180 buf.write("some string", 0, "ascii");
181 console.log(buf.length);
186 ### buf.write(string, [offset], [length], [encoding])
188 * `string` String - data to be written to buffer
189 * `offset` Number, Optional, Default: 0
190 * `length` Number, Optional, Default: `buffer.length - offset`
191 * `encoding` String, Optional, Default: 'utf8'
193 Writes `string` to the buffer at `offset` using the given encoding.
194 `offset` defaults to `0`, `encoding` defaults to `'utf8'`. `length` is
195 the number of bytes to write. Returns number of octets written. If `buffer` did
196 not contain enough space to fit the entire string, it will write a partial
197 amount of the string. `length` defaults to `buffer.length - offset`.
198 The method will not write partial characters.
200 buf = new Buffer(256);
201 len = buf.write('\u00bd + \u00bc = \u00be', 0);
202 console.log(len + " bytes: " + buf.toString('utf8', 0, len));
205 ### buf.toString([encoding], [start], [end])
207 * `encoding` String, Optional, Default: 'utf8'
208 * `start` Number, Optional, Default: 0
209 * `end` Number, Optional, Default: `buffer.length`
211 Decodes and returns a string from buffer data encoded with `encoding`
212 (defaults to `'utf8'`) beginning at `start` (defaults to `0`) and ending at
213 `end` (defaults to `buffer.length`).
215 See `buffer.write()` example, above.
220 Returns a JSON-representation of the Buffer instance. `JSON.stringify`
221 implicitly calls this function when stringifying a Buffer instance.
225 var buf = new Buffer('test');
226 var json = JSON.stringify(buf);
229 // '{"type":"Buffer","data":[116,101,115,116]}'
231 var copy = JSON.parse(json, function(key, value) {
232 return value && value.type === 'Buffer'
233 ? new Buffer(value.data)
238 // <Buffer 74 65 73 74>
245 Get and set the octet at `index`. The values refer to individual bytes,
246 so the legal range is between `0x00` and `0xFF` hex or `0` and `255`.
248 Example: copy an ASCII string into a buffer, one byte at a time:
251 buf = new Buffer(str.length);
253 for (var i = 0; i < str.length ; i++) {
254 buf[i] = str.charCodeAt(i);
261 ### buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd])
263 * `targetBuffer` Buffer object - Buffer to copy into
264 * `targetStart` Number, Optional, Default: 0
265 * `sourceStart` Number, Optional, Default: 0
266 * `sourceEnd` Number, Optional, Default: `buffer.length`
268 Does copy between buffers. The source and target regions can be overlapped.
269 `targetStart` and `sourceStart` default to `0`.
270 `sourceEnd` defaults to `buffer.length`.
272 All values passed that are `undefined`/`NaN` or are out of bounds are set equal
273 to their respective defaults.
275 Example: build two Buffers, then copy `buf1` from byte 16 through byte 19
276 into `buf2`, starting at the 8th byte in `buf2`.
278 buf1 = new Buffer(26);
279 buf2 = new Buffer(26);
281 for (var i = 0 ; i < 26 ; i++) {
282 buf1[i] = i + 97; // 97 is ASCII a
283 buf2[i] = 33; // ASCII !
286 buf1.copy(buf2, 8, 16, 20);
287 console.log(buf2.toString('ascii', 0, 25));
289 // !!!!!!!!qrst!!!!!!!!!!!!!
292 ### buf.slice([start], [end])
294 * `start` Number, Optional, Default: 0
295 * `end` Number, Optional, Default: `buffer.length`
297 Returns a new buffer which references the same memory as the old, but offset
298 and cropped by the `start` (defaults to `0`) and `end` (defaults to
299 `buffer.length`) indexes. Negative indexes start from the end of the buffer.
301 **Modifying the new buffer slice will modify memory in the original buffer!**
303 Example: build a Buffer with the ASCII alphabet, take a slice, then modify one
304 byte from the original Buffer.
306 var buf1 = new Buffer(26);
308 for (var i = 0 ; i < 26 ; i++) {
309 buf1[i] = i + 97; // 97 is ASCII a
312 var buf2 = buf1.slice(0, 3);
313 console.log(buf2.toString('ascii', 0, buf2.length));
315 console.log(buf2.toString('ascii', 0, buf2.length));
320 ### buf.readUInt8(offset, [noAssert])
323 * `noAssert` Boolean, Optional, Default: false
326 Reads an unsigned 8 bit integer from the buffer at the specified offset.
328 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
329 may be beyond the end of the buffer. Defaults to `false`.
333 var buf = new Buffer(4);
340 for (ii = 0; ii < buf.length; ii++) {
341 console.log(buf.readUInt8(ii));
349 ### buf.readUInt16LE(offset, [noAssert])
350 ### buf.readUInt16BE(offset, [noAssert])
353 * `noAssert` Boolean, Optional, Default: false
356 Reads an unsigned 16 bit integer from the buffer at the specified offset with
357 specified endian format.
359 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
360 may be beyond the end of the buffer. Defaults to `false`.
364 var buf = new Buffer(4);
371 console.log(buf.readUInt16BE(0));
372 console.log(buf.readUInt16LE(0));
373 console.log(buf.readUInt16BE(1));
374 console.log(buf.readUInt16LE(1));
375 console.log(buf.readUInt16BE(2));
376 console.log(buf.readUInt16LE(2));
385 ### buf.readUInt32LE(offset, [noAssert])
386 ### buf.readUInt32BE(offset, [noAssert])
389 * `noAssert` Boolean, Optional, Default: false
392 Reads an unsigned 32 bit integer from the buffer at the specified offset with
393 specified endian format.
395 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
396 may be beyond the end of the buffer. Defaults to `false`.
400 var buf = new Buffer(4);
407 console.log(buf.readUInt32BE(0));
408 console.log(buf.readUInt32LE(0));
413 ### buf.readInt8(offset, [noAssert])
416 * `noAssert` Boolean, Optional, Default: false
419 Reads a signed 8 bit integer from the buffer at the specified offset.
421 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
422 may be beyond the end of the buffer. Defaults to `false`.
424 Works as `buffer.readUInt8`, except buffer contents are treated as two's
425 complement signed values.
427 ### buf.readInt16LE(offset, [noAssert])
428 ### buf.readInt16BE(offset, [noAssert])
431 * `noAssert` Boolean, Optional, Default: false
434 Reads a signed 16 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`.
440 Works as `buffer.readUInt16*`, except buffer contents are treated as two's
441 complement signed values.
443 ### buf.readInt32LE(offset, [noAssert])
444 ### buf.readInt32BE(offset, [noAssert])
447 * `noAssert` Boolean, Optional, Default: false
450 Reads a signed 32 bit integer from the buffer at the specified offset with
451 specified endian format.
453 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
454 may be beyond the end of the buffer. Defaults to `false`.
456 Works as `buffer.readUInt32*`, except buffer contents are treated as two's
457 complement signed values.
459 ### buf.readFloatLE(offset, [noAssert])
460 ### buf.readFloatBE(offset, [noAssert])
463 * `noAssert` Boolean, Optional, Default: false
466 Reads a 32 bit float from the buffer at the specified offset with specified
469 Set `noAssert` to true to skip validation of `offset`. This means that `offset`
470 may be beyond the end of the buffer. Defaults to `false`.
474 var buf = new Buffer(4);
481 console.log(buf.readFloatLE(0));
485 ### buf.readDoubleLE(offset, [noAssert])
486 ### buf.readDoubleBE(offset, [noAssert])
489 * `noAssert` Boolean, Optional, Default: false
492 Reads a 64 bit double from the buffer at the specified offset with specified
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`.
500 var buf = new Buffer(8);
511 console.log(buf.readDoubleLE(0));
513 // 0.3333333333333333
515 ### buf.writeUInt8(value, offset, [noAssert])
519 * `noAssert` Boolean, Optional, Default: false
521 Writes `value` to the buffer at the specified offset. Note, `value` must be a
522 valid unsigned 8 bit integer.
524 Set `noAssert` to true to skip validation of `value` and `offset`. This means
525 that `value` may be too large for the specific function and `offset` may be
526 beyond the end of the buffer leading to the values being silently dropped. This
527 should not be used unless you are certain of correctness. Defaults to `false`.
531 var buf = new Buffer(4);
532 buf.writeUInt8(0x3, 0);
533 buf.writeUInt8(0x4, 1);
534 buf.writeUInt8(0x23, 2);
535 buf.writeUInt8(0x42, 3);
539 // <Buffer 03 04 23 42>
541 ### buf.writeUInt16LE(value, offset, [noAssert])
542 ### buf.writeUInt16BE(value, offset, [noAssert])
546 * `noAssert` Boolean, Optional, Default: false
548 Writes `value` to the buffer at the specified offset with specified endian
549 format. Note, `value` must be a valid unsigned 16 bit integer.
551 Set `noAssert` to true to skip validation of `value` and `offset`. This means
552 that `value` may be too large for the specific function and `offset` may be
553 beyond the end of the buffer leading to the values being silently dropped. This
554 should not be used unless you are certain of correctness. Defaults to `false`.
558 var buf = new Buffer(4);
559 buf.writeUInt16BE(0xdead, 0);
560 buf.writeUInt16BE(0xbeef, 2);
564 buf.writeUInt16LE(0xdead, 0);
565 buf.writeUInt16LE(0xbeef, 2);
569 // <Buffer de ad be ef>
570 // <Buffer ad de ef be>
572 ### buf.writeUInt32LE(value, offset, [noAssert])
573 ### buf.writeUInt32BE(value, offset, [noAssert])
577 * `noAssert` Boolean, Optional, Default: false
579 Writes `value` to the buffer at the specified offset with specified endian
580 format. Note, `value` must be a valid unsigned 32 bit integer.
582 Set `noAssert` to true to skip validation of `value` and `offset`. This means
583 that `value` may be too large for the specific function and `offset` may be
584 beyond the end of the buffer leading to the values being silently dropped. This
585 should not be used unless you are certain of correctness. Defaults to `false`.
589 var buf = new Buffer(4);
590 buf.writeUInt32BE(0xfeedface, 0);
594 buf.writeUInt32LE(0xfeedface, 0);
598 // <Buffer fe ed fa ce>
599 // <Buffer ce fa ed fe>
601 ### buf.writeInt8(value, offset, [noAssert])
605 * `noAssert` Boolean, Optional, Default: false
607 Writes `value` to the buffer at the specified offset. Note, `value` must be a
608 valid signed 8 bit integer.
610 Set `noAssert` to true to skip validation of `value` and `offset`. This means
611 that `value` may be too large for the specific function and `offset` may be
612 beyond the end of the buffer leading to the values being silently dropped. This
613 should not be used unless you are certain of correctness. Defaults to `false`.
615 Works as `buffer.writeUInt8`, except value is written out as a two's complement
616 signed integer into `buffer`.
618 ### buf.writeInt16LE(value, offset, [noAssert])
619 ### buf.writeInt16BE(value, offset, [noAssert])
623 * `noAssert` Boolean, Optional, Default: false
625 Writes `value` to the buffer at the specified offset with specified endian
626 format. Note, `value` must be a valid signed 16 bit integer.
628 Set `noAssert` to true to skip validation of `value` and `offset`. This means
629 that `value` may be too large for the specific function and `offset` may be
630 beyond the end of the buffer leading to the values being silently dropped. This
631 should not be used unless you are certain of correctness. Defaults to `false`.
633 Works as `buffer.writeUInt16*`, except value is written out as a two's
634 complement signed integer into `buffer`.
636 ### buf.writeInt32LE(value, offset, [noAssert])
637 ### buf.writeInt32BE(value, offset, [noAssert])
641 * `noAssert` Boolean, Optional, Default: false
643 Writes `value` to the buffer at the specified offset with specified endian
644 format. Note, `value` must be a valid signed 32 bit integer.
646 Set `noAssert` to true to skip validation of `value` and `offset`. This means
647 that `value` may be too large for the specific function and `offset` may be
648 beyond the end of the buffer leading to the values being silently dropped. This
649 should not be used unless you are certain of correctness. Defaults to `false`.
651 Works as `buffer.writeUInt32*`, except value is written out as a two's
652 complement signed integer into `buffer`.
654 ### buf.writeFloatLE(value, offset, [noAssert])
655 ### buf.writeFloatBE(value, offset, [noAssert])
659 * `noAssert` Boolean, Optional, Default: false
661 Writes `value` to the buffer at the specified offset with specified endian
662 format. Note, behavior is unspecified if `value` is not a 32 bit float.
664 Set `noAssert` to true to skip validation of `value` and `offset`. This means
665 that `value` may be too large for the specific function and `offset` may be
666 beyond the end of the buffer leading to the values being silently dropped. This
667 should not be used unless you are certain of correctness. Defaults to `false`.
671 var buf = new Buffer(4);
672 buf.writeFloatBE(0xcafebabe, 0);
676 buf.writeFloatLE(0xcafebabe, 0);
680 // <Buffer 4f 4a fe bb>
681 // <Buffer bb fe 4a 4f>
683 ### buf.writeDoubleLE(value, offset, [noAssert])
684 ### buf.writeDoubleBE(value, offset, [noAssert])
688 * `noAssert` Boolean, Optional, Default: false
690 Writes `value` to the buffer at the specified offset with specified endian
691 format. Note, `value` must be a valid 64 bit double.
693 Set `noAssert` to true to skip validation of `value` and `offset`. This means
694 that `value` may be too large for the specific function and `offset` may be
695 beyond the end of the buffer leading to the values being silently dropped. This
696 should not be used unless you are certain of correctness. Defaults to `false`.
700 var buf = new Buffer(8);
701 buf.writeDoubleBE(0xdeadbeefcafebabe, 0);
705 buf.writeDoubleLE(0xdeadbeefcafebabe, 0);
709 // <Buffer 43 eb d5 b7 dd f9 5f d7>
710 // <Buffer d7 5f f9 dd b7 d5 eb 43>
712 ### buf.fill(value, [offset], [end])
715 * `offset` Number, Optional
716 * `end` Number, Optional
718 Fills the buffer with the specified value. If the `offset` (defaults to `0`)
719 and `end` (defaults to `buffer.length`) are not given it will fill the entire
722 var b = new Buffer(50);
725 ## buffer.INSPECT_MAX_BYTES
727 * Number, Default: 50
729 How many bytes will be returned when `buffer.inspect()` is called. This can
730 be overridden by user modules.
732 Note that this is a property on the buffer module returned by
733 `require('buffer')`, not on the Buffer global, or a buffer instance.
737 Returns an un-pooled `Buffer`.
739 In order to avoid the garbage collection overhead of creating many individually
740 allocated Buffers, by default allocations under 4KB are sliced from a single
741 larger allocated object. This approach improves both performance and memory
742 usage since v8 does not need to track and cleanup as many `Persistent` objects.
744 In the case where a developer may need to retain a small chunk of memory from a
745 pool for an indeterminate amount of time it may be appropriate to create an
746 un-pooled Buffer instance using SlowBuffer and copy out the relevant bits.
748 // need to keep around a few small chunks of memory
751 socket.on('readable', function() {
752 var data = socket.read();
753 // allocate for retained data
754 var sb = new SlowBuffer(10);
755 // copy the data into the new allocation
756 data.copy(sb, 0, 0, 10);
760 Though this should used sparingly and only be a last resort *after* a developer
761 has actively observed undue memory retention in their applications.