📜  Node.js Stream writeable._write() 方法

📅  最后修改于: 2022-05-13 01:56:46.907000             🧑  作者: Mango

Node.js Stream writeable._write() 方法

writable._write() 方法是 Stream 模块的内置应用程序编程接口,用于实现可写流。 writable._write() 方法带有下划线,因为它位于定义它的类中。此外,用户程序不得直接调用它。该方法使用子类实现,仅由内部可写类方法调用。

句法:

writable._write( chunk, encoding, callback )

参数:此方法接受三个参数,如上所述,如下所述:

  • chunk:要写入的数据,可以是缓冲区、字符串或任何类型。
  • encoding:如果 chunk 是字符串类型,则它是要使用的编码类型。
  • 回调:这是一个调用来检查写入是否完成或失败的方法。如果调用不成功,则传递给回调的第一个参数必须是“Error”对象,如果写入成功,则必须为 null。

下面的例子说明了 Node.js 中writable._write() 方法的使用:

示例 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: ");

现在,您需要运行代码并在运行时输入字符串以获取输出。

Enter the string:
GeeksforGeeks
GeeksforGeeks // Output

Enter the string:
GfG
GfG // Output

Enter the string:
Nidhi
Nidhi //Output

现在,要退出它,您需要按 control + C。

示例 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: ");

现在,您需要运行代码并在运行时输入字符串以获取输出。
输出:

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)

现在,要退出它,您需要按 control C。
因此,这里会抛出一个错误,因为运行时输入包含禁止字符的规定字符,因此会抛出一个错误。

参考: https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback_1