github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/io/io.go (about)

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