From c94eddd1afcd5c63744745605e5a4246273fe3d9 Mon Sep 17 00:00:00 2001 From: Jeremy Schlatter Date: Thu, 7 Feb 2013 20:26:12 -0800 Subject: [PATCH] io: Simplify CopyN implementation by delegating to Copy. R=golang-dev, dave, minux.ma, bradfitz, rsc CC=golang-dev https://golang.org/cl/7312055 --- src/pkg/io/io.go | 41 +++++------------------------------------ 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/src/pkg/io/io.go b/src/pkg/io/io.go index 5b24f062efc..f198448344b 100644 --- a/src/pkg/io/io.go +++ b/src/pkg/io/io.go @@ -299,43 +299,12 @@ func ReadFull(r Reader, buf []byte) (n int, err error) { // If dst implements the ReaderFrom interface, // the copy is implemented using it. 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. - // Avoids a buffer allocation and a copy. - if rt, ok := dst.(ReaderFrom); ok { - written, err = rt.ReadFrom(LimitReader(src, n)) - if written < n && err == nil { - // rt stopped early; must have been EOF. - err = EOF - } - return + written, err = Copy(dst, LimitReader(src, n)) + if written < n && err == nil { + // src stopped early; must have been EOF. + err = EOF } - buf := make([]byte, 32*1024) - 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 + return } // Copy copies from src to dst until either EOF is reached