String decoder | Node.js v19.9.0 Documentation
https://nodejs.org/docs/latest-v19.x/api/string_decoder.html • 24 KB fetched Open original page
String decoder | Node.js v19.9.0 Documentation
Node.js
* About this documentation
* Usage and example
* Assertion testing
* Asynchronous context tracking
* Async hooks
* Buffer
* C++ addons
* C/C++ addons with Node-API
* C++ embedder API
* Child processes
* Cluster
* Command-line options
* Console
* Corepack
* Crypto
* Debugger
* Deprecated APIs
* Diagnostics Channel
* DNS
* Domain
* Errors
* Events
* File system
* Globals
* HTTP
* HTTP/2
* HTTPS
* Inspector
* Internationalization
* Modules: CommonJS modules
* Modules: ECMAScript modules
* Modules: node:module API
* Modules: Packages
* Net
* OS
* Path
* Performance hooks
* Permissions
* Process
* Punycode
* Query strings
* Readline
* REPL
* Report
* Single executable applications
* Stream
* String decoder
* Test runner
* Timers
* TLS/SSL
* Trace events
* TTY
* UDP/datagram
* URL
* Utilities
* V8
* VM
* WASI
* Web Crypto API
* Web Streams API
* Worker threads
* Zlib
* Code repository and issue tracker
Node.js v19.9.0 documentation
* Node.js v19.9.0
*
► ▼
Table of contents
* String decoder
* Class: StringDecoder
* new StringDecoder([encoding])
* stringDecoder.end([buffer])
* stringDecoder.write(buffer)
*
► ▼
Index
* About this documentation
* Usage and example
*
Index
* Assertion testing
* Asynchronous context tracking
* Async hooks
* Buffer
* C++ addons
* C/C++ addons with Node-API
* C++ embedder API
* Child processes
* Cluster
* Command-line options
* Console
* Corepack
* Crypto
* Debugger
* Deprecated APIs
* Diagnostics Channel
* DNS
* Domain
* Errors
* Events
* File system
* Globals
* HTTP
* HTTP/2
* HTTPS
* Inspector
* Internationalization
* Modules: CommonJS modules
* Modules: ECMAScript modules
* Modules: node:module API
* Modules: Packages
* Net
* OS
* Path
* Performance hooks
* Permissions
* Process
* Punycode
* Query strings
* Readline
* REPL
* Report
* Single executable applications
* Stream
* String decoder
* Test runner
* Timers
* TLS/SSL
* Trace events
* TTY
* UDP/datagram
* URL
* Utilities
* V8
* VM
* WASI
* Web Crypto API
* Web Streams API
* Worker threads
* Zlib
* Code repository and issue tracker
*
► ▼
Other versions
* 19.x
* 18.x LTS
* 17.x
* 16.x LTS
* 15.x
* 14.x LTS
* 13.x
* 12.x
* 11.x
* 10.x
* 9.x
* 8.x
* 7.x
* 6.x
* 5.x
* 4.x
* 0.12.x
* 0.10.x
*
► ▼
Options
*
View on single page
*
View as JSON
* Edit on GitHub
Table of contents
* String decoder
* Class: StringDecoder
* new StringDecoder([encoding])
* stringDecoder.end([buffer])
* stringDecoder.write(buffer)
String decoder #
Stability: 2 - Stable
Source Code: lib/string_decoder.js
The node:string_decoder module provides an API for decoding Buffer objects
into strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
characters. It can be accessed using:
const { StringDecoder } = require ( 'node:string_decoder' ); copy
The following example shows the basic use of the StringDecoder class.
const { StringDecoder } = require ( 'node:string_decoder' );
const decoder = new StringDecoder ( 'utf8' );
const cent = Buffer . from ([ 0xC2 , 0xA2 ]);
console . log (decoder. write (cent));
const euro = Buffer . from ([ 0xE2 , 0x82 , 0xAC ]);
console . log (decoder. write (euro)); copy
When a Buffer instance is written to the StringDecoder instance, an
internal buffer is used to ensure that the decoded string does not contain
any incomplete multibyte characters. These are held in the buffer until the
next call to stringDecoder.write() or until stringDecoder.end() is called.
In the following example, the three UTF-8 encoded bytes of the European Euro
symbol ( € ) are written over three separate operations:
const { StringDecoder } = require ( 'node:string_decoder' );
const decoder = new StringDecoder ( 'utf8' );
decoder. write ( Buffer . from ([ 0xE2 ]));
decoder. write ( Buffer . from ([ 0x82 ]));
console . log (decoder. end ( Buffer . from ([ 0xAC ]))); copy
Class: StringDecoder #
new StringDecoder([encoding]) #
Added in: v0.1.99
* encoding <string> The character encoding the StringDecoder will use.
Default: 'utf8' .
Creates a new StringDecoder instance.
stringDecoder.end([buffer]) #
Added in: v0.9.3
* buffer <Buffer> | <TypedArray> | <DataView> A Buffer , or TypedArray , or
DataView containing the bytes to decode.
* Returns: <string>
Returns any remaining input stored in the internal buffer as a string. Bytes
representing incomplete UTF-8 and UTF-16 characters will be replaced with
substitution characters appropriate for the character encoding.
If the buffer argument is provided, one final call to stringDecoder.write()
is performed before returning the remaining input.
After end() is called, the stringDecoder object can be reused for new input.
stringDecoder.write(buffer) #
History
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> A Buffer , or TypedArray , or
DataView containing the bytes to decode.
* Returns: <string>
Returns a decoded string, ensuring that any incomplete multibyte characters at
the end of the Buffer , or TypedArray , or DataView are omitted from the
returned string and stored in an internal buffer for the next call to
stringDecoder.write() or stringDecoder.end() .
Links found on this page
- Node.js [direct]
- About this documentation [direct]
- Usage and example [direct]
- Assertion testing [direct]
- Asynchronous context tracking [direct]
- Async hooks [direct]
- Buffer [direct]
- C++ addons [direct]
- C/C++ addons with Node-API [direct]
- C++ embedder API [direct]
- Child processes [direct]
- Cluster [direct]
- Command-line options [direct]
- Console [direct]
- Corepack [direct]
- Crypto [direct]
- Debugger [direct]
- Deprecated APIs [direct]
- Diagnostics Channel [direct]
- DNS [direct]
- Domain [direct]
- Errors [direct]
- Events [direct]
- File system [direct]
- Globals [direct]
- HTTP [direct]
- HTTP/2 [direct]
- HTTPS [direct]
- Inspector [direct]
- Internationalization [direct]
- Modules: CommonJS modules [direct]
- Modules: ECMAScript modules [direct]
- Modules: node:module API [direct]
- Modules: Packages [direct]
- Net [direct]
- OS [direct]
- Path [direct]
- Performance hooks [direct]
- Permissions [direct]
- Process [direct]
- Punycode [direct]
- Query strings [direct]
- Readline [direct]
- REPL [direct]
- Report [direct]
- Single executable applications [direct]
- Stream [direct]
- String decoder [direct]
- Test runner [direct]
- Timers [direct]
- TLS/SSL [direct]
- Trace events [direct]
- TTY [direct]
- UDP/datagram [direct]
- URL [direct]
- Utilities [direct]
- V8 [direct]
- VM [direct]
- WASI [direct]
- Web Crypto API [direct]
- Web Streams API [direct]
- Worker threads [direct]
- Zlib [direct]
- Code repository and issue tracker [direct]
- Index [direct]
- 18.x LTS [direct]
- 17.x [direct]
- 16.x LTS [direct]
- 15.x [direct]
- 14.x LTS [direct]
- 13.x [direct]
- 12.x [direct]
- 11.x [direct]
- 10.x [direct]
- 9.x [direct]
- 8.x [direct]
- 7.x [direct]
- 6.x [direct]
- 5.x [direct]
- 4.x [direct]
|
|