Skip to content
Related Articles

Related Articles

Node.js | stringDecoder.end() Method
  • Last Updated : 31 May, 2020

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:




// 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 additonal 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(): ??

Reference: https://nodejs.org/api/string_decoder.html#string_decoder_stringdecoder_end_buffer

My Personal Notes arrow_drop_up
Recommended Articles
Page :