diff --git a/common/buf/writer.go b/common/buf/writer.go index 2ea3cad9..f51fa3fb 100644 --- a/common/buf/writer.go +++ b/common/buf/writer.go @@ -26,12 +26,14 @@ func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error { return err } +// ReadFrom implements io.ReaderFrom. func (w *BufferToBytesWriter) ReadFrom(reader io.Reader) (int64, error) { var sc SizeCounter err := Copy(NewReader(reader), w, CountSize(&sc)) return sc.Size, err } +// BufferedWriter is a Writer with internal buffer. type BufferedWriter struct { writer Writer legacyWriter io.Writer @@ -39,6 +41,7 @@ type BufferedWriter struct { buffered bool } +// NewBufferedWriter creates a new BufferedWriter. func NewBufferedWriter(writer Writer) *BufferedWriter { w := &BufferedWriter{ writer: writer, @@ -51,6 +54,7 @@ func NewBufferedWriter(writer Writer) *BufferedWriter { return w } +// Write implements io.Writer. func (w *BufferedWriter) Write(b []byte) (int, error) { if !w.buffered && w.legacyWriter != nil { return w.legacyWriter.Write(b) @@ -78,6 +82,7 @@ func (w *BufferedWriter) Write(b []byte) (int, error) { return totalBytes, nil } +// WriteMultiBuffer implements Writer. It takes ownership of the given MultiBuffer. func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error { if !w.buffered { return w.writer.WriteMultiBuffer(b) @@ -99,6 +104,7 @@ func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error { return nil } +// Flush flushes buffered content into underlying writer. func (w *BufferedWriter) Flush() error { if !w.buffer.IsEmpty() { if err := w.writer.WriteMultiBuffer(NewMultiBufferValue(w.buffer)); err != nil {