Skip to content

Commit b81917e

Browse files
committed
Add comments clarifying copyWithBuffer implementation
Signed-off-by: Amr Mahdi <[email protected]>
1 parent f6834d4 commit b81917e

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

content/helpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ func seekReader(r io.Reader, offset, size int64) (io.Reader, error) {
229229
return r, nil
230230
}
231231

232+
// copyWithBuffer is very similar to io.CopyBuffer https://golang.org/pkg/io/#CopyBuffer
233+
// but instead of using Read to read from the src, we use ReadAtLeast to make sure we have
234+
// a full buffer before we do a write operation to dst to reduce overheads associated
235+
// with the write operations of small buffers.
232236
func copyWithBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
233237
// If the reader has a WriteTo method, use it to do the copy.
234238
// Avoids an allocation and a copy.
@@ -259,6 +263,8 @@ func copyWithBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
259263
}
260264
}
261265
if er != nil {
266+
// If an EOF happens after reading fewer than the requested bytes,
267+
// ReadAtLeast returns ErrUnexpectedEOF.
262268
if er != io.EOF && er != io.ErrUnexpectedEOF {
263269
err = er
264270
}

0 commit comments

Comments
 (0)