~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/io/io.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
// middle of reading a fixed-size block or data structure.
35
35
var ErrUnexpectedEOF = errors.New("unexpected EOF")
36
36
 
 
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")
 
41
 
37
42
// Reader is the interface that wraps the basic Read method.
38
43
//
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.
 
63
//
 
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)
60
69
}
70
79
}
71
80
 
72
81
// Closer is the interface that wraps the basic Close method.
 
82
//
 
83
// The behavior of Close after the first call is undefined.
 
84
// Specific implementations may document their own behavior.
73
85
type Closer interface {
74
86
        Close() error
75
87
}
130
142
}
131
143
 
132
144
// ReaderFrom is the interface that wraps the ReadFrom method.
 
145
//
 
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.
 
149
//
 
150
// The Copy function uses ReaderFrom if available.
133
151
type ReaderFrom interface {
134
152
        ReadFrom(r Reader) (n int64, err error)
135
153
}
136
154
 
137
155
// WriterTo is the interface that wraps the WriteTo method.
 
156
//
 
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.
 
160
//
 
161
// The Copy function uses WriterTo if available.
138
162
type WriterTo interface {
139
163
        WriteTo(w Writer) (n int64, err error)
140
164
}
204
228
        UnreadByte() error
205
229
}
206
230
 
 
231
// ByteWriter is the interface that wraps the WriteByte method.
 
232
type ByteWriter interface {
 
233
        WriteByte(c byte) error
 
234
}
 
235
 
207
236
// RuneReader is the interface that wraps the ReadRune method.
208
237
//
209
238
// ReadRune reads a single UTF-8 encoded Unicode character
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
254
284
                nn, err = r.Read(buf[n:])
255
285
                n += nn
256
286
        }
257
 
        if err == EOF {
258
 
                if n >= min {
259
 
                        err = nil
260
 
                } else if n > 0 {
261
 
                        err = ErrUnexpectedEOF
262
 
                }
 
287
        if n >= min {
 
288
                err = nil
 
289
        } else if n > 0 && err == EOF {
 
290
                err = ErrUnexpectedEOF
263
291
        }
264
292
        return
265
293
}
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))
274
303
}
275
304
 
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.
281
309
//
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.
291
 
                        err = EOF
292
 
                }
293
 
                return
294
 
        }
295
 
        buf := make([]byte, 32*1024)
296
 
        for written < n {
297
 
                l := len(buf)
298
 
                if d := n - written; d < int64(l) {
299
 
                        l = int(d)
300
 
                }
301
 
                nr, er := src.Read(buf[0:l])
302
 
                if nr > 0 {
303
 
                        nw, ew := dst.Write(buf[0:nr])
304
 
                        if nw > 0 {
305
 
                                written += int64(nw)
306
 
                        }
307
 
                        if ew != nil {
308
 
                                err = ew
309
 
                                break
310
 
                        }
311
 
                        if nr != nw {
312
 
                                err = ErrShortWrite
313
 
                                break
314
 
                        }
315
 
                }
316
 
                if er != nil {
317
 
                        err = er
318
 
                        break
319
 
                }
320
 
        }
321
 
        return written, err
 
313
        written, err = Copy(dst, LimitReader(src, n))
 
314
        if written == n {
 
315
                return n, nil
 
316
        }
 
317
        if written < n && err == nil {
 
318
                // src stopped early; must have been EOF.
 
319
                err = EOF
 
320
        }
 
321
        return
322
322
}
323
323
 
324
324
// Copy copies from src to dst until either EOF is reached
451
451
        off += s.base
452
452
        if max := s.limit - off; int64(len(p)) > max {
453
453
                p = p[0:max]
 
454
                n, err = s.r.ReadAt(p, off)
 
455
                if err == nil {
 
456
                        err = EOF
 
457
                }
 
458
                return n, err
454
459
        }
455
460
        return s.r.ReadAt(p, off)
456
461
}