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