1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
// Package io provides basic interfaces to I/O primitives.
|
6 |
|
|
// Its primary job is to wrap existing implementations of such primitives,
|
7 |
|
|
// such as those in package os, into shared public interfaces that
|
8 |
|
|
// abstract the functionality, plus some other related primitives.
|
9 |
|
|
package io
|
10 |
|
|
|
11 |
|
|
import (
|
12 |
|
|
"errors"
|
13 |
|
|
)
|
14 |
|
|
|
15 |
|
|
// ErrShortWrite means that a write accepted fewer bytes than requested
|
16 |
|
|
// but failed to return an explicit error.
|
17 |
|
|
var ErrShortWrite = errors.New("short write")
|
18 |
|
|
|
19 |
|
|
// ErrShortBuffer means that a read required a longer buffer than was provided.
|
20 |
|
|
var ErrShortBuffer = errors.New("short buffer")
|
21 |
|
|
|
22 |
|
|
// EOF is the error returned by Read when no more input is available.
|
23 |
|
|
// Functions should return EOF only to signal a graceful end of input.
|
24 |
|
|
// If the EOF occurs unexpectedly in a structured data stream,
|
25 |
|
|
// the appropriate error is either ErrUnexpectedEOF or some other error
|
26 |
|
|
// giving more detail.
|
27 |
|
|
var EOF = errors.New("EOF")
|
28 |
|
|
|
29 |
|
|
// ErrUnexpectedEOF means that EOF was encountered in the
|
30 |
|
|
// middle of reading a fixed-size block or data structure.
|
31 |
|
|
var ErrUnexpectedEOF = errors.New("unexpected EOF")
|
32 |
|
|
|
33 |
|
|
// Reader is the interface that wraps the basic Read method.
|
34 |
|
|
//
|
35 |
|
|
// Read reads up to len(p) bytes into p. It returns the number of bytes
|
36 |
|
|
// read (0 <= n <= len(p)) and any error encountered. Even if Read
|
37 |
|
|
// returns n < len(p), it may use all of p as scratch space during the call.
|
38 |
|
|
// If some data is available but not len(p) bytes, Read conventionally
|
39 |
|
|
// returns what is available instead of waiting for more.
|
40 |
|
|
//
|
41 |
|
|
// When Read encounters an error or end-of-file condition after
|
42 |
|
|
// successfully reading n > 0 bytes, it returns the number of
|
43 |
|
|
// bytes read. It may return the (non-nil) error from the same call
|
44 |
|
|
// or return the error (and n == 0) from a subsequent call.
|
45 |
|
|
// An instance of this general case is that a Reader returning
|
46 |
|
|
// a non-zero number of bytes at the end of the input stream may
|
47 |
|
|
// return either err == EOF or err == nil. The next Read should
|
48 |
|
|
// return 0, EOF regardless.
|
49 |
|
|
//
|
50 |
|
|
// Callers should always process the n > 0 bytes returned before
|
51 |
|
|
// considering the error err. Doing so correctly handles I/O errors
|
52 |
|
|
// that happen after reading some bytes and also both of the
|
53 |
|
|
// allowed EOF behaviors.
|
54 |
|
|
type Reader interface {
|
55 |
|
|
Read(p []byte) (n int, err error)
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
// Writer is the interface that wraps the basic Write method.
|
59 |
|
|
//
|
60 |
|
|
// Write writes len(p) bytes from p to the underlying data stream.
|
61 |
|
|
// It returns the number of bytes written from p (0 <= n <= len(p))
|
62 |
|
|
// and any error encountered that caused the write to stop early.
|
63 |
|
|
// Write must return a non-nil error if it returns n < len(p).
|
64 |
|
|
type Writer interface {
|
65 |
|
|
Write(p []byte) (n int, err error)
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
// Closer is the interface that wraps the basic Close method.
|
69 |
|
|
type Closer interface {
|
70 |
|
|
Close() error
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
// Seeker is the interface that wraps the basic Seek method.
|
74 |
|
|
//
|
75 |
|
|
// Seek sets the offset for the next Read or Write to offset,
|
76 |
|
|
// interpreted according to whence: 0 means relative to the origin of
|
77 |
|
|
// the file, 1 means relative to the current offset, and 2 means
|
78 |
|
|
// relative to the end. Seek returns the new offset and an Error, if
|
79 |
|
|
// any.
|
80 |
|
|
type Seeker interface {
|
81 |
|
|
Seek(offset int64, whence int) (ret int64, err error)
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
// ReadWriter is the interface that groups the basic Read and Write methods.
|
85 |
|
|
type ReadWriter interface {
|
86 |
|
|
Reader
|
87 |
|
|
Writer
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
// ReadCloser is the interface that groups the basic Read and Close methods.
|
91 |
|
|
type ReadCloser interface {
|
92 |
|
|
Reader
|
93 |
|
|
Closer
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
// WriteCloser is the interface that groups the basic Write and Close methods.
|
97 |
|
|
type WriteCloser interface {
|
98 |
|
|
Writer
|
99 |
|
|
Closer
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
// ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
|
103 |
|
|
type ReadWriteCloser interface {
|
104 |
|
|
Reader
|
105 |
|
|
Writer
|
106 |
|
|
Closer
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
// ReadSeeker is the interface that groups the basic Read and Seek methods.
|
110 |
|
|
type ReadSeeker interface {
|
111 |
|
|
Reader
|
112 |
|
|
Seeker
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
// WriteSeeker is the interface that groups the basic Write and Seek methods.
|
116 |
|
|
type WriteSeeker interface {
|
117 |
|
|
Writer
|
118 |
|
|
Seeker
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
// ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods.
|
122 |
|
|
type ReadWriteSeeker interface {
|
123 |
|
|
Reader
|
124 |
|
|
Writer
|
125 |
|
|
Seeker
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
// ReaderFrom is the interface that wraps the ReadFrom method.
|
129 |
|
|
type ReaderFrom interface {
|
130 |
|
|
ReadFrom(r Reader) (n int64, err error)
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
// WriterTo is the interface that wraps the WriteTo method.
|
134 |
|
|
type WriterTo interface {
|
135 |
|
|
WriteTo(w Writer) (n int64, err error)
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
// ReaderAt is the interface that wraps the basic ReadAt method.
|
139 |
|
|
//
|
140 |
|
|
// ReadAt reads len(p) bytes into p starting at offset off in the
|
141 |
|
|
// underlying input source. It returns the number of bytes
|
142 |
|
|
// read (0 <= n <= len(p)) and any error encountered.
|
143 |
|
|
//
|
144 |
|
|
// When ReadAt returns n < len(p), it returns a non-nil error
|
145 |
|
|
// explaining why more bytes were not returned. In this respect,
|
146 |
|
|
// ReadAt is stricter than Read.
|
147 |
|
|
//
|
148 |
|
|
// Even if ReadAt returns n < len(p), it may use all of p as scratch
|
149 |
|
|
// space during the call. If some data is available but not len(p) bytes,
|
150 |
|
|
// ReadAt blocks until either all the data is available or an error occurs.
|
151 |
|
|
// In this respect ReadAt is different from Read.
|
152 |
|
|
//
|
153 |
|
|
// If the n = len(p) bytes returned by ReadAt are at the end of the
|
154 |
|
|
// input source, ReadAt may return either err == EOF or err == nil.
|
155 |
|
|
//
|
156 |
|
|
// If ReadAt is reading from an input source with a seek offset,
|
157 |
|
|
// ReadAt should not affect nor be affected by the underlying
|
158 |
|
|
// seek offset.
|
159 |
|
|
type ReaderAt interface {
|
160 |
|
|
ReadAt(p []byte, off int64) (n int, err error)
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
// WriterAt is the interface that wraps the basic WriteAt method.
|
164 |
|
|
//
|
165 |
|
|
// WriteAt writes len(p) bytes from p to the underlying data stream
|
166 |
|
|
// at offset off. It returns the number of bytes written from p (0 <= n <= len(p))
|
167 |
|
|
// and any error encountered that caused the write to stop early.
|
168 |
|
|
// WriteAt must return a non-nil error if it returns n < len(p).
|
169 |
|
|
type WriterAt interface {
|
170 |
|
|
WriteAt(p []byte, off int64) (n int, err error)
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
// ByteReader is the interface that wraps the ReadByte method.
|
174 |
|
|
//
|
175 |
|
|
// ReadByte reads and returns the next byte from the input.
|
176 |
|
|
// If no byte is available, err will be set.
|
177 |
|
|
type ByteReader interface {
|
178 |
|
|
ReadByte() (c byte, err error)
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
// ByteScanner is the interface that adds the UnreadByte method to the
|
182 |
|
|
// basic ReadByte method.
|
183 |
|
|
//
|
184 |
|
|
// UnreadByte causes the next call to ReadByte to return the same byte
|
185 |
|
|
// as the previous call to ReadByte.
|
186 |
|
|
// It may be an error to call UnreadByte twice without an intervening
|
187 |
|
|
// call to ReadByte.
|
188 |
|
|
type ByteScanner interface {
|
189 |
|
|
ByteReader
|
190 |
|
|
UnreadByte() error
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
// RuneReader is the interface that wraps the ReadRune method.
|
194 |
|
|
//
|
195 |
|
|
// ReadRune reads a single UTF-8 encoded Unicode character
|
196 |
|
|
// and returns the rune and its size in bytes. If no character is
|
197 |
|
|
// available, err will be set.
|
198 |
|
|
type RuneReader interface {
|
199 |
|
|
ReadRune() (r rune, size int, err error)
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
// RuneScanner is the interface that adds the UnreadRune method to the
|
203 |
|
|
// basic ReadRune method.
|
204 |
|
|
//
|
205 |
|
|
// UnreadRune causes the next call to ReadRune to return the same rune
|
206 |
|
|
// as the previous call to ReadRune.
|
207 |
|
|
// It may be an error to call UnreadRune twice without an intervening
|
208 |
|
|
// call to ReadRune.
|
209 |
|
|
type RuneScanner interface {
|
210 |
|
|
RuneReader
|
211 |
|
|
UnreadRune() error
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
// stringWriter is the interface that wraps the WriteString method.
|
215 |
|
|
type stringWriter interface {
|
216 |
|
|
WriteString(s string) (n int, err error)
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
// WriteString writes the contents of the string s to w, which accepts an array of bytes.
|
220 |
|
|
// If w already implements a WriteString method, it is invoked directly.
|
221 |
|
|
func WriteString(w Writer, s string) (n int, err error) {
|
222 |
|
|
if sw, ok := w.(stringWriter); ok {
|
223 |
|
|
return sw.WriteString(s)
|
224 |
|
|
}
|
225 |
|
|
return w.Write([]byte(s))
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
// ReadAtLeast reads from r into buf until it has read at least min bytes.
|
229 |
|
|
// It returns the number of bytes copied and an error if fewer bytes were read.
|
230 |
|
|
// The error is EOF only if no bytes were read.
|
231 |
|
|
// If an EOF happens after reading fewer than min bytes,
|
232 |
|
|
// ReadAtLeast returns ErrUnexpectedEOF.
|
233 |
|
|
// If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer.
|
234 |
|
|
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error) {
|
235 |
|
|
if len(buf) < min {
|
236 |
|
|
return 0, ErrShortBuffer
|
237 |
|
|
}
|
238 |
|
|
for n < min && err == nil {
|
239 |
|
|
var nn int
|
240 |
|
|
nn, err = r.Read(buf[n:])
|
241 |
|
|
n += nn
|
242 |
|
|
}
|
243 |
|
|
if err == EOF {
|
244 |
|
|
if n >= min {
|
245 |
|
|
err = nil
|
246 |
|
|
} else if n > 0 {
|
247 |
|
|
err = ErrUnexpectedEOF
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
|
|
return
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
// ReadFull reads exactly len(buf) bytes from r into buf.
|
254 |
|
|
// It returns the number of bytes copied and an error if fewer bytes were read.
|
255 |
|
|
// The error is EOF only if no bytes were read.
|
256 |
|
|
// If an EOF happens after reading some but not all the bytes,
|
257 |
|
|
// ReadFull returns ErrUnexpectedEOF.
|
258 |
|
|
func ReadFull(r Reader, buf []byte) (n int, err error) {
|
259 |
|
|
return ReadAtLeast(r, buf, len(buf))
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
// CopyN copies n bytes (or until an error) from src to dst.
|
263 |
|
|
// It returns the number of bytes copied and the earliest
|
264 |
|
|
// error encountered while copying. Because Read can
|
265 |
|
|
// return the full amount requested as well as an error
|
266 |
|
|
// (including EOF), so can CopyN.
|
267 |
|
|
//
|
268 |
|
|
// If dst implements the ReaderFrom interface,
|
269 |
|
|
// the copy is implemented using it.
|
270 |
|
|
func CopyN(dst Writer, src Reader, n int64) (written int64, err error) {
|
271 |
|
|
// If the writer has a ReadFrom method, use it to do the copy.
|
272 |
|
|
// Avoids a buffer allocation and a copy.
|
273 |
|
|
if rt, ok := dst.(ReaderFrom); ok {
|
274 |
|
|
written, err = rt.ReadFrom(LimitReader(src, n))
|
275 |
|
|
if written < n && err == nil {
|
276 |
|
|
// rt stopped early; must have been EOF.
|
277 |
|
|
err = EOF
|
278 |
|
|
}
|
279 |
|
|
return
|
280 |
|
|
}
|
281 |
|
|
buf := make([]byte, 32*1024)
|
282 |
|
|
for written < n {
|
283 |
|
|
l := len(buf)
|
284 |
|
|
if d := n - written; d < int64(l) {
|
285 |
|
|
l = int(d)
|
286 |
|
|
}
|
287 |
|
|
nr, er := src.Read(buf[0:l])
|
288 |
|
|
if nr > 0 {
|
289 |
|
|
nw, ew := dst.Write(buf[0:nr])
|
290 |
|
|
if nw > 0 {
|
291 |
|
|
written += int64(nw)
|
292 |
|
|
}
|
293 |
|
|
if ew != nil {
|
294 |
|
|
err = ew
|
295 |
|
|
break
|
296 |
|
|
}
|
297 |
|
|
if nr != nw {
|
298 |
|
|
err = ErrShortWrite
|
299 |
|
|
break
|
300 |
|
|
}
|
301 |
|
|
}
|
302 |
|
|
if er != nil {
|
303 |
|
|
err = er
|
304 |
|
|
break
|
305 |
|
|
}
|
306 |
|
|
}
|
307 |
|
|
return written, err
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
// Copy copies from src to dst until either EOF is reached
|
311 |
|
|
// on src or an error occurs. It returns the number of bytes
|
312 |
|
|
// copied and the first error encountered while copying, if any.
|
313 |
|
|
//
|
314 |
|
|
// A successful Copy returns err == nil, not err == EOF.
|
315 |
|
|
// Because Copy is defined to read from src until EOF, it does
|
316 |
|
|
// not treat an EOF from Read as an error to be reported.
|
317 |
|
|
//
|
318 |
|
|
// If dst implements the ReaderFrom interface,
|
319 |
|
|
// the copy is implemented by calling dst.ReadFrom(src).
|
320 |
|
|
// Otherwise, if src implements the WriterTo interface,
|
321 |
|
|
// the copy is implemented by calling src.WriteTo(dst).
|
322 |
|
|
func Copy(dst Writer, src Reader) (written int64, err error) {
|
323 |
|
|
// If the writer has a ReadFrom method, use it to do the copy.
|
324 |
|
|
// Avoids an allocation and a copy.
|
325 |
|
|
if rt, ok := dst.(ReaderFrom); ok {
|
326 |
|
|
return rt.ReadFrom(src)
|
327 |
|
|
}
|
328 |
|
|
// Similarly, if the reader has a WriteTo method, use it to do the copy.
|
329 |
|
|
if wt, ok := src.(WriterTo); ok {
|
330 |
|
|
return wt.WriteTo(dst)
|
331 |
|
|
}
|
332 |
|
|
buf := make([]byte, 32*1024)
|
333 |
|
|
for {
|
334 |
|
|
nr, er := src.Read(buf)
|
335 |
|
|
if nr > 0 {
|
336 |
|
|
nw, ew := dst.Write(buf[0:nr])
|
337 |
|
|
if nw > 0 {
|
338 |
|
|
written += int64(nw)
|
339 |
|
|
}
|
340 |
|
|
if ew != nil {
|
341 |
|
|
err = ew
|
342 |
|
|
break
|
343 |
|
|
}
|
344 |
|
|
if nr != nw {
|
345 |
|
|
err = ErrShortWrite
|
346 |
|
|
break
|
347 |
|
|
}
|
348 |
|
|
}
|
349 |
|
|
if er == EOF {
|
350 |
|
|
break
|
351 |
|
|
}
|
352 |
|
|
if er != nil {
|
353 |
|
|
err = er
|
354 |
|
|
break
|
355 |
|
|
}
|
356 |
|
|
}
|
357 |
|
|
return written, err
|
358 |
|
|
}
|
359 |
|
|
|
360 |
|
|
// LimitReader returns a Reader that reads from r
|
361 |
|
|
// but stops with EOF after n bytes.
|
362 |
|
|
// The underlying implementation is a *LimitedReader.
|
363 |
|
|
func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n} }
|
364 |
|
|
|
365 |
|
|
// A LimitedReader reads from R but limits the amount of
|
366 |
|
|
// data returned to just N bytes. Each call to Read
|
367 |
|
|
// updates N to reflect the new amount remaining.
|
368 |
|
|
type LimitedReader struct {
|
369 |
|
|
R Reader // underlying reader
|
370 |
|
|
N int64 // max bytes remaining
|
371 |
|
|
}
|
372 |
|
|
|
373 |
|
|
func (l *LimitedReader) Read(p []byte) (n int, err error) {
|
374 |
|
|
if l.N <= 0 {
|
375 |
|
|
return 0, EOF
|
376 |
|
|
}
|
377 |
|
|
if int64(len(p)) > l.N {
|
378 |
|
|
p = p[0:l.N]
|
379 |
|
|
}
|
380 |
|
|
n, err = l.R.Read(p)
|
381 |
|
|
l.N -= int64(n)
|
382 |
|
|
return
|
383 |
|
|
}
|
384 |
|
|
|
385 |
|
|
// NewSectionReader returns a SectionReader that reads from r
|
386 |
|
|
// starting at offset off and stops with EOF after n bytes.
|
387 |
|
|
func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader {
|
388 |
|
|
return &SectionReader{r, off, off, off + n}
|
389 |
|
|
}
|
390 |
|
|
|
391 |
|
|
// SectionReader implements Read, Seek, and ReadAt on a section
|
392 |
|
|
// of an underlying ReaderAt.
|
393 |
|
|
type SectionReader struct {
|
394 |
|
|
r ReaderAt
|
395 |
|
|
base int64
|
396 |
|
|
off int64
|
397 |
|
|
limit int64
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
func (s *SectionReader) Read(p []byte) (n int, err error) {
|
401 |
|
|
if s.off >= s.limit {
|
402 |
|
|
return 0, EOF
|
403 |
|
|
}
|
404 |
|
|
if max := s.limit - s.off; int64(len(p)) > max {
|
405 |
|
|
p = p[0:max]
|
406 |
|
|
}
|
407 |
|
|
n, err = s.r.ReadAt(p, s.off)
|
408 |
|
|
s.off += int64(n)
|
409 |
|
|
return
|
410 |
|
|
}
|
411 |
|
|
|
412 |
|
|
var errWhence = errors.New("Seek: invalid whence")
|
413 |
|
|
var errOffset = errors.New("Seek: invalid offset")
|
414 |
|
|
|
415 |
|
|
func (s *SectionReader) Seek(offset int64, whence int) (ret int64, err error) {
|
416 |
|
|
switch whence {
|
417 |
|
|
default:
|
418 |
|
|
return 0, errWhence
|
419 |
|
|
case 0:
|
420 |
|
|
offset += s.base
|
421 |
|
|
case 1:
|
422 |
|
|
offset += s.off
|
423 |
|
|
case 2:
|
424 |
|
|
offset += s.limit
|
425 |
|
|
}
|
426 |
|
|
if offset < s.base || offset > s.limit {
|
427 |
|
|
return 0, errOffset
|
428 |
|
|
}
|
429 |
|
|
s.off = offset
|
430 |
|
|
return offset - s.base, nil
|
431 |
|
|
}
|
432 |
|
|
|
433 |
|
|
func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error) {
|
434 |
|
|
if off < 0 || off >= s.limit-s.base {
|
435 |
|
|
return 0, EOF
|
436 |
|
|
}
|
437 |
|
|
off += s.base
|
438 |
|
|
if max := s.limit - off; int64(len(p)) > max {
|
439 |
|
|
p = p[0:max]
|
440 |
|
|
}
|
441 |
|
|
return s.r.ReadAt(p, off)
|
442 |
|
|
}
|
443 |
|
|
|
444 |
|
|
// Size returns the size of the section in bytes.
|
445 |
|
|
func (s *SectionReader) Size() int64 { return s.limit - s.base }
|
446 |
|
|
|
447 |
|
|
// TeeReader returns a Reader that writes to w what it reads from r.
|
448 |
|
|
// All reads from r performed through it are matched with
|
449 |
|
|
// corresponding writes to w. There is no internal buffering -
|
450 |
|
|
// the write must complete before the read completes.
|
451 |
|
|
// Any error encountered while writing is reported as a read error.
|
452 |
|
|
func TeeReader(r Reader, w Writer) Reader {
|
453 |
|
|
return &teeReader{r, w}
|
454 |
|
|
}
|
455 |
|
|
|
456 |
|
|
type teeReader struct {
|
457 |
|
|
r Reader
|
458 |
|
|
w Writer
|
459 |
|
|
}
|
460 |
|
|
|
461 |
|
|
func (t *teeReader) Read(p []byte) (n int, err error) {
|
462 |
|
|
n, err = t.r.Read(p)
|
463 |
|
|
if n > 0 {
|
464 |
|
|
if n, err := t.w.Write(p[:n]); err != nil {
|
465 |
|
|
return n, err
|
466 |
|
|
}
|
467 |
|
|
}
|
468 |
|
|
return
|
469 |
|
|
}
|