The stringDecoder.end() method is used to return any remaining data stored in the decoder's internal buffer as a string. It ensures that incomplete multibyte characters are handled properly before the decoding process is completed.
- Returns the remaining buffered data.
- Handles incomplete UTF-8 and UTF-16 characters safely.
- Can process an optional final buffer before ending.
- Commonly used when decoding streams is complete.
Syntax
stringDecoder.end([buffer])where,
- buffer: It is a Buffer, TypedArray, or DataView that contains the bytes that have to be decoded. It is an optional parameter.
Return Value: It returns the remaining input stored in a buffer as a string.
Example 1:
// Import the string_decoder module
// and get the StringDecoder class
// using object destructuring
const { StringDecoder } = require("string_decoder");
const decoder = new StringDecoder("utf-8");
// Using the end() method
const text_one = Buffer.from("GeeksforGeeks", "utf-8");
let decoded_text = decoder.end(text_one);
console.log("Decoded Text:", decoded_text);
// The Euro Symbol is denoted using
// the bytes [0xE2, 0x82, 0xAC]
console.log("Decoding the Euro Symbol:");
// Decoding the euro symbol
// Using the write() method to
// write the first two parts
console.log(decoder.write(Buffer.from([0xE2])));
console.log(decoder.write(Buffer.from([0x82])));
// Finishing the symbol using the end() method
// with that gives an additional call to write()
// using the last part of the buffer
console.log(decoder.end(Buffer.from([0xAC])));
Output:
Decoded Text: GeeksforGeeks
Decoding the Euro Symbol:
€
Example 2:
// Import the string_decoder module
// and get the StringDecoder class
// using object destructuring
const { StringDecoder } = require("string_decoder");
const decoder = new StringDecoder("utf-8");
// The Cent Symbol is denoted using
// Buffer.from([0xc2, 0xa2])
// Decoding the complete cent symbol from buffer
let cent_symbol = Buffer.from([0xc2, 0xa2]);
let cent_symbol_out = decoder.end(cent_symbol);
console.log("Complete Cent Symbol:", cent_symbol_out);
// Decoding incomplete cent symbol using
// Buffer.write() method
cent_symbol = Buffer.from([0xc2]);
cent_symbol_out = decoder.write(cent_symbol);
console.log("Cent Symbol using write():",
cent_symbol_out);
// Decoding incomplete cent symbol
// using Buffer.end() method
cent_symbol = Buffer.from([0xc2]);
cent_symbol_out = decoder.end(cent_symbol);
console.log("Cent Symbol using end():",
cent_symbol_out);
Output:
Complete Cent Symbol: ¢
Cent Symbol using write():
Cent Symbol using end(): ??
Use cases of stringDecoder.end()
- Flushing any remaining data from the decoder when a stream ends.
- Handling incomplete multibyte characters at the end of encoded data.
- Finalizing the decoding process after all chunks have been processed.
- Preventing data loss when reading data in multiple buffer chunks.
- Completing text extraction from network, file, or stream-based data sources.
Reference: https://nodejs.org/api/string_decoder.html#string_decoder_stringdecoder_end_buffer