writable._write(chunk, encoding, callback)


  • chunk <Buffer> | <string> | <any> 要写入的 Buffer,从 string 转换为 stream.write()。 如果流的 decodeStrings 选项是 false 或者流在对象模式下运行,则块将不会被转换,而是传给 stream.write() 的任何内容。
  • encoding <string> 如果块是字符串,则 encoding 是该字符串的字符编码。 如果块是 Buffer,或者如果流在对象模式下运行,则可以忽略 encoding
  • callback <Function> 当对提供的块的处理完成时调用此函数(可选地带有错误参数)。

所有 Writable 流实现都必须提供 writable._write() 和/或 writable._writev() 方法来将数据发送到底层资源。

Transform 流提供了它们自己的 writable._write() 实现。

此函数不得由应用程序代码直接调用。 它应该由子类实现,并且只能由内部 Writable 类方法调用。

如果调用失败,则传给 callback 的第一个参数必须是 Error 对象,如果写入成功,则传入 null 对象。

在调用 writable._write() 和调用 callback 之间发生的对 writable.write() 的所有调用都将导致写入的数据被缓冲。 当调用 callback 时,流可能会触发 'drain' 事件。 如果流实现能够同时处理多个数据块,则应实现 writable._writev() 方法。

如果在构造函数选项中将 decodeStrings 属性显式设置为 false,则 chunk 将保持传给 .write() 的相同对象,并且可能是字符串而不是 Buffer。 这是为了支持对某些字符串数据编码进行优化处理的实现。 在这种情况下,encoding 参数将指示字符串的字符编码。 否则,可以安全地忽略 encoding 参数。

writable._write() 方法以下划线为前缀,因为它是定义它的类的内部方法,不应由用户程序直接调用。

  • chunk <Buffer> | <string> | <any> The Buffer to be written, converted from the string passed to stream.write(). If the stream's decodeStrings option is false or the stream is operating in object mode, the chunk will not be converted & will be whatever was passed to stream.write().
  • encoding <string> If the chunk is a string, then encoding is the character encoding of that string. If chunk is a Buffer, or if the stream is operating in object mode, encoding may be ignored.
  • callback <Function> Call this function (optionally with an error argument) when processing is complete for the supplied chunk.

All Writable stream implementations must provide a writable._write() and/or writable._writev() method to send data to the underlying resource.

Transform streams provide their own implementation of the writable._write().

This function MUST NOT be called by application code directly. It should be implemented by child classes, and called by the internal Writable class methods only.

The callback method must be called to signal either that the write completed successfully or failed with an error. The first argument passed to the callback must be the Error object if the call failed or null if the write succeeded.

All calls to writable.write() that occur between the time writable._write() is called and the callback is called will cause the written data to be buffered. When the callback is invoked, the stream might emit a 'drain' event. If a stream implementation is capable of processing multiple chunks of data at once, the writable._writev() method should be implemented.

If the decodeStrings property is explicitly set to false in the constructor options, then chunk will remain the same object that is passed to .write(), and may be a string rather than a Buffer. This is to support implementations that have an optimized handling for certain string data encodings. In that case, the encoding argument will indicate the character encoding of the string. Otherwise, the encoding argument can be safely ignored.

The writable._write() method is prefixed with an underscore because it is internal to the class that defines it, and should never be called directly by user programs.