Commit 79d94d50 authored by Robert Griesemer's avatar Robert Griesemer

Copyn

R=rsc
DELTA=34  (34 added, 0 deleted, 0 changed)
OCL=19541
CL=19545
parent 23bd1298
......@@ -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
}
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