From ccb2a9f168cf95e635ba44051e8a4bed5e03115f Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 13 Feb 2018 11:15:04 +0100 Subject: [PATCH] comments --- common/buf/copy.go | 1 + common/buf/io.go | 1 + common/buf/reader.go | 16 +++++++++------- common/buf/writer.go | 3 +++ 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/common/buf/copy.go b/common/buf/copy.go index d4f65235d..7d19efdda 100644 --- a/common/buf/copy.go +++ b/common/buf/copy.go @@ -36,6 +36,7 @@ func (h *copyHandler) writeTo(writer Writer, mb MultiBuffer) error { return err } +// SizeCounter is for counting bytes copied by Copy(). type SizeCounter struct { Size int64 } diff --git a/common/buf/io.go b/common/buf/io.go index 17debb2b5..e3c8aa338 100644 --- a/common/buf/io.go +++ b/common/buf/io.go @@ -67,6 +67,7 @@ func NewWriter(writer io.Writer) Writer { } } +// NewSequentialWriter returns a Writer that write Buffers in a MultiBuffer sequentially. func NewSequentialWriter(writer io.Writer) Writer { return &seqWriter{ writer: writer, diff --git a/common/buf/reader.go b/common/buf/reader.go index 574bd5f61..45dc4ef32 100644 --- a/common/buf/reader.go +++ b/common/buf/reader.go @@ -12,6 +12,7 @@ type BytesToBufferReader struct { buffer []byte } +// NewBytesToBufferReader returns a new BytesToBufferReader. func NewBytesToBufferReader(reader io.Reader) Reader { return &BytesToBufferReader{ Reader: reader, @@ -52,19 +53,14 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) { return nil, err } -var ( - _ Reader = (*BufferedReader)(nil) - _ io.Reader = (*BufferedReader)(nil) - _ io.ByteReader = (*BufferedReader)(nil) - _ io.WriterTo = (*BufferedReader)(nil) -) - +// BufferedReader is a Reader that keeps its internal buffer. type BufferedReader struct { stream Reader leftOver MultiBuffer buffered bool } +// NewBufferedReader returns a new BufferedReader. func NewBufferedReader(reader Reader) *BufferedReader { return &BufferedReader{ stream: reader, @@ -72,20 +68,24 @@ func NewBufferedReader(reader Reader) *BufferedReader { } } +// SetBuffered sets whether to keep the interal buffer. func (r *BufferedReader) SetBuffered(f bool) { r.buffered = f } +// IsBuffered returns true if internal buffer is used. func (r *BufferedReader) IsBuffered() bool { return r.buffered } +// ReadByte implements io.ByteReader. func (r *BufferedReader) ReadByte() (byte, error) { var b [1]byte _, err := r.Read(b[:]) return b[0], err } +// Read implements io.Reader. It reads from internal buffer first (if available) and then reads from the underlying reader. func (r *BufferedReader) Read(b []byte) (int, error) { if r.leftOver != nil { nBytes, _ := r.leftOver.Read(b) @@ -113,6 +113,7 @@ func (r *BufferedReader) Read(b []byte) (int, error) { return 0, err } +// ReadMultiBuffer implements Reader. func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error) { if r.leftOver != nil { mb := r.leftOver @@ -164,6 +165,7 @@ func (r *BufferedReader) writeToInternal(writer io.Writer) (int64, error) { } } +// WriteTo implements io.WriterTo. func (r *BufferedReader) WriteTo(writer io.Writer) (int64, error) { nBytes, err := r.writeToInternal(writer) if errors.Cause(err) == io.EOF { diff --git a/common/buf/writer.go b/common/buf/writer.go index 4d26fb979..05798cf8b 100644 --- a/common/buf/writer.go +++ b/common/buf/writer.go @@ -11,6 +11,7 @@ type BufferToBytesWriter struct { io.Writer } +// NewBufferToBytesWriter returns a new BufferToBytesWriter. func NewBufferToBytesWriter(writer io.Writer) *BufferToBytesWriter { return &BufferToBytesWriter{ Writer: writer, @@ -49,6 +50,7 @@ func NewBufferedWriter(writer Writer) *BufferedWriter { } } +// WriteByte implements io.ByteWriter. func (w *BufferedWriter) WriteByte(c byte) error { _, err := w.Write([]byte{c}) return err @@ -121,6 +123,7 @@ func (w *BufferedWriter) Flush() error { return nil } +// SetBuffered sets whether the internal buffer is used. If set to false, Flush() will be called to clear the buffer. func (w *BufferedWriter) SetBuffered(f bool) error { w.buffered = f if !f {