2
2
3
3
Stability: 2 - Stable
4
4
5
- To use this module, do ` require('string_decoder') ` . StringDecoder decodes a
6
- buffer to a string. It is a simple interface to ` buffer.toString() ` but provides
7
- additional support for utf8.
5
+ The ` string_decoder ` module provides an API for decoding ` Buffer ` objects into
6
+ strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
7
+ characters. It can be accessed using:
8
+
9
+ ``` js
10
+ const StringDecoder = require (' string_decoder' ).StringDecoder ;
11
+ ```
12
+
13
+ The following example shows the basic use of the ` StringDecoder ` class.
8
14
9
15
``` js
10
16
const StringDecoder = require (' string_decoder' ).StringDecoder ;
@@ -17,23 +23,55 @@ const euro = Buffer.from([0xE2, 0x82, 0xAC]);
17
23
console .log (decoder .write (euro));
18
24
```
19
25
20
- ## Class: StringDecoder
26
+ When a ` Buffer ` instance is written to the ` StringDecoder ` instance, an
27
+ internal buffer is used to ensure that the decoded string does not contain
28
+ any incomplete multibyte characters. These are held in the buffer until the
29
+ next call to ` stringDecoder.write() ` or until ` stringDecoder.end() ` is called.
30
+
31
+ In the following example, the three UTF-8 encoded bytes of the European euro
32
+ symbol are written over three separate operations:
33
+
34
+ ``` js
35
+ const StringDecoder = require (' string_decoder' ).StringDecoder ;
36
+ const decoder = new StringDecoder (' utf8' );
37
+
38
+ decoder .write (Buffer .from ([0xE2 ]));
39
+ decoder .write (Buffer .from ([0x82 ]));
40
+ console .log (decoder .end (Buffer .from ([0xAC ])));
41
+ ```
42
+
43
+ ## Class: new StringDecoder([ encoding] )
21
44
<!-- YAML
22
45
added: v0.1.99
23
46
-->
24
47
25
- Accepts a single argument, ` encoding ` which defaults to ` 'utf8' ` .
48
+ * ` encoding ` {string} The character encoding the ` StringDecoder ` will use.
49
+ Defaults to ` 'utf8' ` .
26
50
27
- ### decoder.end()
51
+ Creates a new ` StringDecoder ` instance.
52
+
53
+ ### stringDecoder.end([ buffer] )
28
54
<!-- YAML
29
55
added: v0.9.3
30
56
-->
31
57
32
- Returns any trailing bytes that were left in the buffer.
58
+ * ` buffer ` {Buffer} A ` Buffer ` containing the bytes to decode.
59
+
60
+ Returns any remaining input stored in the internal buffer as a string. Bytes
61
+ representing incomplete UTF-8 and UTF-16 characters will be replaced with
62
+ substitution characters appropriate for the character encoding.
33
63
34
- ### decoder.write(buffer)
64
+ If the ` buffer ` argument is provided, one final call to ` stringDecoder.write() `
65
+ is performed before returning the remaining input.
66
+
67
+ ### stringDecoder.write(buffer)
35
68
<!-- YAML
36
69
added: v0.1.99
37
70
-->
38
71
39
- Returns a decoded string.
72
+ * ` buffer ` {Buffer} A ` Buffer ` containing the bytes to decode.
73
+
74
+ Returns a decoded string, ensuring that any incomplete multibyte characters at
75
+ the end of the ` Buffer ` are omitted from the returned string and stored in an
76
+ internal buffer for the next call to ` stringDecoder.write() ` or
77
+ ` stringDecoder.end() ` .
0 commit comments