writable._write(chunk, encoding, callback)
-
chunk<Buffer> | <string> | <any> 要写入的Buffer,从string转换为stream.write()。如果流的decodeStrings选项是false或者流在对象模式下运行,则块将不会被转换并且将是传递给stream.write()的任何内容。¥
chunk<Buffer> | <string> | <any> TheBufferto be written, converted from thestringpassed tostream.write(). If the stream'sdecodeStringsoption isfalseor the stream is operating in object mode, the chunk will not be converted & will be whatever was passed tostream.write(). -
encoding<string> 如果块是字符串,则encoding是该字符串的字符编码。如果块是Buffer,或者如果流以对象模式运行,则encoding可能会被忽略。¥
encoding<string> If the chunk is a string, thenencodingis the character encoding of that string. If chunk is aBuffer, or if the stream is operating in object mode,encodingmay be ignored. -
callback<Function> 当对提供的块的处理完成时调用此函数(可选地带有错误参数)。¥
callback<Function> Call this function (optionally with an error argument) when processing is complete for the supplied chunk.
所有 Writable 流实现都必须提供 writable._write() 和/或 writable._writev() 方法来将数据发送到底层资源。
¥All Writable stream implementations must provide a
writable._write() and/or
writable._writev() method to send data to the underlying
resource.
Transform 流提供了它们自己的 writable._write() 实现。
¥Transform streams provide their own implementation of the
writable._write().
此函数不得由应用代码直接调用。它应该由子类实现,并且只能由内部 Writable 类方法调用。
¥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.
callback 函数必须在 writable._write() 内部同步调用或异步调用(即不同的时钟周期),以触发写入成功完成或因错误而失败的信号。如果调用失败,传递给 callback 的第一个参数必须是 Error 对象,如果写入成功,则必须是 null。
¥The callback function must be called synchronously inside of
writable._write() or asynchronously (i.e. different tick) 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.
在调用 writable._write() 和调用 callback 之间发生的对 writable.write() 的所有调用都会导致写入的数据被缓冲。调用 callback 时,流可能会触发 'drain' 事件。如果流实现能够一次处理多个数据块,则应实现 writable._writev() 方法。
¥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.
如果在构造函数选项中将 decodeStrings 属性显式设置为 false,则 chunk 将保持传递给 .write() 的同一对象,并且可能是字符串而不是 Buffer。这是为了支持对某些字符串数据编码进行优化处理的实现。在这种情况下,encoding 参数将指示字符串的字符编码。否则,可以安全地忽略 encoding 参数。
¥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.
writable._write() 方法带有下划线前缀,因为它是定义它的类的内部方法,不应由用户程序直接调用。
¥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.