MCPcopy
hub / github.com/containerd/containerd / copyWithBuffer

Function copyWithBuffer

core/content/helpers.go:321–360  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

319// a full buffer before we do a write operation to dst to reduce overheads associated
320// with the write operations of small buffers.
321func 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

Callers 4

CopyFunction · 0.85
CopyReaderAtFunction · 0.85
CopyReaderFunction · 0.85
seekReaderFunction · 0.85

Calls 4

PutMethod · 0.80
WriteToMethod · 0.65
GetMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…