22
33 Stability: 2 - Stable
44
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.
814
915``` js
1016const StringDecoder = require (' string_decoder' ).StringDecoder ;
@@ -17,23 +23,55 @@ const euro = Buffer.from([0xE2, 0x82, 0xAC]);
1723console .log (decoder .write (euro));
1824```
1925
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] )
2144<!-- YAML
2245added: v0.1.99
2346-->
2447
25- Accepts a single argument, ` encoding ` which defaults to ` 'utf8' ` .
48+ * ` encoding ` {string} The character encoding the ` StringDecoder ` will use.
49+ Defaults to ` 'utf8' ` .
2650
27- ### decoder.end()
51+ Creates a new ` StringDecoder ` instance.
52+
53+ ### stringDecoder.end([ buffer] )
2854<!-- YAML
2955added: v0.9.3
3056-->
3157
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.
3363
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)
3568<!-- YAML
3669added: v0.1.99
3770-->
3871
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