copyWithBuffer is very similar to io.CopyBuffer https://golang.org/pkg/io/#CopyBuffer but instead of using Read to read from the src, we use ReadAtLeast to make sure we have a full buffer before we do a write operation to dst to reduce overheads associated with the write operations of small buffers
(dst io.Writer, src io.Reader)
| 319 | // a full buffer before we do a write operation to dst to reduce overheads associated |
| 320 | // with the write operations of small buffers. |
| 321 | func copyWithBuffer(dst io.Writer, src io.Reader) (written int64, err error) { |
| 322 | // If the reader has a WriteTo method, use it to do the copy. |
| 323 | // Avoids an allocation and a copy. |
| 324 | if wt, ok := src.(io.WriterTo); ok { |
| 325 | return wt.WriteTo(dst) |
| 326 | } |
| 327 | // Similarly, if the writer has a ReadFrom method, use it to do the copy. |
| 328 | if rt, ok := dst.(io.ReaderFrom); ok { |
| 329 | return rt.ReadFrom(src) |
| 330 | } |
| 331 | bufRef := bufPool.Get().(*[]byte) |
| 332 | defer bufPool.Put(bufRef) |
| 333 | buf := *bufRef |
| 334 | for { |
| 335 | nr, er := io.ReadAtLeast(src, buf, len(buf)) |
| 336 | if nr > 0 { |
| 337 | nw, ew := dst.Write(buf[0:nr]) |
| 338 | if nw > 0 { |
| 339 | written += int64(nw) |
| 340 | } |
| 341 | if ew != nil { |
| 342 | err = ew |
| 343 | break |
| 344 | } |
| 345 | if nr != nw { |
| 346 | err = io.ErrShortWrite |
| 347 | break |
| 348 | } |
| 349 | } |
| 350 | if er != nil { |
| 351 | // If an EOF happens after reading fewer than the requested bytes, |
| 352 | // ReadAtLeast returns ErrUnexpectedEOF. |
| 353 | if er != io.EOF && er != io.ErrUnexpectedEOF { |
| 354 | err = er |
| 355 | } |
| 356 | break |
| 357 | } |
| 358 | } |
| 359 | return |
| 360 | } |
| 361 | |
| 362 | // Exists returns whether an attempt to access the content would not error out |
| 363 | // with an ErrNotFound error. It will return an encountered error if it was |
no test coverage detected
searching dependent graphs…