Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js Stream readable.readableEncoding Property

  • Last Updated : 12 Oct, 2021

The readable.readableEncodin property in a readable Stream is utilized to get the property encoding of a stated readable stream. Moreover, you can set this property using readable.setEncoding() method.

Syntax:

readable.readableEncoding

Return Value: It returns the encoding used in the program.

Below examples illustrate the use of readable.readableEncoding property in Node.js:

Example 1:






// Node.js program to demonstrate the     
// readable.readableEncoding Property  
  
// Include fs module
const fs = require("fs");
  
// Constructing readable stream
const readable = fs.createReadStream("input.text");
  
// Setting the encoding 
readable.setEncoding("base64");
  
// Instructions for reading data
readable.on('readable', () => {
  let chunk;
  
  // Using while loop and calling
  // read method with parameter
  while (null !== (chunk = readable.read())) {
  
    // Displaying the chunk
    console.log(`read: ${chunk}`);
  }
});
  
// Calling readableEncoding property
readable.readableEncoding;

Output:

read: aGVs
read: bG8=

Example 2:




// Node.js program to demonstrate the     
// readable.readableEncoding Property  
  
// Include fs module
const fs = require("fs");
  
// Constructing readable stream
const readable = fs.createReadStream("input.text");
  
// Setting the encoding 
readable.setEncoding("hex");
  
// Instructions for reading data
readable.on('readable', () => {
  let chunk;
  
  // Using while loop and calling
  // read method with parameter
  while (null !== (chunk = readable.read())) {
  
    // Displaying the chunk
    console.log(`read: ${chunk}`);
  }
});
  
// Calling readableEncoding property
readable.readableEncoding;

Output:

read: 68656c6c6f

Reference: https://nodejs.org/api/stream.html#stream_readable_readableencoding.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!