mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
500 B
34 lines
500 B
9 years ago
|
package net
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
const (
|
||
9 years ago
|
bufferSize = 32 * 1024
|
||
9 years ago
|
)
|
||
|
|
||
|
func ReaderToChan(stream chan<- []byte, reader io.Reader) error {
|
||
|
for {
|
||
9 years ago
|
buffer := make([]byte, bufferSize)
|
||
9 years ago
|
nBytes, err := reader.Read(buffer)
|
||
9 years ago
|
if nBytes > 0 {
|
||
|
stream <- buffer[:nBytes]
|
||
|
}
|
||
9 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func ChanToWriter(writer io.Writer, stream <-chan []byte) error {
|
||
|
for buffer := range stream {
|
||
9 years ago
|
_, err := writer.Write(buffer)
|
||
9 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|