|
| 1 | +## Buffers |
| 2 | + |
| 3 | +Pure Javascript is Unicode friendly but not nice to binary data. When |
| 4 | +dealing with TCP streams or the file system, it's necessary to handle octet |
| 5 | +streams. Node has several strategies for manipulating, creating, and |
| 6 | +consuming octet streams. |
| 7 | + |
| 8 | +Raw data is stored in instances of the `Buffer` class. A `Buffer` is similar |
| 9 | +to an array of integers but corresponds to a raw memory allocation outside |
| 10 | +the V8 heap. A `Buffer` cannot be resized. |
| 11 | + |
| 12 | +The `Buffer` object is global. |
| 13 | + |
| 14 | +Converting between Buffers and JavaScript string objects requires an explicit encoding |
| 15 | +method. Here are the different string encodings; |
| 16 | + |
| 17 | +* `'ascii'` - for 7 bit ASCII data only. This encoding method is very fast, and will |
| 18 | +strip the high bit if set. |
| 19 | + |
| 20 | +* `'utf8'` - Unicode characters. Many web pages and other document formats use UTF-8. |
| 21 | + |
| 22 | +* `'base64'` - Base64 string encoding. |
| 23 | + |
| 24 | +* `'binary'` - A way of encoding raw binary data into strings by using only |
| 25 | +the first 8 bits of each character. This encoding method is depreciated and |
| 26 | +should be avoided in favor of `Buffer` objects where possible. This encoding |
| 27 | +will be removed in future versions of Node. |
| 28 | + |
| 29 | + |
| 30 | +### new Buffer(size) |
| 31 | + |
| 32 | +Allocates a new buffer of `size` octets. |
| 33 | + |
| 34 | +### new Buffer(array) |
| 35 | + |
| 36 | +Allocates a new buffer using an `array` of octets. |
| 37 | + |
| 38 | +### new Buffer(str, encoding='utf8') |
| 39 | + |
| 40 | +Allocates a new buffer containing the given `str`. |
| 41 | + |
| 42 | +### buffer.write(string, offset=0, encoding='utf8') |
| 43 | + |
| 44 | +Writes `string` to the buffer at `offset` using the given encoding. Returns |
| 45 | +number of octets written. If `buffer` did not contain enough space to fit |
| 46 | +the entire string it will write a partial amount of the string. In the case |
| 47 | +of `'utf8'` encoding, the method will not write partial characters. |
| 48 | + |
| 49 | +Example: write a utf8 string into a buffer, then print it |
| 50 | + |
| 51 | + buf = new Buffer(256); |
| 52 | + len = buf.write('\u00bd + \u00bc = \u00be', 0); |
| 53 | + console.log(len + " bytes: " + buf.toString('utf8', 0, len)); |
| 54 | + |
| 55 | + // 12 bytes: ½ + ¼ = ¾ |
| 56 | + |
| 57 | + |
| 58 | +### buffer.toString(encoding, start=0, end=buffer.length) |
| 59 | + |
| 60 | +Decodes and returns a string from buffer data encoded with `encoding` |
| 61 | +beginning at `start` and ending at `end`. |
| 62 | + |
| 63 | +See `buffer.write()` example, above. |
| 64 | + |
| 65 | + |
| 66 | +### buffer[index] |
| 67 | + |
| 68 | +Get and set the octet at `index`. The values refer to individual bytes, |
| 69 | +so the legal range is between `0x00` and `0xFF` hex or `0` and `255`. |
| 70 | + |
| 71 | +Example: copy an ASCII string into a buffer, one byte at a time: |
| 72 | + |
| 73 | + str = "node.js"; |
| 74 | + buf = new Buffer(str.length); |
| 75 | + |
| 76 | + for (var i = 0; i < str.length ; i++) { |
| 77 | + buf[i] = str.charCodeAt(i); |
| 78 | + } |
| 79 | + |
| 80 | + console.log(buf); |
| 81 | + |
| 82 | + // node.js |
| 83 | + |
| 84 | + |
| 85 | +### Buffer.byteLength(string, encoding='utf8') |
| 86 | + |
| 87 | +Gives the actual byte length of a string. This is not the same as |
| 88 | +`String.prototype.length` since that returns the number of *characters* in a |
| 89 | +string. |
| 90 | + |
| 91 | +Example: |
| 92 | + |
| 93 | + str = '\u00bd + \u00bc = \u00be'; |
| 94 | + |
| 95 | + console.log(str + ": " + str.length + " characters, " + |
| 96 | + Buffer.byteLength(str, 'utf8') + " bytes"); |
| 97 | + |
| 98 | + // ½ + ¼ = ¾: 9 characters, 12 bytes |
| 99 | + |
| 100 | + |
| 101 | +### buffer.length |
| 102 | + |
| 103 | +The size of the buffer in bytes. Note that this is not necessarily the size |
| 104 | +of the contents. `length` refers to the amount of memory allocated for the |
| 105 | +buffer object. It does not change when the contents of the buffer are changed. |
| 106 | + |
| 107 | + buf = new Buffer(1234); |
| 108 | + |
| 109 | + console.log(buf.length); |
| 110 | + buf.write("some string", "ascii", 0); |
| 111 | + console.log(buf.length); |
| 112 | + |
| 113 | + // 1234 |
| 114 | + // 1234 |
| 115 | + |
| 116 | +### buffer.copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length) |
| 117 | + |
| 118 | +Does a memcpy() between buffers. |
| 119 | + |
| 120 | +Example: build two Buffers, then copy `buf1` from byte 16 through byte 19 |
| 121 | +into `buf2`, starting at the 8th byte in `buf2`. |
| 122 | + |
| 123 | + buf1 = new Buffer(26); |
| 124 | + buf2 = new Buffer(26); |
| 125 | + |
| 126 | + for (var i = 0 ; i < 26 ; i++) { |
| 127 | + buf1[i] = i + 97; // 97 is ASCII a |
| 128 | + buf2[i] = 33; // ASCII ! |
| 129 | + } |
| 130 | + |
| 131 | + buf1.copy(buf2, 8, 16, 20); |
| 132 | + console.log(buf2.toString('ascii', 0, 25)); |
| 133 | + |
| 134 | + // !!!!!!!!qrst!!!!!!!!!!!!! |
| 135 | + |
| 136 | + |
| 137 | +### buffer.slice(start, end=buffer.length) |
| 138 | + |
| 139 | +Returns a new buffer which references the |
| 140 | +same memory as the old, but offset and cropped by the `start` and `end` |
| 141 | +indexes. |
| 142 | + |
| 143 | +**Modifying the new buffer slice will modify memory in the original buffer!** |
| 144 | + |
| 145 | +Example: build a Buffer with the ASCII alphabet, take a slice, then modify one byte |
| 146 | +from the original Buffer. |
| 147 | + |
| 148 | + var buf1 = new Buffer(26); |
| 149 | + |
| 150 | + for (var i = 0 ; i < 26 ; i++) { |
| 151 | + buf1[i] = i + 97; // 97 is ASCII a |
| 152 | + } |
| 153 | + |
| 154 | + var buf2 = buf1.slice(0, 3); |
| 155 | + console.log(buf2.toString('ascii', 0, buf2.length)); |
| 156 | + buf1[0] = 33; |
| 157 | + console.log(buf2.toString('ascii', 0, buf2.length)); |
| 158 | + |
| 159 | + // abc |
| 160 | + // !bc |
0 commit comments