2015-09-13 14:01:50 -04:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2015-09-13 18:30:50 -04:00
|
|
|
_ "github.com/v2ray/v2ray-core/log"
|
2015-09-13 14:01:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-09-13 18:30:50 -04:00
|
|
|
bufferSize = 32 * 1024
|
2015-09-13 14:01:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func ReaderToChan(stream chan<- []byte, reader io.Reader) error {
|
|
|
|
for {
|
|
|
|
buffer := make([]byte, bufferSize)
|
|
|
|
nBytes, err := reader.Read(buffer)
|
2015-09-13 18:56:49 -04:00
|
|
|
if nBytes > 0 {
|
|
|
|
stream <- buffer[:nBytes]
|
|
|
|
}
|
2015-09-13 14:01:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChanToWriter(writer io.Writer, stream <-chan []byte) error {
|
|
|
|
for buffer := range stream {
|
2015-09-13 18:30:50 -04:00
|
|
|
_, err := writer.Write(buffer)
|
2015-09-13 14:01:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|