mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-04 09:17:32 -05:00
35 lines
572 B
Go
35 lines
572 B
Go
|
package net
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
|
||
|
"github.com/v2ray/v2ray-core/log"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
bufferSize = 8192
|
||
|
)
|
||
|
|
||
|
func ReaderToChan(stream chan<- []byte, reader io.Reader) error {
|
||
|
for {
|
||
|
buffer := make([]byte, bufferSize)
|
||
|
nBytes, err := reader.Read(buffer)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
stream <- buffer[:nBytes]
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func ChanToWriter(writer io.Writer, stream <-chan []byte) error {
|
||
|
for buffer := range stream {
|
||
|
nBytes, err := writer.Write(buffer)
|
||
|
log.Debug("Writing %d bytes with error %v", nBytes, err)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|