From 79d94d504f8f3e82e994a4f63d37f56cebc6e7cc Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 18 Nov 2008 18:08:05 -0800 Subject: [PATCH] Copyn R=rsc DELTA=34 (34 added, 0 deleted, 0 changed) OCL=19541 CL=19545 --- src/lib/io.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/lib/io.go b/src/lib/io.go index 80f753a4bf..699f9196c3 100644 --- a/src/lib/io.go +++ b/src/lib/io.go @@ -76,3 +76,37 @@ export func MakeFullReader(fd Read) Read { } return &FullRead{fd} } + +// Copies n bytes (or until EOF is reached) from src to dst. +// Returns the number of bytes copied and the error, if any. +export func Copyn(src Read, dst Write, n int) (c int, err *os.Error) { + buf := new([]byte, 32*1024); // BUG 6g crashes on non-pointer array slices + c = 0; + for c < n { + l := n - c; + if l > len(buf) { + l = len(buf) + } + nr, er := src.Read(buf[0 : l]); + if nr > 0 { + nw, ew := dst.Write(buf[0 : nr]); + if nw != nr || ew != nil { + c += nw; + if ew == nil { + ew = os.EIO + } + err = ew; + break; + } + c += nr; + } + if er != nil { + err = er; + break; + } + if nr == 0 { + break; + } + } + return c, err +}