mirror of
https://github.com/xtaci/kcptun.git
synced 2024-04-21 12:32:32 +00:00
25 lines
609 B
Go
25 lines
609 B
Go
package generic
|
|
|
|
import (
|
|
"io"
|
|
)
|
|
|
|
const bufSize = 4096
|
|
|
|
// Memory optimized io.Copy function specified for this library
|
|
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
|
|
// If the reader has a WriteTo method, use it to do the copy.
|
|
// Avoids an allocation and a copy.
|
|
if wt, ok := src.(io.WriterTo); ok {
|
|
return wt.WriteTo(dst)
|
|
}
|
|
// Similarly, if the writer has a ReadFrom method, use it to do the copy.
|
|
if rt, ok := dst.(io.ReaderFrom); ok {
|
|
return rt.ReadFrom(src)
|
|
}
|
|
|
|
// fallback to standard io.CopyBuffer
|
|
buf := make([]byte, bufSize)
|
|
return io.CopyBuffer(dst, src, buf)
|
|
}
|