#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
2 min read
Question 19 of 62easy

What is the String Decoder module?

Decoding Buffer objects to strings properly.

What You'll Learn

  • Why String Decoder is needed
  • Handling multi-byte characters
  • Usage with streams

The Problem

code.jsJavaScript
// Multi-byte characters can be split across chunks
const euroSign = Buffer.from([0xE2, 0x82, 0xAC]); // € in UTF-8

// If split into chunks:
const chunk1 = Buffer.from([0xE2, 0x82]);
const chunk2 = Buffer.from([0xAC]);

// ❌ Wrong - produces garbage
console.log(chunk1.toString() + chunk2.toString());

Solution: StringDecoder

code.jsJavaScript
const { StringDecoder } = require('string_decoder');

const decoder = new StringDecoder('utf8');

const chunk1 = Buffer.from([0xE2, 0x82]);
const chunk2 = Buffer.from([0xAC]);

// ✅ Correct - buffers incomplete characters
console.log(decoder.write(chunk1)); // '' (buffered)
console.log(decoder.write(chunk2)); // '€'

// End and flush remaining
console.log(decoder.end());

With Streams

code.jsJavaScript
const { StringDecoder } = require('string_decoder');
const fs = require('fs');

const decoder = new StringDecoder('utf8');
const stream = fs.createReadStream('file.txt');

let text = '';
stream.on('data', (chunk) => {
  text += decoder.write(chunk);
});

stream.on('end', () => {
  text += decoder.end();
  console.log(text);
});