v2ray-core/common/buf/io.go

204 lines
4.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.
2017-04-15 19:07:23 +00:00
Read() (MultiBuffer, error)
2016-12-09 12:17:34 +00:00
}
2017-04-09 13:04:04 +00:00
var ErrReadTimeout = newError("IO timeout")
2017-03-27 09:12:34 +00:00
type TimeoutReader interface {
2017-04-15 19:07:23 +00:00
ReadTimeout(time.Duration) (MultiBuffer, error)
2017-03-27 09:12:34 +00:00
}
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.
2017-04-15 19:07:23 +00:00
Write(MultiBuffer) error
2016-12-09 12:17:34 +00:00
}
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-04-27 11:31:09 +00: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)
}
}
2017-04-27 20:20:29 +00:00
type copyHandler struct {
onReadError func(error) error
onData func()
onWriteError func(error) error
}
2017-04-27 21:33:07 +00:00
func (h *copyHandler) readFrom(reader Reader) (MultiBuffer, error) {
mb, err := reader.Read()
if err != nil && h.onReadError != nil {
err = h.onReadError(err)
}
return mb, err
}
func (h *copyHandler) writeTo(writer Writer, mb MultiBuffer) error {
err := writer.Write(mb)
if err != nil && h.onWriteError != nil {
err = h.onWriteError(err)
}
return err
}
2017-04-27 20:20:29 +00:00
type CopyOption func(*copyHandler)
func IgnoreReaderError() CopyOption {
return func(handler *copyHandler) {
handler.onReadError = func(err error) error {
return nil
}
}
}
func IgnoreWriterError() CopyOption {
return func(handler *copyHandler) {
handler.onWriteError = func(err error) error {
return nil
}
}
}
2017-04-27 20:30:48 +00:00
func UpdateActivity(timer signal.ActivityTimer) CopyOption {
return func(handler *copyHandler) {
handler.onData = func() {
timer.Update()
}
}
}
2017-04-27 21:33:07 +00:00
func copyInternal(reader Reader, writer Writer, handler *copyHandler) error {
2016-12-09 12:17:34 +00:00
for {
2017-04-27 21:33:07 +00:00
buffer, err := handler.readFrom(reader)
2016-12-09 12:17:34 +00:00
if err != nil {
2017-04-27 21:33:07 +00:00
return err
2016-12-09 12:17:34 +00:00
}
if buffer.IsEmpty() {
buffer.Release()
continue
}
2017-04-27 21:33:07 +00:00
if handler.onData != nil {
handler.onData()
}
if err := handler.writeTo(writer, buffer); err != nil {
buffer.Release()
return err
2016-12-09 12:17:34 +00:00
}
}
}
2017-04-17 20:50:02 +00:00
// Copy dumps all payload from reader to writer or stops when an error occurs.
// ActivityTimer gets updated as soon as there is a payload.
2017-04-27 20:30:48 +00:00
func Copy(reader Reader, writer Writer, options ...CopyOption) error {
2017-04-27 21:33:07 +00:00
handler := new(copyHandler)
2017-04-27 20:20:29 +00:00
for _, option := range options {
2017-04-27 21:33:07 +00:00
option(handler)
2017-04-27 20:20:29 +00:00
}
2017-04-27 20:30:48 +00:00
err := copyInternal(reader, writer, handler)
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 {
2017-04-15 20:22:29 +00:00
if mr, ok := reader.(MultiBufferReader); ok {
return &readerAdpater{
MultiBufferReader: mr,
}
}
2016-12-09 12:17:34 +00:00
return &BytesToBufferReader{
reader: reader,
2017-04-19 21:30:18 +00:00
buffer: make([]byte, 32*1024),
2016-12-09 12:17:34 +00:00
}
}
2017-04-20 09:00:15 +00:00
func NewMergingReader(reader io.Reader) Reader {
return NewMergingReaderSize(reader, 32*1024)
}
func NewMergingReaderSize(reader io.Reader, size uint32) Reader {
return &BytesToBufferReader{
reader: reader,
buffer: make([]byte, size),
}
}
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 {
2017-04-19 19:27:21 +00:00
if mw, ok := writer.(MultiBufferWriter); ok {
return &writerAdapter{
writer: mw,
}
}
2016-12-09 12:17:34 +00:00
return &BufferToBytesWriter{
writer: writer,
}
}
2017-04-20 09:00:15 +00:00
func NewMergingWriter(writer io.Writer) Writer {
return NewMergingWriterSize(writer, 4096)
}
func NewMergingWriterSize(writer io.Writer, size uint32) Writer {
return &mergingWriter{
writer: writer,
buffer: make([]byte, size),
}
}
2017-04-21 12:51:09 +00:00
func NewSequentialWriter(writer io.Writer) Writer {
return &seqWriter{
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,
}
}