Open In App

Node.js Stream writable._write() Method

Last Updated : 11 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The writable._write() method is an inbuilt application programming interface of Stream module which is used to implement a writable stream. The writable._write() method is affixed with an underscore as it is inside the class that defines it. Moreover, the user program must not call it directly. This method implemented by using child classes and it is called by the internal Writable class methods only.

Syntax:

writable._write( chunk, encoding, callback )

Parameters: This method accept three parameters as mentioned above and described below:

  • chunk: It is the data to be written which can be of type buffer, string or any.
  • encoding: It is the type of encoding to be used if chunk is of type string.
  • callback: It is a method which is called to check if the write completed or failed. The first argument passed to the callback must be “Error” object if the call didn’t succeeded and null if the write succeeded.

Below examples illustrate the use of writable._write() method Node.js:

Example 1:




// Node.js program to demonstrate the     
// writable._write() method
  
// Constructing writable stream
const {Writable} = require("stream");
  
// Function to check char
const charchecks = new Writable({
  
  // Implementing write function
  write(chunk, encoding, callback){
  
    // Defining string
    const string = chunk.toString();
      
    // If string contains below character
    // then an error is show else the
    // written string is returned
    if(string.includes("\/")){
      callback(Error("Forbidden character"));
      
    }
    else
    {
      // Displays string
      console.log(string);
      callback();
    }
  }
});
  
// Piping standard input to standard output, if
// you don't enter the forbidden character else
// it throws error
process.stdin.pipe(charchecks).on('error', console.log);
  
// Enter the string to be written
console.log("Enter the string: ");


Now, you need to run the code and enter the String in run time to get the output.

Enter the string:
GeeksforGeeks
GeeksforGeeks // Output

Enter the string:
GfG
GfG // Output

Enter the string:
Nidhi
Nidhi //Output

Now, to exit it you need to press control + C.

Example 2:




// Node.js program to demonstrate the     
// writable._write() method
   
// Constructing writable stream
const {Writable} = require("stream");
   
// Function to check char
const charchecks = new Writable({
   
  // Implementing write function
  write(chunk, encoding, callback){
   
    // Defining string and encoding it
    const string = chunk.toString('hex');
   
    // Prints encoded string
    console.log(string);
       
    // If the encoded string contains below 
    // character then an error is shown else
    // the length of the encoded string is 
    // returned
    if(string.includes("c")){
         
      callback(Error("This is an error."));
       
    }
    else
    {
      // Displays length of the encoded string
      console.log(string.length);
      callback();
    }
  }
});
   
// Piping standard input to standard output, if
// you don't enter the forbidden character else
// it throws an error
process.stdin.pipe(charchecks).on('error', console.log);
   
// Enter the string to be written
console.log("Enter the string: ");


Now, you need to run the code and enter the String in run time to get the output.
Output:

Enter the string:
Geeks
4765656b730a   // encoded string
12             // length of encoded string
Nidhi
4e696468690a
12
portal
706f7274616c0a   // encoded string contains "c" so length of it  
                 // is not returned and an error is thrown
Error: This is an error.
    at Writable.write [as _write] (/home/runner/QuickwittedDistantCensorware/index.js:25:16)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at Writable.write (_stream_writable.js:299:11)
    at ReadStream.ondata (_stream_readable.js:710:20)
    at ReadStream.emit (events.js:198:13)
    at ReadStream.EventEmitter.emit (domain.js:448:20)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at ReadStream.Readable.push (_stream_readable.js:224:10)

Now, to exit it you need to press control C.
So, here an error is thrown as the run time input includes the stated character which is forbidden character so, an error is thrown.

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



Previous Article
Next Article

Similar Reads

Node.js Stream writable.writable Property
The writable.writable property is an inbuilt application programming interface of Stream module which is used to check the writable.write() method is safe to call or not. Syntax: writable.writable Return Value: It returns true if it is safe to call writable.write() method otherwise returns false. Below examples illustrate the use of writable.writab
2 min read
Node.js Stream writable.cork() Method
The writable.cork() method is an inbuilt application programming interface of Stream module which is used to write every data into the buffer memory. When we use stream.uncork() or stream.end() methods then the buffer data will be flushed. Syntax: writable.cork() Parameters: This method does not accept any parameters. Return Value: If this method i
2 min read
Node.js Stream writable.write() Method
The writable.write() method is an inbuilt application programming interface of Stream module which is used to write some data to the Writable stream. The callback function is called once the data has been completely handled. Syntax: writable.write( chunk, encoding, callback) Parameters: This method accepts three parameters as mentioned above and de
2 min read
Node.js Stream writable.end() Method
The writable.end() method is an inbuilt application programming interface of Stream module so that no more data can be written to the Writable anymore. The arguments chunk and encoding are optional which will permit one final new chunk of data to be written instantly before closing the stream. Moreover, the optional callback function is added as a
3 min read
Node.js Stream writable.destroy() Method
The writable.destroy() method is an inbuilt application programming interface of Stream module which is used to destroy the created stream and you cannot call the write() method to write data again after you have already destroyed the created stream. Syntax: writable.destroy() Parameters: This method does not accept any parameters. Return Value: It
2 min read
Node.js Stream writable.uncork() Method
The writable.uncork() method is an inbuilt application programming interface of Stream module which is used to flush all the buffered data when stream.cork() method was called. Syntax: writable.uncork() Parameters: This method does not accept any parameters. Return Value: If this method is being called then the data which was being corked is again
2 min read
Node.js Stream writable.setDefaultEncoding() Method
The writable.setDefaultEncoding() method is an inbuilt application programming interface of Stream module which is used to set the default encoding for a Writable stream. Syntax: writable.setDefaultEncoding( encoding ) Parameters: This method accepts single parameter encoding which holds the encoding to be used for the Writable stream. Return Value
2 min read
Node.js Stream writable.writableFinished Property
The writable.writableFinished property is set to true instantly before the emit of the 'finish' event. Syntax: writable.writableFinished Return Value: It returns true if 'finish' event is called before it else it returns false. Below examples illustrate the use of writable.writableFinished property in Node.js: Example 1: // Node.js program to demon
2 min read
Node.js Stream writable.writableObjectMode Property
The writable.writableObjectMode property in Stream module is used to get the object mode value of the Writable stream. Syntax: writable.writableObjectMode Return Value: It returns true if the objectMode is set to true otherwise returns false. Below examples illustrate the use of writable.writableObjectMode property in Node.js: Example 1: // Node.js
2 min read
Node.js Stream writable.writableLength Property
The writable.writableLength property is an inbuilt application of stream module which is used to check the number of bytes in the queue that is ready to be written. Syntax: writable.writableLength Return Value: This property returns the number of bytes that is ready to write into the queue. Below examples illustrate the use of writable.writableLeng
2 min read