v2ray-core/common/net/transport.go

35 lines
677 B
Go
Raw Normal View History

2015-09-13 18:01:50 +00:00
package net
import (
"io"
)
const (
bufferSize = 4 * 1024
2015-09-13 18:01:50 +00:00
)
2015-09-21 10:15:25 +00:00
// ReaderToChan dumps all content from a given reader to a chan by constantly reading it until EOF.
2015-09-13 18:01:50 +00:00
func ReaderToChan(stream chan<- []byte, reader io.Reader) error {
for {
2015-09-18 08:14:22 +00:00
buffer := make([]byte, bufferSize)
2015-09-13 18:01:50 +00:00
nBytes, err := reader.Read(buffer)
2015-09-14 16:19:17 +00:00
if nBytes > 0 {
stream <- buffer[:nBytes]
}
2015-09-13 18:01:50 +00:00
if err != nil {
return err
}
}
}
2015-09-21 10:15:25 +00:00
// ChanToWriter dumps all content from a given chan to a writer until the chan is closed.
2015-09-13 18:01:50 +00:00
func ChanToWriter(writer io.Writer, stream <-chan []byte) error {
for buffer := range stream {
2015-09-13 22:30:50 +00:00
_, err := writer.Write(buffer)
2015-09-13 18:01:50 +00:00
if err != nil {
return err
}
}
return nil
}