mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.9 KiB
76 lines
1.9 KiB
package buf
|
|
|
|
import (
|
|
"io"
|
|
"time"
|
|
)
|
|
|
|
// Reader extends io.Reader with MultiBuffer.
|
|
type Reader interface {
|
|
// ReadMultiBuffer reads content from underlying reader, and put it into a MultiBuffer.
|
|
ReadMultiBuffer() (MultiBuffer, error)
|
|
}
|
|
|
|
// ErrReadTimeout is an error that happens with IO timeout.
|
|
var ErrReadTimeout = newError("IO timeout")
|
|
|
|
// TimeoutReader is a reader that returns error if Read() operation takes longer than the given timeout.
|
|
type TimeoutReader interface {
|
|
ReadTimeout(time.Duration) (MultiBuffer, error)
|
|
}
|
|
|
|
// Writer extends io.Writer with MultiBuffer.
|
|
type Writer interface {
|
|
// WriteMultiBuffer writes a MultiBuffer into underlying writer.
|
|
WriteMultiBuffer(MultiBuffer) error
|
|
}
|
|
|
|
// ReadFrom creates a Supplier to read from a given io.Reader.
|
|
func ReadFrom(reader io.Reader) Supplier {
|
|
return func(b []byte) (int, error) {
|
|
return reader.Read(b)
|
|
}
|
|
}
|
|
|
|
// ReadFullFrom creates a Supplier to read full buffer from a given io.Reader.
|
|
func ReadFullFrom(reader io.Reader, size int32) Supplier {
|
|
return func(b []byte) (int, error) {
|
|
return io.ReadFull(reader, b[:size])
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
// NewReader creates a new Reader.
|
|
// The Reader instance doesn't take the ownership of reader.
|
|
func NewReader(reader io.Reader) Reader {
|
|
if mr, ok := reader.(Reader); ok {
|
|
return mr
|
|
}
|
|
|
|
return NewBytesToBufferReader(reader)
|
|
}
|
|
|
|
// NewWriter creates a new Writer.
|
|
func NewWriter(writer io.Writer) Writer {
|
|
if mw, ok := writer.(Writer); ok {
|
|
return mw
|
|
}
|
|
|
|
return &BufferToBytesWriter{
|
|
Writer: writer,
|
|
}
|
|
}
|
|
|
|
// NewSequentialWriter returns a Writer that write Buffers in a MultiBuffer sequentially.
|
|
func NewSequentialWriter(writer io.Writer) Writer {
|
|
return &seqWriter{
|
|
writer: writer,
|
|
}
|
|
}
|