v2ray-core/common/buf/io.go

104 lines
2.3 KiB
Go
Raw Normal View History

2016-12-09 11:08:25 +00:00
package buf
2016-12-09 12:17:34 +00:00
import (
"io"
2017-03-27 09:12:34 +00:00
"time"
2016-12-09 12:17:34 +00:00
"v2ray.com/core/common/errors"
2017-01-31 11:42:05 +00:00
"v2ray.com/core/common/signal"
2016-12-09 12:17:34 +00:00
)
// Reader extends io.Reader with alloc.Buffer.
type Reader interface {
// Read reads content from underlying reader, and put it into an alloc.Buffer.
Read() (*Buffer, error)
}
2017-04-08 23:43:25 +00:00
var ErrReadTimeout = newError("Buf: IO timeout.")
2017-03-27 09:12:34 +00:00
type TimeoutReader interface {
ReadTimeout(time.Duration) (*Buffer, error)
}
2016-12-09 12:17:34 +00:00
// Writer extends io.Writer with alloc.Buffer.
type Writer interface {
// Write writes an alloc.Buffer into underlying writer.
Write(*Buffer) error
}
2016-12-09 11:08:25 +00:00
2016-12-11 08:44:24 +00:00
// ReadFrom creates a Supplier to read from a given io.Reader.
2016-12-09 11:08:25 +00:00
func ReadFrom(reader io.Reader) Supplier {
return func(b []byte) (int, error) {
return reader.Read(b)
}
}
2016-12-11 08:44:24 +00:00
// ReadFullFrom creates a Supplier to read full buffer from a given io.Reader.
2016-12-09 11:08:25 +00:00
func ReadFullFrom(reader io.Reader, size int) Supplier {
return func(b []byte) (int, error) {
return io.ReadFull(reader, b[:size])
}
}
2016-12-09 12:17:34 +00:00
2017-02-15 21:51:01 +00:00
// Pipe dumps all payload from reader to writer, until an error occurs.
// ActivityTimer gets updated as soon as there is a payload.
2017-04-04 08:24:38 +00:00
func Pipe(timer signal.ActivityTimer, reader Reader, writer Writer) error {
2016-12-09 12:17:34 +00:00
for {
buffer, err := reader.Read()
if err != nil {
return err
}
2017-03-31 19:45:43 +00:00
timer.Update()
2017-01-31 11:42:05 +00:00
2016-12-09 12:17:34 +00:00
if buffer.IsEmpty() {
buffer.Release()
continue
}
err = writer.Write(buffer)
if err != nil {
buffer.Release()
return err
}
}
}
// PipeUntilEOF behaves the same as Pipe(). The only difference is PipeUntilEOF returns nil on EOF.
2017-04-04 08:24:38 +00:00
func PipeUntilEOF(timer signal.ActivityTimer, reader Reader, writer Writer) error {
2017-01-31 11:42:05 +00:00
err := Pipe(timer, reader, writer)
2016-12-09 12:17:34 +00:00
if err != nil && errors.Cause(err) != io.EOF {
return err
}
return nil
}
// NewReader creates a new Reader.
// The Reader instance doesn't take the ownership of reader.
func NewReader(reader io.Reader) Reader {
return &BytesToBufferReader{
reader: reader,
}
}
2017-02-15 21:51:01 +00:00
// ToBytesReader converts a Reaaer to io.Reader.
func ToBytesReader(stream Reader) io.Reader {
2017-02-20 09:33:35 +00:00
return &bufferToBytesReader{
2016-12-09 12:17:34 +00:00
stream: stream,
}
}
// NewWriter creates a new Writer.
func NewWriter(writer io.Writer) Writer {
return &BufferToBytesWriter{
writer: writer,
}
}
2017-02-15 21:51:01 +00:00
// ToBytesWriter converts a Writer to io.Writer
func ToBytesWriter(writer Writer) io.Writer {
2017-02-20 09:33:35 +00:00
return &bytesToBufferWriter{
2016-12-09 12:17:34 +00:00
writer: writer,
}
}