diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/lazy_file_reader.go | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/source/lazy_file_reader.go b/source/lazy_file_reader.go index b11644bef..6e75055ad 100644 --- a/source/lazy_file_reader.go +++ b/source/lazy_file_reader.go @@ -1,4 +1,5 @@ // Copyright 2015 The Hugo Authors. All rights reserved. +// Portions Copyright 2009 The Go Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -129,15 +130,24 @@ func (l *LazyFileReader) WriteTo(w io.Writer) (n int64, err error) { return 0, nil } - // following code is taken from io.Copy in 'io/io.go' - buf := make([]byte, 32*1024) + return l.copyBuffer(w, f, nil) +} + +// copyBuffer is the actual implementation of Copy and CopyBuffer. +// If buf is nil, one is allocated. +// +// Most of this function is copied from the Go stdlib 'io/io.go'. +func (l *LazyFileReader) copyBuffer(dst io.Writer, src io.Reader, buf []byte) (written int64, err error) { + if buf == nil { + buf = make([]byte, 32*1024) + } for { - nr, er := f.Read(buf) + nr, er := src.Read(buf) if nr > 0 { - nw, ew := w.Write(buf[0:nr]) + nw, ew := dst.Write(buf[0:nr]) if nw > 0 { l.pos += int64(nw) - n += int64(nw) + written += int64(nw) } if ew != nil { err = ew @@ -156,5 +166,5 @@ func (l *LazyFileReader) WriteTo(w io.Writer) (n int64, err error) { break } } - return n, err + return written, err } |