34
34
// middle of reading a fixed-size block or data structure.
35
35
var ErrUnexpectedEOF = errors.New("unexpected EOF")
37
// ErrNoProgress is returned by some clients of an io.Reader when
38
// many calls to Read have failed to return any data or error,
39
// usually the sign of a broken io.Reader implementation.
40
var ErrNoProgress = errors.New("multiple Read calls return no data or error")
37
42
// Reader is the interface that wraps the basic Read method.
39
44
// Read reads up to len(p) bytes into p. It returns the number of bytes
55
60
// considering the error err. Doing so correctly handles I/O errors
56
61
// that happen after reading some bytes and also both of the
57
62
// allowed EOF behaviors.
64
// Implementations of Read are discouraged from returning a
65
// zero byte count with a nil error, and callers should treat
66
// that situation as a no-op.
58
67
type Reader interface {
59
68
Read(p []byte) (n int, err error)
132
144
// ReaderFrom is the interface that wraps the ReadFrom method.
146
// ReadFrom reads data from r until EOF or error.
147
// The return value n is the number of bytes read.
148
// Any error except io.EOF encountered during the read is also returned.
150
// The Copy function uses ReaderFrom if available.
133
151
type ReaderFrom interface {
134
152
ReadFrom(r Reader) (n int64, err error)
137
155
// WriterTo is the interface that wraps the WriteTo method.
157
// WriteTo writes data to w until there's no more data to write or
158
// when an error occurs. The return value n is the number of bytes
159
// written. Any error encountered during the write is also returned.
161
// The Copy function uses WriterTo if available.
138
162
type WriterTo interface {
139
163
WriteTo(w Writer) (n int64, err error)
245
274
// If an EOF happens after reading fewer than min bytes,
246
275
// ReadAtLeast returns ErrUnexpectedEOF.
247
276
// If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer.
277
// On return, n >= min if and only if err == nil.
248
278
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error) {
249
279
if len(buf) < min {
250
280
return 0, ErrShortBuffer
269
297
// The error is EOF only if no bytes were read.
270
298
// If an EOF happens after reading some but not all the bytes,
271
299
// ReadFull returns ErrUnexpectedEOF.
300
// On return, n == len(buf) if and only if err == nil.
272
301
func ReadFull(r Reader, buf []byte) (n int, err error) {
273
302
return ReadAtLeast(r, buf, len(buf))
276
305
// CopyN copies n bytes (or until an error) from src to dst.
277
306
// It returns the number of bytes copied and the earliest
278
// error encountered while copying. Because Read can
279
// return the full amount requested as well as an error
280
// (including EOF), so can CopyN.
307
// error encountered while copying.
308
// On return, written == n if and only if err == nil.
282
310
// If dst implements the ReaderFrom interface,
283
311
// the copy is implemented using it.
284
312
func CopyN(dst Writer, src Reader, n int64) (written int64, err error) {
285
// If the writer has a ReadFrom method, use it to do the copy.
286
// Avoids a buffer allocation and a copy.
287
if rt, ok := dst.(ReaderFrom); ok {
288
written, err = rt.ReadFrom(LimitReader(src, n))
289
if written < n && err == nil {
290
// rt stopped early; must have been EOF.
295
buf := make([]byte, 32*1024)
298
if d := n - written; d < int64(l) {
301
nr, er := src.Read(buf[0:l])
303
nw, ew := dst.Write(buf[0:nr])
313
written, err = Copy(dst, LimitReader(src, n))
317
if written < n && err == nil {
318
// src stopped early; must have been EOF.
324
324
// Copy copies from src to dst until either EOF is reached