Node.js stringDecoder.end() Method - GeeksforGeeks

08 Oct, 2021
The stringDecoder.end() method is used to return all the remaining input stored in the internal buffer as a string. This method ensures that any incomplete UTF-8 and UTF-16 characters are replaced with substitution characters that are appropriate for character encoding.
When the optional buffer argument is provided, the stringDecoder.write() method is called once with the data before returning the remaining buffer input.
Syntax: 
stringDecoder.end( [buffer] )
Parameters: This function accepts single parameter as mentioned above and described below: 
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.Below programs illustrate the stringDecoder.end() method in Node.js:
Example 1: 
Javascript
// Import the string_decoder module// and get the StringDecoder class // using object destructuringconst { StringDecoder } = require("string_decoder");  const decoder = new StringDecoder("utf-8");  // Using the end() methodconst 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 partsconsole.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 bufferconsole.log(decoder.end(Buffer.from([0xAC])));
Output: 
Decoded Text: GeeksforGeeks
Decoding the Euro Symbol:


€
Example 2: 
Javascript
// Import the string_decoder module// and get the StringDecoder class // using object destructuringconst { 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 bufferlet 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() methodcent_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() methodcent_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(): ??
Reference: https://nodejs.org/api/string_decoder.html#string_decoder_stringdecoder_end_buffer
arorakashish0911
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript