2016-12-09 06:08:25 -05:00
|
|
|
package buf
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
import (
|
|
|
|
"io"
|
2017-03-27 05:12:34 -04:00
|
|
|
"time"
|
2016-12-09 07:17:34 -05:00
|
|
|
)
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
// Reader extends io.Reader with MultiBuffer.
|
2016-12-09 07:17:34 -05:00
|
|
|
type Reader interface {
|
2017-11-09 16:33:15 -05:00
|
|
|
// ReadMultiBuffer reads content from underlying reader, and put it into a MultiBuffer.
|
|
|
|
ReadMultiBuffer() (MultiBuffer, error)
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
2017-10-22 09:03:20 -04:00
|
|
|
// ErrReadTimeout is an error that happens with IO timeout.
|
2017-04-09 09:04:04 -04:00
|
|
|
var ErrReadTimeout = newError("IO timeout")
|
2017-03-27 05:12:34 -04:00
|
|
|
|
2017-11-08 12:14:44 -05:00
|
|
|
// TimeoutReader is a reader that returns error if Read() operation takes longer than the given timeout.
|
2017-03-27 05:12:34 -04:00
|
|
|
type TimeoutReader interface {
|
2017-04-15 15:07:23 -04:00
|
|
|
ReadTimeout(time.Duration) (MultiBuffer, error)
|
2017-03-27 05:12:34 -04:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
// Writer extends io.Writer with MultiBuffer.
|
2016-12-09 07:17:34 -05:00
|
|
|
type Writer interface {
|
2017-11-09 16:33:15 -05:00
|
|
|
// WriteMultiBuffer writes a MultiBuffer into underlying writer.
|
|
|
|
WriteMultiBuffer(MultiBuffer) error
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
2016-12-09 06:08:25 -05:00
|
|
|
|
2016-12-11 03:44:24 -05:00
|
|
|
// ReadFrom creates a Supplier to read from a given io.Reader.
|
2016-12-09 06:08:25 -05:00
|
|
|
func ReadFrom(reader io.Reader) Supplier {
|
|
|
|
return func(b []byte) (int, error) {
|
|
|
|
return reader.Read(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 03:44:24 -05:00
|
|
|
// ReadFullFrom creates a Supplier to read full buffer from a given io.Reader.
|
2016-12-09 06:08:25 -05:00
|
|
|
func ReadFullFrom(reader io.Reader, size int) Supplier {
|
|
|
|
return func(b []byte) (int, error) {
|
|
|
|
return io.ReadFull(reader, b[:size])
|
|
|
|
}
|
|
|
|
}
|
2016-12-09 07:17:34 -05:00
|
|
|
|
2017-04-27 07:31:09 -04:00
|
|
|
// ReadAtLeastFrom create a Supplier to read at least size bytes from the given io.Reader.
|
|
|
|
func ReadAtLeastFrom(reader io.Reader, size int) Supplier {
|
|
|
|
return func(b []byte) (int, error) {
|
|
|
|
return io.ReadAtLeast(reader, b, size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
// NewReader creates a new Reader.
|
|
|
|
// The Reader instance doesn't take the ownership of reader.
|
|
|
|
func NewReader(reader io.Reader) Reader {
|
2017-11-09 16:33:15 -05:00
|
|
|
if mr, ok := reader.(Reader); ok {
|
|
|
|
return mr
|
2017-04-15 16:22:29 -04:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
return NewBytesToBufferReader(reader)
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWriter creates a new Writer.
|
|
|
|
func NewWriter(writer io.Writer) Writer {
|
2017-11-09 16:33:15 -05:00
|
|
|
if mw, ok := writer.(Writer); ok {
|
|
|
|
return mw
|
2017-04-19 15:27:21 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
return &BufferToBytesWriter{
|
2017-11-09 16:33:15 -05:00
|
|
|
Writer: writer,
|
2017-04-20 05:00:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// NewSequentialWriter returns a Writer that write Buffers in a MultiBuffer sequentially.
|
2017-04-21 08:51:09 -04:00
|
|
|
func NewSequentialWriter(writer io.Writer) Writer {
|
|
|
|
return &seqWriter{
|
|
|
|
writer: writer,
|
|
|
|
}
|
|
|
|
}
|