Encoding
TextEncoder
The TextEncoder API takes code point streams as inputs and generates
UTF-8
byte streams as outputs. For more information, see TextEncoder. Constructor API
// The TextEncoder() constructor does not have any parameters.const encoder = new TextEncoder();
Attributes
// encoder.encodingreadonly encoding: string;
The name of the encoding algorithm that is used by the encoder. The value is fixed to
UTF-8
.Methods
encode
encoder.encode(input?: string | undefined): Uint8Array
The encoder.encode() method takes code point streams as inputs and generates
UTF-8
byte streams as outputs.Note:
The maximum length of input is 300 MB. An exception will be thrown if the maximum length is exceeded.
encoder.encode
parametersParameter | Type | Required | Description |
input | string | undefined | No | The text to be encoded. |
encodeInto
encoder.encodeInto(input: string, destination: Uint8Array): EncodeIntoResult;
The encoder.encodeInto() method takes code point streams as inputs, generates
UTF-8
byte streams as outputs, and writes the outputs to a destination
byte array.Parameters
Parameter | Type | Required | Description |
input | string | Yes | The text to be encoded. |
destination | Yes | The object in which the encoded text is stored. |
Return value:
EncodeIntoResult
Parameter | Type | Description |
read | number | The number of UTF-16 units that have been converted to UTF-8. |
written | number |
TextDecoder
The TextDecoder API takes byte streams as inputs and generates code point streams as outputs. For more information, see TextDecoder.
Construction API
const decoder = new TextDecoder(label?: string | undefined, options?: DecoderOptions | undefined): TextEncoder;
Parameters
Note:
iso-8859-16
hz-gb-2312
csiso2022kr, iso-2022-kr
Parameter | Type | Required | Description |
label | string | undefined | No | The name of the decoding algorithm that is used by the decoder. Default value: UTF-8. For the valid values of label, see Encoding API Encodings. |
options | No | The configuration items of the decoder. |
DecoderOptions
The following table describes the configuration items of the decoder.
Parameter | Type | Default value | Description |
fatal | boolean | false | Specifies whether to throw an exception when decoding fails. |
ignoreBOM | boolean | false | Specifies whether to ignore byte-order marker. |
Attributes
encoding
// decoder.encodingreadonly encoding: string;
The name of the decoding algorithm that is used by the decoder.
fatal
// decoder.fatalreadonly fatal: boolean;
Specifies whether to throw an exception when decoding fails.
ignoreBOM
// decoder.ignoreBOMreadonly ignoreBOM: boolean;
Methods
decode
const result = decoder.decode(buffer?: ArrayBuffer | ArrayBufferView | undefined, options?: DecodeOptions | undefined): string;
Note:
The maximum length of buffer is 100 MB. An exception will be thrown if the maximum length is exceeded.
Parameter | Type | Required | Description |
buffer | No | The byte stream to be decoded. The maximum length of buffer is 100 MB. An exception will be thrown if the maximum length is exceeded. | |
options | No | The configuration items for decoding. |
DecodeOptions
The following table describes the configuration items for decoding.
Parameter | Type | Default value | Description |
stream | boolean | false | Specifies whether to perform decoding in streaming mode. Default value: false. Valid values: true Perform decoding in streaming mode. Use this value if data is processed in chunks and additional chunks are expected. false Do not perform decoding in streaming mode. Use this value if the current chunk is the last one or data is not chunked. |
Sample Code
function handleEvent(event) {// Encoderconst encoder = new TextEncoder();const encodeText = encoder.encode('hello world');// Decoderconst decoder = new TextDecoder();const decodeText = decoder.decode(encodeText);// Responseconst response = new Response(JSON.stringify({encodeText: encodeText.toString(),decodeText,}));return response;}addEventListener('fetch', (event) => {event.respondWith(handleEvent(event));});