11cb0ef41Sopenharmony_ci# String decoder
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/string_decoder.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciThe `node:string_decoder` module provides an API for decoding `Buffer` objects
101cb0ef41Sopenharmony_ciinto strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
111cb0ef41Sopenharmony_cicharacters. It can be accessed using:
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci```js
141cb0ef41Sopenharmony_ciconst { StringDecoder } = require('node:string_decoder');
151cb0ef41Sopenharmony_ci```
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciThe following example shows the basic use of the `StringDecoder` class.
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci```js
201cb0ef41Sopenharmony_ciconst { StringDecoder } = require('node:string_decoder');
211cb0ef41Sopenharmony_ciconst decoder = new StringDecoder('utf8');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst cent = Buffer.from([0xC2, 0xA2]);
241cb0ef41Sopenharmony_ciconsole.log(decoder.write(cent)); // Prints: ¢
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst euro = Buffer.from([0xE2, 0x82, 0xAC]);
271cb0ef41Sopenharmony_ciconsole.log(decoder.write(euro)); // Prints: €
281cb0ef41Sopenharmony_ci```
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciWhen a `Buffer` instance is written to the `StringDecoder` instance, an
311cb0ef41Sopenharmony_ciinternal buffer is used to ensure that the decoded string does not contain
321cb0ef41Sopenharmony_ciany incomplete multibyte characters. These are held in the buffer until the
331cb0ef41Sopenharmony_cinext call to `stringDecoder.write()` or until `stringDecoder.end()` is called.
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciIn the following example, the three UTF-8 encoded bytes of the European Euro
361cb0ef41Sopenharmony_cisymbol (`€`) are written over three separate operations:
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci```js
391cb0ef41Sopenharmony_ciconst { StringDecoder } = require('node:string_decoder');
401cb0ef41Sopenharmony_ciconst decoder = new StringDecoder('utf8');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cidecoder.write(Buffer.from([0xE2]));
431cb0ef41Sopenharmony_cidecoder.write(Buffer.from([0x82]));
441cb0ef41Sopenharmony_ciconsole.log(decoder.end(Buffer.from([0xAC]))); // Prints: €
451cb0ef41Sopenharmony_ci```
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci## Class: `StringDecoder`
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci### `new StringDecoder([encoding])`
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci<!-- YAML
521cb0ef41Sopenharmony_ciadded: v0.1.99
531cb0ef41Sopenharmony_ci-->
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci* `encoding` {string} The character [encoding][] the `StringDecoder` will use.
561cb0ef41Sopenharmony_ci  **Default:** `'utf8'`.
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciCreates a new `StringDecoder` instance.
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci### `stringDecoder.end([buffer])`
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci<!-- YAML
631cb0ef41Sopenharmony_ciadded: v0.9.3
641cb0ef41Sopenharmony_ci-->
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci* `buffer` {string|Buffer|TypedArray|DataView} The bytes to decode.
671cb0ef41Sopenharmony_ci* Returns: {string}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ciReturns any remaining input stored in the internal buffer as a string. Bytes
701cb0ef41Sopenharmony_cirepresenting incomplete UTF-8 and UTF-16 characters will be replaced with
711cb0ef41Sopenharmony_cisubstitution characters appropriate for the character encoding.
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ciIf the `buffer` argument is provided, one final call to `stringDecoder.write()`
741cb0ef41Sopenharmony_ciis performed before returning the remaining input.
751cb0ef41Sopenharmony_ciAfter `end()` is called, the `stringDecoder` object can be reused for new input.
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci### `stringDecoder.write(buffer)`
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci<!-- YAML
801cb0ef41Sopenharmony_ciadded: v0.1.99
811cb0ef41Sopenharmony_cichanges:
821cb0ef41Sopenharmony_ci  - version: v8.0.0
831cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/9618
841cb0ef41Sopenharmony_ci    description: Each invalid character is now replaced by a single replacement
851cb0ef41Sopenharmony_ci                 character instead of one for each individual byte.
861cb0ef41Sopenharmony_ci-->
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci* `buffer` {string|Buffer|TypedArray|DataView} The bytes to decode.
891cb0ef41Sopenharmony_ci* Returns: {string}
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ciReturns a decoded string, ensuring that any incomplete multibyte characters at
921cb0ef41Sopenharmony_cithe end of the `Buffer`, or `TypedArray`, or `DataView` are omitted from the
931cb0ef41Sopenharmony_cireturned string and stored in an internal buffer for the next call to
941cb0ef41Sopenharmony_ci`stringDecoder.write()` or `stringDecoder.end()`.
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci[encoding]: buffer.md#buffers-and-character-encodings
97