Commit c94eddd1 authored by Jeremy Schlatter's avatar Jeremy Schlatter Committed by Brad Fitzpatrick

io: Simplify CopyN implementation by delegating to Copy.

R=golang-dev, dave, minux.ma, bradfitz, rsc
CC=golang-dev
https://golang.org/cl/7312055
parent abbe1111
...@@ -299,43 +299,12 @@ func ReadFull(r Reader, buf []byte) (n int, err error) { ...@@ -299,43 +299,12 @@ func ReadFull(r Reader, buf []byte) (n int, err error) {
// If dst implements the ReaderFrom interface, // If dst implements the ReaderFrom interface,
// the copy is implemented using it. // the copy is implemented using it.
func CopyN(dst Writer, src Reader, n int64) (written int64, err error) { func CopyN(dst Writer, src Reader, n int64) (written int64, err error) {
// If the writer has a ReadFrom method, use it to do the copy. written, err = Copy(dst, LimitReader(src, n))
// Avoids a buffer allocation and a copy. if written < n && err == nil {
if rt, ok := dst.(ReaderFrom); ok { // src stopped early; must have been EOF.
written, err = rt.ReadFrom(LimitReader(src, n)) err = EOF
if written < n && err == nil {
// rt stopped early; must have been EOF.
err = EOF
}
return
} }
buf := make([]byte, 32*1024) return
for written < n {
l := len(buf)
if d := n - written; d < int64(l) {
l = int(d)
}
nr, er := src.Read(buf[0:l])
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = ErrShortWrite
break
}
}
if er != nil {
err = er
break
}
}
return written, err
} }
// Copy copies from src to dst until either EOF is reached // Copy copies from src to dst until either EOF is reached
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment