Skip to content

Commit

Permalink
Fix: Formation and descriptions of 'how-to-use-buffers.md' (#2166)
Browse files Browse the repository at this point in the history
1) To differ the versions between v6.0.0 and later, because there're some methods deprecated for 'Buffer' (See:#2165).

2) Add some normal js formations to make snippets of codes shown better.
  • Loading branch information
Maledong committed Apr 5, 2019
1 parent caf442e commit 7a86346
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions locale/en/knowledge/advanced/buffers/how-to-use-buffers.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Don't use binary strings. Use *buffers* instead!

## What Are Buffers?

Buffers are instances of the `Buffer` class in node, which is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data. In addition, the "integers" in a buffer each represent a byte and so are limited to values from 0 to 255 (2^8 - 1), inclusive.
The `Buffer` class in Node.js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data. The integers in a buffer each represent a byte and so are limited to values from 0 to 255 inclusive. When using `console.log()` to print the `Buffer` instance, you'll get a chain of values in hexadecimal values.

## Where You See Buffers:

Expand All @@ -33,22 +33,28 @@ In the wild, buffers are usually seen in the context of binary data coming from

There are a few ways to create new buffers:

var buffer = new Buffer(8);
```js
var buffer = Buffer.alloc(8);
// This will print out 8 bytes of zero:
// <Buffer 00 00 00 00 00 00 00 00>
```
This buffer is initialized and contains 8 bytes of zero.

This buffer is uninitialized and contains 8 bytes.

var buffer = new Buffer([ 8, 6, 7, 5, 3, 0, 9]);
```js
var buffer = Buffer.from([ 8, 6, 7, 5, 3, 0, 9]);
// This will print out 8 bytes of certain values:
// <Buffer 08 06 07 05 03 00 09>
```

This initializes the buffer to the contents of this array. Keep in mind that the contents of the array are integers representing bytes.

var buffer = new Buffer("I'm a string!", "utf-8")

This initializes the buffer to a binary encoding of the first string as specified by the second argument (in this case, utf-8). **utf-8** is by far the most common encoding used with node, but `Buffer` also supports:
```js
var buffer = Buffer.from("I'm a string!", "utf-8");
// This will print out a chain of values in utf-8:
// <Buffer 49 27 6d 20 61 20 73 74 72 69 6e 67 21>
```

* **"ascii"**: This encoding is way fast, but is limited to the ascii character set. Moreover, it will convert null characters into spaces, unlike the utf-8 encoding.
* **"ucs2"**: A two-byte, little-endian encoding. Can encode a subset of unicode.
* **"base64"**: Base64 string encoding.
* **"binary"**: This is the "binary string" format mentioned earlier, and is in the process of being deprecated. Avoid its use.
This initializes the buffer to a binary encoding of the first string as specified by the second argument (in this case, `'utf-8'`). `'utf-8'` is by far the most common encoding used with Node.js, but `Buffer` also supports others. See [Supported Encodings](https://nodejs.org/dist/latest/docs/api/buffer.html#buffer_buffers_and_character_encodings) for more details.

### Writing to Buffers

Expand Down

0 comments on commit 7a86346

Please sign in to comment.