Node.js – stringDecoder.end() Method


The stringDecoder.end() method will return any remaining input that is left in the internal buffer as a string. Bytes that are left incomplete and represent UTF-8 and UTF-16 characters are replaced with the substitution characters appropriate for character encoding.

StringDecoder.write() method is called before returning the remaining input if any buffer argument is provided. The stringDecoder can be reused for taking new inputs once the end() method is called.

Syntax

stringDecoder.end( [buffer] )

Parameters

  • buffer - This parameter takes input for the bytes to decode. It can take buffer, TypedArray or DataView as the input parameters.

Example 1

Create a file with the name "end.js" and copy the following code. After creating the file, use the command "node end.js" to run this code as shown in the example below

// stringDecoder.end() method Demo Example

// Importing the string_decoder module
const { StringDecoder } = require("string_decoder");

// Defining the decoder type
const decoder = new StringDecoder("utf-8");

// Converting text to buffer
const text = Buffer.from("TutorialsPoint", "utf-8");

// Getting text from the buffer using end() method
let decoded_text = decoder.end(text);

// Printing the decoded text
console.log("Decoded Text:", decoded_text);

Output

Decoded Text: TutorialsPoint

Example 2

// stringDecoder.end() method Demo Example

// Importing the string_decoder module
const { StringDecoder } = require("string_decoder");

// Defining the decoder type
const decoder = new StringDecoder("utf-8");

// Euro Symbol: [0xE2, 0x82, 0xAC]
console.log("Decoding the Euro Symbol:");

// Writing the euro values in decoder
decoder.write(Buffer.from([0xE2]));
decoder.write(Buffer.from([0x82]));

// Printing the symbol using end() method
console.log(decoder.end(Buffer.from([0xAC])));

Output

Updated on: 24-Nov-2021

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements