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.
v2ray-core/common/buf/io.go

116 lines
2.4 KiB

8 years ago
package buf
import (
"io"
8 years ago
"time"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/signal"
)
// Reader extends io.Reader with alloc.Buffer.
type Reader interface {
// Read reads content from underlying reader, and put it into an alloc.Buffer.
8 years ago
Read() (MultiBuffer, error)
}
var ErrReadTimeout = newError("IO timeout")
8 years ago
type TimeoutReader interface {
8 years ago
ReadTimeout(time.Duration) (MultiBuffer, error)
8 years ago
}
// Writer extends io.Writer with alloc.Buffer.
type Writer interface {
// Write writes an alloc.Buffer into underlying writer.
8 years ago
Write(MultiBuffer) error
}
8 years ago
8 years ago
// ReadFrom creates a Supplier to read from a given io.Reader.
8 years ago
func ReadFrom(reader io.Reader) Supplier {
return func(b []byte) (int, error) {
return reader.Read(b)
}
}
8 years ago
// ReadFullFrom creates a Supplier to read full buffer from a given io.Reader.
8 years ago
func ReadFullFrom(reader io.Reader, size int) Supplier {
return func(b []byte) (int, error) {
return io.ReadFull(reader, b[:size])
}
}
8 years ago
func copyInternal(timer signal.ActivityTimer, reader Reader, writer Writer) error {
for {
buffer, err := reader.Read()
if err != nil {
return err
}
timer.Update()
if buffer.IsEmpty() {
buffer.Release()
continue
}
err = writer.Write(buffer)
if err != nil {
buffer.Release()
return err
}
}
}
8 years ago
// Copy dumps all payload from reader to writer or stops when an error occurs.
// ActivityTimer gets updated as soon as there is a payload.
func Copy(timer signal.ActivityTimer, reader Reader, writer Writer) error {
err := copyInternal(timer, reader, writer)
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 {
8 years ago
if mr, ok := reader.(MultiBufferReader); ok {
return &readerAdpater{
MultiBufferReader: mr,
}
}
return &BytesToBufferReader{
reader: reader,
buffer: make([]byte, 32*1024),
}
}
// ToBytesReader converts a Reaaer to io.Reader.
func ToBytesReader(stream Reader) io.Reader {
return &bufferToBytesReader{
stream: stream,
}
}
// NewWriter creates a new Writer.
func NewWriter(writer io.Writer) Writer {
8 years ago
if mw, ok := writer.(MultiBufferWriter); ok {
return &writerAdapter{
writer: mw,
}
}
return &BufferToBytesWriter{
writer: writer,
}
}
// ToBytesWriter converts a Writer to io.Writer
func ToBytesWriter(writer Writer) io.Writer {
return &bytesToBufferWriter{
writer: writer,
}
}