module APISource Code:lib/string_decoder.js
Thestring_decoder module provides an API for decodingBuffer objects intostrings in a manner that preserves encoded multi-byte UTF-8 and UTF-16characters. It can be accessed using:
const {StringDecoder } =require('string_decoder');The following example shows the basic use of theStringDecoder class.
const {StringDecoder } =require('string_decoder');const decoder =newStringDecoder('utf8');const cent =Buffer.from([0xC2,0xA2]);console.log(decoder.write(cent));const euro =Buffer.from([0xE2,0x82,0xAC]);console.log(decoder.write(euro));When aBuffer instance is written to theStringDecoder instance, aninternal buffer is used to ensure that the decoded string does not containany incomplete multibyte characters. These are held in the buffer until thenext call tostringDecoder.write() or untilstringDecoder.end() is called.
In the following example, the three UTF-8 encoded bytes of the European Eurosymbol (€) are written over three separate operations:
const {StringDecoder } =require('string_decoder');const decoder =newStringDecoder('utf8');decoder.write(Buffer.from([0xE2]));decoder.write(Buffer.from([0x82]));console.log(decoder.end(Buffer.from([0xAC])));StringDecoder#new StringDecoder([encoding])#Creates a newStringDecoder instance.
stringDecoder.end([buffer])#buffer<Buffer> |<TypedArray> |<DataView> ABuffer, orTypedArray, orDataView containing the bytes to decode.Returns any remaining input stored in the internal buffer as a string. Bytesrepresenting incomplete UTF-8 and UTF-16 characters will be replaced withsubstitution characters appropriate for the character encoding.
If thebuffer argument is provided, one final call tostringDecoder.write()is performed before returning the remaining input.Afterend() is called, thestringDecoder object can be reused for new input.
stringDecoder.write(buffer)#| Version | Changes |
|---|---|
| v8.0.0 | Each invalid character is now replaced by a single replacement character instead of one for each individual byte. |
| v0.1.99 | Added in: v0.1.99 |
buffer<Buffer> |<TypedArray> |<DataView> ABuffer, orTypedArray, orDataView containing the bytes to decode.Returns a decoded string, ensuring that any incomplete multibyte characters atthe end of theBuffer, orTypedArray, orDataView are omitted from thereturned string and stored in an internal buffer for the next call tostringDecoder.write() orstringDecoder.end().