v2ray-core/common/io/transport.go

44 lines
967 B
Go
Raw Normal View History

2016-01-29 13:39:55 +00:00
package io
import (
"io"
"github.com/v2ray/v2ray-core/common/alloc"
)
func RawReaderToChan(stream chan<- *alloc.Buffer, reader io.Reader) error {
return ReaderToChan(stream, NewAdaptiveReader(reader))
}
// ReaderToChan dumps all content from a given reader to a chan by constantly reading it until EOF.
func ReaderToChan(stream chan<- *alloc.Buffer, reader Reader) error {
for {
buffer, err := reader.Read()
2016-02-01 11:22:29 +00:00
if buffer.Len() > 0 {
2016-01-29 13:39:55 +00:00
stream <- buffer
} else {
2016-02-01 11:22:29 +00:00
buffer.Release()
2016-01-29 13:39:55 +00:00
}
if err != nil {
return err
}
}
}
2016-02-01 11:22:29 +00:00
func ChanToRawWriter(writer io.Writer, stream <-chan *alloc.Buffer) error {
return ChanToWriter(NewAdaptiveWriter(writer), stream)
}
2016-01-29 13:39:55 +00:00
// ChanToWriter dumps all content from a given chan to a writer until the chan is closed.
2016-02-01 11:22:29 +00:00
func ChanToWriter(writer Writer, stream <-chan *alloc.Buffer) error {
2016-01-29 13:39:55 +00:00
for buffer := range stream {
2016-02-01 11:22:29 +00:00
err := writer.Write(buffer)
2016-01-29 13:39:55 +00:00
buffer.Release()
if err != nil {
return err
}
}
return nil
}