Skip to content

Commit 9561858

Browse files
committed
core/cp: impl Stream()
1 parent fc43536 commit 9561858

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

intra/core/cp.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import (
1111
"io"
1212
)
1313

14-
var errNoPipe = errors.New("src or dst nil")
14+
var (
15+
errNoPipe = errors.New("pipe: src or dst nil")
16+
errNoStream = errors.New("stream: reader or writer nil")
17+
)
1518

1619
// Pipe copies data from src to dst, and returns the number of bytes copied.
1720
// Prefers src.WriteTo(dst) and dst.ReadFrom(src) if available.
@@ -28,6 +31,16 @@ func Pipe(dst io.Writer, src io.Reader) (int64, error) {
2831
} else if x, ok := dst.(io.ReaderFrom); ok {
2932
return x.ReadFrom(src)
3033
}
34+
return Stream(dst, src)
35+
}
36+
37+
// Stream reads data from src in to dst until error, and returns the no. of bytes read.
38+
// Internally, it uses io.CopyBuffer, recycling buffers from global pool.
39+
func Stream(dst io.Writer, src io.Reader) (int64, error) {
40+
if IsNil(src) || IsNil(dst) {
41+
return 0, errNoStream
42+
}
43+
3144
bptr := Alloc()
3245
b := *bptr
3346
b = b[:cap(b)]

0 commit comments

Comments
 (0)