Merge remote-tracking branch 'joyent/v0.12'
[platform/upstream/nodejs.git] / doc / api / buffer.markdown
1 # Buffer
2
3     Stability: 3 - Stable
4
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.
9
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.
13
14 The `Buffer` class is a global, making it very rare that one would need
15 to ever `require('buffer')`.
16
17 Converting between Buffers and JavaScript string objects requires an explicit
18 encoding method.  Here are the different string encodings.
19
20 * `'ascii'` - for 7 bit ASCII data only.  This encoding method is very fast, and
21   will strip the high bit if set.
22
23 * `'utf8'` - Multibyte encoded Unicode characters. Many web pages and other
24   document formats use UTF-8.
25
26 * `'utf16le'` - 2 or 4 bytes, little endian encoded Unicode characters.
27   Surrogate pairs (U+10000 to U+10FFFF) are supported.
28
29 * `'ucs2'` - Alias of `'utf16le'`.
30
31 * `'base64'` - Base64 string encoding.
32
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.
37
38 * `'hex'` - Encode each byte as two hexadecimal characters.
39
40 Creating a typed array from a `Buffer` works with the following caveats:
41
42 1. The buffer's memory is copied, not shared.
43
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]`.
48
49 NOTE: Node.js v0.8 simply retained a reference to the buffer in `array.buffer`
50 instead of cloning it.
51
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.
55
56 ## Class: Buffer
57
58 The Buffer class is a global type for dealing with binary data directly.
59 It can be constructed in a variety of ways.
60
61 ### new Buffer(size)
62
63 * `size` Number
64
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`
67 will be thrown here.
68
69 ### new Buffer(array)
70
71 * `array` Array
72
73 Allocates a new buffer using an `array` of octets.
74
75 ### new Buffer(buffer)
76
77 * `buffer` {Buffer}
78
79 Copies the passed `buffer` data onto a new `Buffer` instance.
80
81 ### new Buffer(str[, encoding])
82
83 * `str` String - string to encode.
84 * `encoding` String - encoding to use, Optional.
85
86 Allocates a new buffer containing the given `str`.
87 `encoding` defaults to `'utf8'`.
88
89 ### Class Method: Buffer.isEncoding(encoding)
90
91 * `encoding` {String} The encoding string to test
92
93 Returns true if the `encoding` is a valid encoding argument, or false
94 otherwise.
95
96 ### Class Method: Buffer.isBuffer(obj)
97
98 * `obj` Object
99 * Return: Boolean
100
101 Tests if `obj` is a `Buffer`.
102
103 ### Class Method: Buffer.byteLength(string[, encoding])
104
105 * `string` String
106 * `encoding` String, Optional, Default: 'utf8'
107 * Return: Number
108
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.
112
113 Example:
114
115     str = '\u00bd + \u00bc = \u00be';
116
117     console.log(str + ": " + str.length + " characters, " +
118       Buffer.byteLength(str, 'utf8') + " bytes");
119
120     // ½ + ¼ = ¾: 9 characters, 12 bytes
121
122 ### Class Method: Buffer.concat(list[, totalLength])
123
124 * `list` {Array} List of Buffer objects to concat
125 * `totalLength` {Number} Total length of the buffers when concatenated
126
127 Returns a buffer which is the result of concatenating all the buffers in
128 the list together.
129
130 If the list has no items, or if the totalLength is 0, then it returns a
131 zero-length buffer.
132
133 If the list has exactly one item, then the first item of the list is
134 returned.
135
136 If the list has more than one item, then a new Buffer is created.
137
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.
141
142 ### Class Method: Buffer.compare(buf1, buf2)
143
144 * `buf1` {Buffer}
145 * `buf2` {Buffer}
146
147 The same as [`buf1.compare(buf2)`](#buffer_buf_compare_otherbuffer). Useful
148 for sorting an Array of Buffers:
149
150     var arr = [Buffer('1234'), Buffer('0123')];
151     arr.sort(Buffer.compare);
152
153
154 ### buf.length
155
156 * Number
157
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.
161
162     buf = new Buffer(1234);
163
164     console.log(buf.length);
165     buf.write("some string", 0, "ascii");
166     console.log(buf.length);
167
168     // 1234
169     // 1234
170
171 ### buf.write(string[, offset][, length][, encoding])
172
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'
177
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.
184
185     buf = new Buffer(256);
186     len = buf.write('\u00bd + \u00bc = \u00be', 0);
187     console.log(len + " bytes: " + buf.toString('utf8', 0, len));
188
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])
193
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
198 * Return: {Number}
199
200 Writes `value` to the buffer at the specified `offset` and `byteLength`.
201 Supports up to 48 bits of accuracy. For example:
202
203     var b = new Buffer(6);
204     b.writeUIntBE(0x1234567890ab, 0, 6);
205     // <Buffer 12 34 56 78 90 ab>
206
207 Set `noAssert` to `true` to skip validation of `value` and `offset`. Defaults
208 to `false`.
209
210 ### buf.readUIntLE(offset, byteLength[, noAssert])
211 ### buf.readUIntBE(offset, byteLength[, noAssert])
212 ### buf.readIntLE(offset, byteLength[, noAssert])
213 ### buf.readIntBE(offset, byteLength[, noAssert])
214
215 * `offset` {Number} `0 <= offset <= buf.length`
216 * `byteLength` {Number} `0 < byteLength <= 6`
217 * `noAssert` {Boolean} Default: false
218 * Return: {Number}
219
220 A generalized version of all numeric read methods. Supports up to 48 bits of
221 accuracy. For example:
222
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'
228
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`.
231
232 ### buf.toString([encoding][, start][, end])
233
234 * `encoding` String, Optional, Default: 'utf8'
235 * `start` Number, Optional, Default: 0
236 * `end` Number, Optional, Default: `buffer.length`
237
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`).
241
242 See `buffer.write()` example, above.
243
244
245 ### buf.toJSON()
246
247 Returns a JSON-representation of the Buffer instance.  `JSON.stringify`
248 implicitly calls this function when stringifying a Buffer instance.
249
250 Example:
251
252     var buf = new Buffer('test');
253     var json = JSON.stringify(buf);
254
255     console.log(json);
256     // '{"type":"Buffer","data":[116,101,115,116]}'
257
258     var copy = JSON.parse(json, function(key, value) {
259         return value && value.type === 'Buffer'
260           ? new Buffer(value.data)
261           : value;
262       });
263
264     console.log(copy);
265     // <Buffer 74 65 73 74>
266
267 ### buf[index]
268
269 <!--type=property-->
270 <!--name=[index]-->
271
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`.
274
275 Example: copy an ASCII string into a buffer, one byte at a time:
276
277     str = "node.js";
278     buf = new Buffer(str.length);
279
280     for (var i = 0; i < str.length ; i++) {
281       buf[i] = str.charCodeAt(i);
282     }
283
284     console.log(buf);
285
286     // node.js
287
288 ### buf.equals(otherBuffer)
289
290 * `otherBuffer` {Buffer}
291
292 Returns a boolean of whether `this` and `otherBuffer` have the same
293 bytes.
294
295 ### buf.compare(otherBuffer)
296
297 * `otherBuffer` {Buffer}
298
299 Returns a number indicating whether `this` comes before or after or is
300 the same as the `otherBuffer` in sort order.
301
302
303 ### buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])
304
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`
309
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`.
313
314 All values passed that are `undefined`/`NaN` or are out of bounds are set equal
315 to their respective defaults.
316
317 Example: build two Buffers, then copy `buf1` from byte 16 through byte 19
318 into `buf2`, starting at the 8th byte in `buf2`.
319
320     buf1 = new Buffer(26);
321     buf2 = new Buffer(26);
322
323     for (var i = 0 ; i < 26 ; i++) {
324       buf1[i] = i + 97; // 97 is ASCII a
325       buf2[i] = 33; // ASCII !
326     }
327
328     buf1.copy(buf2, 8, 16, 20);
329     console.log(buf2.toString('ascii', 0, 25));
330
331     // !!!!!!!!qrst!!!!!!!!!!!!!
332
333
334 ### buf.slice([start][, end])
335
336 * `start` Number, Optional, Default: 0
337 * `end` Number, Optional, Default: `buffer.length`
338
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.
342
343 **Modifying the new buffer slice will modify memory in the original buffer!**
344
345 Example: build a Buffer with the ASCII alphabet, take a slice, then modify one
346 byte from the original Buffer.
347
348     var buf1 = new Buffer(26);
349
350     for (var i = 0 ; i < 26 ; i++) {
351       buf1[i] = i + 97; // 97 is ASCII a
352     }
353
354     var buf2 = buf1.slice(0, 3);
355     console.log(buf2.toString('ascii', 0, buf2.length));
356     buf1[0] = 33;
357     console.log(buf2.toString('ascii', 0, buf2.length));
358
359     // abc
360     // !bc
361
362 ### buf.readUInt8(offset[, noAssert])
363
364 * `offset` Number
365 * `noAssert` Boolean, Optional, Default: false
366 * Return: Number
367
368 Reads an unsigned 8 bit integer from the buffer at the specified offset.
369
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`.
372
373 Example:
374
375     var buf = new Buffer(4);
376
377     buf[0] = 0x3;
378     buf[1] = 0x4;
379     buf[2] = 0x23;
380     buf[3] = 0x42;
381
382     for (ii = 0; ii < buf.length; ii++) {
383       console.log(buf.readUInt8(ii));
384     }
385
386     // 0x3
387     // 0x4
388     // 0x23
389     // 0x42
390
391 ### buf.readUInt16LE(offset[, noAssert])
392 ### buf.readUInt16BE(offset[, noAssert])
393
394 * `offset` Number
395 * `noAssert` Boolean, Optional, Default: false
396 * Return: Number
397
398 Reads an unsigned 16 bit integer from the buffer at the specified offset with
399 specified endian format.
400
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`.
403
404 Example:
405
406     var buf = new Buffer(4);
407
408     buf[0] = 0x3;
409     buf[1] = 0x4;
410     buf[2] = 0x23;
411     buf[3] = 0x42;
412
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));
419
420     // 0x0304
421     // 0x0403
422     // 0x0423
423     // 0x2304
424     // 0x2342
425     // 0x4223
426
427 ### buf.readUInt32LE(offset[, noAssert])
428 ### buf.readUInt32BE(offset[, noAssert])
429
430 * `offset` Number
431 * `noAssert` Boolean, Optional, Default: false
432 * Return: Number
433
434 Reads an unsigned 32 bit integer from the buffer at the specified offset with
435 specified endian format.
436
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`.
439
440 Example:
441
442     var buf = new Buffer(4);
443
444     buf[0] = 0x3;
445     buf[1] = 0x4;
446     buf[2] = 0x23;
447     buf[3] = 0x42;
448
449     console.log(buf.readUInt32BE(0));
450     console.log(buf.readUInt32LE(0));
451
452     // 0x03042342
453     // 0x42230403
454
455 ### buf.readInt8(offset[, noAssert])
456
457 * `offset` Number
458 * `noAssert` Boolean, Optional, Default: false
459 * Return: Number
460
461 Reads a signed 8 bit integer from the buffer at the specified offset.
462
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`.
465
466 Works as `buffer.readUInt8`, except buffer contents are treated as two's
467 complement signed values.
468
469 ### buf.readInt16LE(offset[, noAssert])
470 ### buf.readInt16BE(offset[, noAssert])
471
472 * `offset` Number
473 * `noAssert` Boolean, Optional, Default: false
474 * Return: Number
475
476 Reads a signed 16 bit integer from the buffer at the specified offset with
477 specified endian format.
478
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`.
481
482 Works as `buffer.readUInt16*`, except buffer contents are treated as two's
483 complement signed values.
484
485 ### buf.readInt32LE(offset[, noAssert])
486 ### buf.readInt32BE(offset[, noAssert])
487
488 * `offset` Number
489 * `noAssert` Boolean, Optional, Default: false
490 * Return: Number
491
492 Reads a signed 32 bit integer from the buffer at the specified offset with
493 specified endian format.
494
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`.
497
498 Works as `buffer.readUInt32*`, except buffer contents are treated as two's
499 complement signed values.
500
501 ### buf.readFloatLE(offset[, noAssert])
502 ### buf.readFloatBE(offset[, noAssert])
503
504 * `offset` Number
505 * `noAssert` Boolean, Optional, Default: false
506 * Return: Number
507
508 Reads a 32 bit float from the buffer at the specified offset with specified
509 endian format.
510
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`.
513
514 Example:
515
516     var buf = new Buffer(4);
517
518     buf[0] = 0x00;
519     buf[1] = 0x00;
520     buf[2] = 0x80;
521     buf[3] = 0x3f;
522
523     console.log(buf.readFloatLE(0));
524
525     // 0x01
526
527 ### buf.readDoubleLE(offset[, noAssert])
528 ### buf.readDoubleBE(offset[, noAssert])
529
530 * `offset` Number
531 * `noAssert` Boolean, Optional, Default: false
532 * Return: Number
533
534 Reads a 64 bit double from the buffer at the specified offset with specified
535 endian format.
536
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`.
539
540 Example:
541
542     var buf = new Buffer(8);
543
544     buf[0] = 0x55;
545     buf[1] = 0x55;
546     buf[2] = 0x55;
547     buf[3] = 0x55;
548     buf[4] = 0x55;
549     buf[5] = 0x55;
550     buf[6] = 0xd5;
551     buf[7] = 0x3f;
552
553     console.log(buf.readDoubleLE(0));
554
555     // 0.3333333333333333
556
557 ### buf.writeUInt8(value, offset[, noAssert])
558
559 * `value` Number
560 * `offset` Number
561 * `noAssert` Boolean, Optional, Default: false
562
563 Writes `value` to the buffer at the specified offset. Note, `value` must be a
564 valid unsigned 8 bit integer.
565
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`.
570
571 Example:
572
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);
578
579     console.log(buf);
580
581     // <Buffer 03 04 23 42>
582
583 ### buf.writeUInt16LE(value, offset[, noAssert])
584 ### buf.writeUInt16BE(value, offset[, noAssert])
585
586 * `value` Number
587 * `offset` Number
588 * `noAssert` Boolean, Optional, Default: false
589
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.
592
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`.
597
598 Example:
599
600     var buf = new Buffer(4);
601     buf.writeUInt16BE(0xdead, 0);
602     buf.writeUInt16BE(0xbeef, 2);
603
604     console.log(buf);
605
606     buf.writeUInt16LE(0xdead, 0);
607     buf.writeUInt16LE(0xbeef, 2);
608
609     console.log(buf);
610
611     // <Buffer de ad be ef>
612     // <Buffer ad de ef be>
613
614 ### buf.writeUInt32LE(value, offset[, noAssert])
615 ### buf.writeUInt32BE(value, offset[, noAssert])
616
617 * `value` Number
618 * `offset` Number
619 * `noAssert` Boolean, Optional, Default: false
620
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.
623
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`.
628
629 Example:
630
631     var buf = new Buffer(4);
632     buf.writeUInt32BE(0xfeedface, 0);
633
634     console.log(buf);
635
636     buf.writeUInt32LE(0xfeedface, 0);
637
638     console.log(buf);
639
640     // <Buffer fe ed fa ce>
641     // <Buffer ce fa ed fe>
642
643 ### buf.writeInt8(value, offset[, noAssert])
644
645 * `value` Number
646 * `offset` Number
647 * `noAssert` Boolean, Optional, Default: false
648
649 Writes `value` to the buffer at the specified offset. Note, `value` must be a
650 valid signed 8 bit integer.
651
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`.
656
657 Works as `buffer.writeUInt8`, except value is written out as a two's complement
658 signed integer into `buffer`.
659
660 ### buf.writeInt16LE(value, offset[, noAssert])
661 ### buf.writeInt16BE(value, offset[, noAssert])
662
663 * `value` Number
664 * `offset` Number
665 * `noAssert` Boolean, Optional, Default: false
666
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.
669
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`.
674
675 Works as `buffer.writeUInt16*`, except value is written out as a two's
676 complement signed integer into `buffer`.
677
678 ### buf.writeInt32LE(value, offset[, noAssert])
679 ### buf.writeInt32BE(value, offset[, noAssert])
680
681 * `value` Number
682 * `offset` Number
683 * `noAssert` Boolean, Optional, Default: false
684
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.
687
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`.
692
693 Works as `buffer.writeUInt32*`, except value is written out as a two's
694 complement signed integer into `buffer`.
695
696 ### buf.writeFloatLE(value, offset[, noAssert])
697 ### buf.writeFloatBE(value, offset[, noAssert])
698
699 * `value` Number
700 * `offset` Number
701 * `noAssert` Boolean, Optional, Default: false
702
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.
705
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`.
710
711 Example:
712
713     var buf = new Buffer(4);
714     buf.writeFloatBE(0xcafebabe, 0);
715
716     console.log(buf);
717
718     buf.writeFloatLE(0xcafebabe, 0);
719
720     console.log(buf);
721
722     // <Buffer 4f 4a fe bb>
723     // <Buffer bb fe 4a 4f>
724
725 ### buf.writeDoubleLE(value, offset[, noAssert])
726 ### buf.writeDoubleBE(value, offset[, noAssert])
727
728 * `value` Number
729 * `offset` Number
730 * `noAssert` Boolean, Optional, Default: false
731
732 Writes `value` to the buffer at the specified offset with specified endian
733 format. Note, `value` must be a valid 64 bit double.
734
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`.
739
740 Example:
741
742     var buf = new Buffer(8);
743     buf.writeDoubleBE(0xdeadbeefcafebabe, 0);
744
745     console.log(buf);
746
747     buf.writeDoubleLE(0xdeadbeefcafebabe, 0);
748
749     console.log(buf);
750
751     // <Buffer 43 eb d5 b7 dd f9 5f d7>
752     // <Buffer d7 5f f9 dd b7 d5 eb 43>
753
754 ### buf.fill(value[, offset][, end])
755
756 * `value`
757 * `offset` Number, Optional
758 * `end` Number, Optional
759
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
762 buffer.
763
764     var b = new Buffer(50);
765     b.fill("h");
766
767 ## buffer.INSPECT_MAX_BYTES
768
769 * Number, Default: 50
770
771 How many bytes will be returned when `buffer.inspect()` is called. This can
772 be overridden by user modules.
773
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.
776
777 ## Class: SlowBuffer
778
779 Returns an un-pooled `Buffer`.
780
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.
785
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.
789
790     // need to keep around a few small chunks of memory
791     var store = [];
792
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);
799       store.push(sb);
800     });
801
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.