1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-07 02:38:10 -05:00
v2fly/common/io/transport.go

44 lines
967 B
Go
Raw Normal View History

2016-01-29 08:39:55 -05: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 06:22:29 -05:00
if buffer.Len() > 0 {
2016-01-29 08:39:55 -05:00
stream <- buffer
} else {
2016-02-01 06:22:29 -05:00
buffer.Release()
2016-01-29 08:39:55 -05:00
}
if err != nil {
return err
}
}
}
2016-02-01 06:22:29 -05:00
func ChanToRawWriter(writer io.Writer, stream <-chan *alloc.Buffer) error {
return ChanToWriter(NewAdaptiveWriter(writer), stream)
}
2016-01-29 08:39:55 -05:00
// ChanToWriter dumps all content from a given chan to a writer until the chan is closed.
2016-02-01 06:22:29 -05:00
func ChanToWriter(writer Writer, stream <-chan *alloc.Buffer) error {
2016-01-29 08:39:55 -05:00
for buffer := range stream {
2016-02-01 06:22:29 -05:00
err := writer.Write(buffer)
2016-01-29 08:39:55 -05:00
buffer.Release()
if err != nil {
return err
}
}
return nil
}