Source file src/bufio/bufio.go

Documentation: bufio

     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 bufio implements buffered I/O. It wraps an io.Reader or io.Writer
     6  // object, creating another object (Reader or Writer) that also implements
     7  // the interface but provides buffering and some help for textual I/O.
     8  package bufio
     9  
    10  import (
    11  	"bytes"
    12  	"errors"
    13  	"io"
    14  	"unicode/utf8"
    15  )
    16  
    17  const (
    18  	defaultBufSize = 4096
    19  )
    20  
    21  var (
    22  	ErrInvalidUnreadByte = errors.New("bufio: invalid use of UnreadByte")
    23  	ErrInvalidUnreadRune = errors.New("bufio: invalid use of UnreadRune")
    24  	ErrBufferFull        = errors.New("bufio: buffer full")
    25  	ErrNegativeCount     = errors.New("bufio: negative count")
    26  )
    27  
    28  // Buffered input.
    29  
    30  // Reader implements buffering for an io.Reader object.
    31  type Reader struct {
    32  	buf          []byte
    33  	rd           io.Reader // reader provided by the client
    34  	r, w         int       // buf read and write positions
    35  	err          error
    36  	lastByte     int // last byte read for UnreadByte; -1 means invalid
    37  	lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid
    38  }
    39  
    40  const minReadBufferSize = 16
    41  const maxConsecutiveEmptyReads = 100
    42  
    43  // NewReaderSize returns a new Reader whose buffer has at least the specified
    44  // size. If the argument io.Reader is already a Reader with large enough
    45  // size, it returns the underlying Reader.
    46  func NewReaderSize(rd io.Reader, size int) *Reader {
    47  	// Is it already a Reader?
    48  	b, ok := rd.(*Reader)
    49  	if ok && len(b.buf) >= size {
    50  		return b
    51  	}
    52  	if size < minReadBufferSize {
    53  		size = minReadBufferSize
    54  	}
    55  	r := new(Reader)
    56  	r.reset(make([]byte, size), rd)
    57  	return r
    58  }
    59  
    60  // NewReader returns a new Reader whose buffer has the default size.
    61  func NewReader(rd io.Reader) *Reader {
    62  	return NewReaderSize(rd, defaultBufSize)
    63  }
    64  
    65  // Size returns the size of the underlying buffer in bytes.
    66  func (b *Reader) Size() int { return len(b.buf) }
    67  
    68  // Reset discards any buffered data, resets all state, and switches
    69  // the buffered reader to read from r.
    70  func (b *Reader) Reset(r io.Reader) {
    71  	b.reset(b.buf, r)
    72  }
    73  
    74  func (b *Reader) reset(buf []byte, r io.Reader) {
    75  	*b = Reader{
    76  		buf:          buf,
    77  		rd:           r,
    78  		lastByte:     -1,
    79  		lastRuneSize: -1,
    80  	}
    81  }
    82  
    83  var errNegativeRead = errors.New("bufio: reader returned negative count from Read")
    84  
    85  // fill reads a new chunk into the buffer.
    86  func (b *Reader) fill() {
    87  	// Slide existing data to beginning.
    88  	if b.r > 0 {
    89  		copy(b.buf, b.buf[b.r:b.w])
    90  		b.w -= b.r
    91  		b.r = 0
    92  	}
    93  
    94  	if b.w >= len(b.buf) {
    95  		panic("bufio: tried to fill full buffer")
    96  	}
    97  
    98  	// Read new data: try a limited number of times.
    99  	for i := maxConsecutiveEmptyReads; i > 0; i-- {
   100  		n, err := b.rd.Read(b.buf[b.w:])
   101  		if n < 0 {
   102  			panic(errNegativeRead)
   103  		}
   104  		b.w += n
   105  		if err != nil {
   106  			b.err = err
   107  			return
   108  		}
   109  		if n > 0 {
   110  			return
   111  		}
   112  	}
   113  	b.err = io.ErrNoProgress
   114  }
   115  
   116  func (b *Reader) readErr() error {
   117  	err := b.err
   118  	b.err = nil
   119  	return err
   120  }
   121  
   122  // Peek returns the next n bytes without advancing the reader. The bytes stop
   123  // being valid at the next read call. If Peek returns fewer than n bytes, it
   124  // also returns an error explaining why the read is short. The error is
   125  // ErrBufferFull if n is larger than b's buffer size.
   126  //
   127  // Calling Peek prevents a UnreadByte or UnreadRune call from succeeding
   128  // until the next read operation.
   129  func (b *Reader) Peek(n int) ([]byte, error) {
   130  	if n < 0 {
   131  		return nil, ErrNegativeCount
   132  	}
   133  
   134  	b.lastByte = -1
   135  	b.lastRuneSize = -1
   136  
   137  	for b.w-b.r < n && b.w-b.r < len(b.buf) && b.err == nil {
   138  		b.fill() // b.w-b.r < len(b.buf) => buffer is not full
   139  	}
   140  
   141  	if n > len(b.buf) {
   142  		return b.buf[b.r:b.w], ErrBufferFull
   143  	}
   144  
   145  	// 0 <= n <= len(b.buf)
   146  	var err error
   147  	if avail := b.w - b.r; avail < n {
   148  		// not enough data in buffer
   149  		n = avail
   150  		err = b.readErr()
   151  		if err == nil {
   152  			err = ErrBufferFull
   153  		}
   154  	}
   155  	return b.buf[b.r : b.r+n], err
   156  }
   157  
   158  // Discard skips the next n bytes, returning the number of bytes discarded.
   159  //
   160  // If Discard skips fewer than n bytes, it also returns an error.
   161  // If 0 <= n <= b.Buffered(), Discard is guaranteed to succeed without
   162  // reading from the underlying io.Reader.
   163  func (b *Reader) Discard(n int) (discarded int, err error) {
   164  	if n < 0 {
   165  		return 0, ErrNegativeCount
   166  	}
   167  	if n == 0 {
   168  		return
   169  	}
   170  	remain := n
   171  	for {
   172  		skip := b.Buffered()
   173  		if skip == 0 {
   174  			b.fill()
   175  			skip = b.Buffered()
   176  		}
   177  		if skip > remain {
   178  			skip = remain
   179  		}
   180  		b.r += skip
   181  		remain -= skip
   182  		if remain == 0 {
   183  			return n, nil
   184  		}
   185  		if b.err != nil {
   186  			return n - remain, b.readErr()
   187  		}
   188  	}
   189  }
   190  
   191  // Read reads data into p.
   192  // It returns the number of bytes read into p.
   193  // The bytes are taken from at most one Read on the underlying Reader,
   194  // hence n may be less than len(p).
   195  // To read exactly len(p) bytes, use io.ReadFull(b, p).
   196  // At EOF, the count will be zero and err will be io.EOF.
   197  func (b *Reader) Read(p []byte) (n int, err error) {
   198  	n = len(p)
   199  	if n == 0 {
   200  		if b.Buffered() > 0 {
   201  			return 0, nil
   202  		}
   203  		return 0, b.readErr()
   204  	}
   205  	if b.r == b.w {
   206  		if b.err != nil {
   207  			return 0, b.readErr()
   208  		}
   209  		if len(p) >= len(b.buf) {
   210  			// Large read, empty buffer.
   211  			// Read directly into p to avoid copy.
   212  			n, b.err = b.rd.Read(p)
   213  			if n < 0 {
   214  				panic(errNegativeRead)
   215  			}
   216  			if n > 0 {
   217  				b.lastByte = int(p[n-1])
   218  				b.lastRuneSize = -1
   219  			}
   220  			return n, b.readErr()
   221  		}
   222  		// One read.
   223  		// Do not use b.fill, which will loop.
   224  		b.r = 0
   225  		b.w = 0
   226  		n, b.err = b.rd.Read(b.buf)
   227  		if n < 0 {
   228  			panic(errNegativeRead)
   229  		}
   230  		if n == 0 {
   231  			return 0, b.readErr()
   232  		}
   233  		b.w += n
   234  	}
   235  
   236  	// copy as much as we can
   237  	n = copy(p, b.buf[b.r:b.w])
   238  	b.r += n
   239  	b.lastByte = int(b.buf[b.r-1])
   240  	b.lastRuneSize = -1
   241  	return n, nil
   242  }
   243  
   244  // ReadByte reads and returns a single byte.
   245  // If no byte is available, returns an error.
   246  func (b *Reader) ReadByte() (byte, error) {
   247  	b.lastRuneSize = -1
   248  	for b.r == b.w {
   249  		if b.err != nil {
   250  			return 0, b.readErr()
   251  		}
   252  		b.fill() // buffer is empty
   253  	}
   254  	c := b.buf[b.r]
   255  	b.r++
   256  	b.lastByte = int(c)
   257  	return c, nil
   258  }
   259  
   260  // UnreadByte unreads the last byte. Only the most recently read byte can be unread.
   261  //
   262  // UnreadByte returns an error if the most recent method called on the
   263  // Reader was not a read operation. Notably, Peek is not considered a
   264  // read operation.
   265  func (b *Reader) UnreadByte() error {
   266  	if b.lastByte < 0 || b.r == 0 && b.w > 0 {
   267  		return ErrInvalidUnreadByte
   268  	}
   269  	// b.r > 0 || b.w == 0
   270  	if b.r > 0 {
   271  		b.r--
   272  	} else {
   273  		// b.r == 0 && b.w == 0
   274  		b.w = 1
   275  	}
   276  	b.buf[b.r] = byte(b.lastByte)
   277  	b.lastByte = -1
   278  	b.lastRuneSize = -1
   279  	return nil
   280  }
   281  
   282  // ReadRune reads a single UTF-8 encoded Unicode character and returns the
   283  // rune and its size in bytes. If the encoded rune is invalid, it consumes one byte
   284  // and returns unicode.ReplacementChar (U+FFFD) with a size of 1.
   285  func (b *Reader) ReadRune() (r rune, size int, err error) {
   286  	for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil && b.w-b.r < len(b.buf) {
   287  		b.fill() // b.w-b.r < len(buf) => buffer is not full
   288  	}
   289  	b.lastRuneSize = -1
   290  	if b.r == b.w {
   291  		return 0, 0, b.readErr()
   292  	}
   293  	r, size = rune(b.buf[b.r]), 1
   294  	if r >= utf8.RuneSelf {
   295  		r, size = utf8.DecodeRune(b.buf[b.r:b.w])
   296  	}
   297  	b.r += size
   298  	b.lastByte = int(b.buf[b.r-1])
   299  	b.lastRuneSize = size
   300  	return r, size, nil
   301  }
   302  
   303  // UnreadRune unreads the last rune. If the most recent method called on
   304  // the Reader was not a ReadRune, UnreadRune returns an error. (In this
   305  // regard it is stricter than UnreadByte, which will unread the last byte
   306  // from any read operation.)
   307  func (b *Reader) UnreadRune() error {
   308  	if b.lastRuneSize < 0 || b.r < b.lastRuneSize {
   309  		return ErrInvalidUnreadRune
   310  	}
   311  	b.r -= b.lastRuneSize
   312  	b.lastByte = -1
   313  	b.lastRuneSize = -1
   314  	return nil
   315  }
   316  
   317  // Buffered returns the number of bytes that can be read from the current buffer.
   318  func (b *Reader) Buffered() int { return b.w - b.r }
   319  
   320  // ReadSlice reads until the first occurrence of delim in the input,
   321  // returning a slice pointing at the bytes in the buffer.
   322  // The bytes stop being valid at the next read.
   323  // If ReadSlice encounters an error before finding a delimiter,
   324  // it returns all the data in the buffer and the error itself (often io.EOF).
   325  // ReadSlice fails with error ErrBufferFull if the buffer fills without a delim.
   326  // Because the data returned from ReadSlice will be overwritten
   327  // by the next I/O operation, most clients should use
   328  // ReadBytes or ReadString instead.
   329  // ReadSlice returns err != nil if and only if line does not end in delim.
   330  func (b *Reader) ReadSlice(delim byte) (line []byte, err error) {
   331  	s := 0 // search start index
   332  	for {
   333  		// Search buffer.
   334  		if i := bytes.IndexByte(b.buf[b.r+s:b.w], delim); i >= 0 {
   335  			i += s
   336  			line = b.buf[b.r : b.r+i+1]
   337  			b.r += i + 1
   338  			break
   339  		}
   340  
   341  		// Pending error?
   342  		if b.err != nil {
   343  			line = b.buf[b.r:b.w]
   344  			b.r = b.w
   345  			err = b.readErr()
   346  			break
   347  		}
   348  
   349  		// Buffer full?
   350  		if b.Buffered() >= len(b.buf) {
   351  			b.r = b.w
   352  			line = b.buf
   353  			err = ErrBufferFull
   354  			break
   355  		}
   356  
   357  		s = b.w - b.r // do not rescan area we scanned before
   358  
   359  		b.fill() // buffer is not full
   360  	}
   361  
   362  	// Handle last byte, if any.
   363  	if i := len(line) - 1; i >= 0 {
   364  		b.lastByte = int(line[i])
   365  		b.lastRuneSize = -1
   366  	}
   367  
   368  	return
   369  }
   370  
   371  // ReadLine is a low-level line-reading primitive. Most callers should use
   372  // ReadBytes('\n') or ReadString('\n') instead or use a Scanner.
   373  //
   374  // ReadLine tries to return a single line, not including the end-of-line bytes.
   375  // If the line was too long for the buffer then isPrefix is set and the
   376  // beginning of the line is returned. The rest of the line will be returned
   377  // from future calls. isPrefix will be false when returning the last fragment
   378  // of the line. The returned buffer is only valid until the next call to
   379  // ReadLine. ReadLine either returns a non-nil line or it returns an error,
   380  // never both.
   381  //
   382  // The text returned from ReadLine does not include the line end ("\r\n" or "\n").
   383  // No indication or error is given if the input ends without a final line end.
   384  // Calling UnreadByte after ReadLine will always unread the last byte read
   385  // (possibly a character belonging to the line end) even if that byte is not
   386  // part of the line returned by ReadLine.
   387  func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) {
   388  	line, err = b.ReadSlice('\n')
   389  	if err == ErrBufferFull {
   390  		// Handle the case where "\r\n" straddles the buffer.
   391  		if len(line) > 0 && line[len(line)-1] == '\r' {
   392  			// Put the '\r' back on buf and drop it from line.
   393  			// Let the next call to ReadLine check for "\r\n".
   394  			if b.r == 0 {
   395  				// should be unreachable
   396  				panic("bufio: tried to rewind past start of buffer")
   397  			}
   398  			b.r--
   399  			line = line[:len(line)-1]
   400  		}
   401  		return line, true, nil
   402  	}
   403  
   404  	if len(line) == 0 {
   405  		if err != nil {
   406  			line = nil
   407  		}
   408  		return
   409  	}
   410  	err = nil
   411  
   412  	if line[len(line)-1] == '\n' {
   413  		drop := 1
   414  		if len(line) > 1 && line[len(line)-2] == '\r' {
   415  			drop = 2
   416  		}
   417  		line = line[:len(line)-drop]
   418  	}
   419  	return
   420  }
   421  
   422  // ReadBytes reads until the first occurrence of delim in the input,
   423  // returning a slice containing the data up to and including the delimiter.
   424  // If ReadBytes encounters an error before finding a delimiter,
   425  // it returns the data read before the error and the error itself (often io.EOF).
   426  // ReadBytes returns err != nil if and only if the returned data does not end in
   427  // delim.
   428  // For simple uses, a Scanner may be more convenient.
   429  func (b *Reader) ReadBytes(delim byte) ([]byte, error) {
   430  	// Use ReadSlice to look for array,
   431  	// accumulating full buffers.
   432  	var frag []byte
   433  	var full [][]byte
   434  	var err error
   435  	for {
   436  		var e error
   437  		frag, e = b.ReadSlice(delim)
   438  		if e == nil { // got final fragment
   439  			break
   440  		}
   441  		if e != ErrBufferFull { // unexpected error
   442  			err = e
   443  			break
   444  		}
   445  
   446  		// Make a copy of the buffer.
   447  		buf := make([]byte, len(frag))
   448  		copy(buf, frag)
   449  		full = append(full, buf)
   450  	}
   451  
   452  	// Allocate new buffer to hold the full pieces and the fragment.
   453  	n := 0
   454  	for i := range full {
   455  		n += len(full[i])
   456  	}
   457  	n += len(frag)
   458  
   459  	// Copy full pieces and fragment in.
   460  	buf := make([]byte, n)
   461  	n = 0
   462  	for i := range full {
   463  		n += copy(buf[n:], full[i])
   464  	}
   465  	copy(buf[n:], frag)
   466  	return buf, err
   467  }
   468  
   469  // ReadString reads until the first occurrence of delim in the input,
   470  // returning a string containing the data up to and including the delimiter.
   471  // If ReadString encounters an error before finding a delimiter,
   472  // it returns the data read before the error and the error itself (often io.EOF).
   473  // ReadString returns err != nil if and only if the returned data does not end in
   474  // delim.
   475  // For simple uses, a Scanner may be more convenient.
   476  func (b *Reader) ReadString(delim byte) (string, error) {
   477  	bytes, err := b.ReadBytes(delim)
   478  	return string(bytes), err
   479  }
   480  
   481  // WriteTo implements io.WriterTo.
   482  // This may make multiple calls to the Read method of the underlying Reader.
   483  // If the underlying reader supports the WriteTo method,
   484  // this calls the underlying WriteTo without buffering.
   485  func (b *Reader) WriteTo(w io.Writer) (n int64, err error) {
   486  	n, err = b.writeBuf(w)
   487  	if err != nil {
   488  		return
   489  	}
   490  
   491  	if r, ok := b.rd.(io.WriterTo); ok {
   492  		m, err := r.WriteTo(w)
   493  		n += m
   494  		return n, err
   495  	}
   496  
   497  	if w, ok := w.(io.ReaderFrom); ok {
   498  		m, err := w.ReadFrom(b.rd)
   499  		n += m
   500  		return n, err
   501  	}
   502  
   503  	if b.w-b.r < len(b.buf) {
   504  		b.fill() // buffer not full
   505  	}
   506  
   507  	for b.r < b.w {
   508  		// b.r < b.w => buffer is not empty
   509  		m, err := b.writeBuf(w)
   510  		n += m
   511  		if err != nil {
   512  			return n, err
   513  		}
   514  		b.fill() // buffer is empty
   515  	}
   516  
   517  	if b.err == io.EOF {
   518  		b.err = nil
   519  	}
   520  
   521  	return n, b.readErr()
   522  }
   523  
   524  var errNegativeWrite = errors.New("bufio: writer returned negative count from Write")
   525  
   526  // writeBuf writes the Reader's buffer to the writer.
   527  func (b *Reader) writeBuf(w io.Writer) (int64, error) {
   528  	n, err := w.Write(b.buf[b.r:b.w])
   529  	if n < 0 {
   530  		panic(errNegativeWrite)
   531  	}
   532  	b.r += n
   533  	return int64(n), err
   534  }
   535  
   536  // buffered output
   537  
   538  // Writer implements buffering for an io.Writer object.
   539  // If an error occurs writing to a Writer, no more data will be
   540  // accepted and all subsequent writes, and Flush, will return the error.
   541  // After all data has been written, the client should call the
   542  // Flush method to guarantee all data has been forwarded to
   543  // the underlying io.Writer.
   544  type Writer struct {
   545  	err error
   546  	buf []byte
   547  	n   int
   548  	wr  io.Writer
   549  }
   550  
   551  // NewWriterSize returns a new Writer whose buffer has at least the specified
   552  // size. If the argument io.Writer is already a Writer with large enough
   553  // size, it returns the underlying Writer.
   554  func NewWriterSize(w io.Writer, size int) *Writer {
   555  	// Is it already a Writer?
   556  	b, ok := w.(*Writer)
   557  	if ok && len(b.buf) >= size {
   558  		return b
   559  	}
   560  	if size <= 0 {
   561  		size = defaultBufSize
   562  	}
   563  	return &Writer{
   564  		buf: make([]byte, size),
   565  		wr:  w,
   566  	}
   567  }
   568  
   569  // NewWriter returns a new Writer whose buffer has the default size.
   570  func NewWriter(w io.Writer) *Writer {
   571  	return NewWriterSize(w, defaultBufSize)
   572  }
   573  
   574  // Size returns the size of the underlying buffer in bytes.
   575  func (b *Writer) Size() int { return len(b.buf) }
   576  
   577  // Reset discards any unflushed buffered data, clears any error, and
   578  // resets b to write its output to w.
   579  func (b *Writer) Reset(w io.Writer) {
   580  	b.err = nil
   581  	b.n = 0
   582  	b.wr = w
   583  }
   584  
   585  // Flush writes any buffered data to the underlying io.Writer.
   586  func (b *Writer) Flush() error {
   587  	if b.err != nil {
   588  		return b.err
   589  	}
   590  	if b.n == 0 {
   591  		return nil
   592  	}
   593  	n, err := b.wr.Write(b.buf[0:b.n])
   594  	if n < b.n && err == nil {
   595  		err = io.ErrShortWrite
   596  	}
   597  	if err != nil {
   598  		if n > 0 && n < b.n {
   599  			copy(b.buf[0:b.n-n], b.buf[n:b.n])
   600  		}
   601  		b.n -= n
   602  		b.err = err
   603  		return err
   604  	}
   605  	b.n = 0
   606  	return nil
   607  }
   608  
   609  // Available returns how many bytes are unused in the buffer.
   610  func (b *Writer) Available() int { return len(b.buf) - b.n }
   611  
   612  // Buffered returns the number of bytes that have been written into the current buffer.
   613  func (b *Writer) Buffered() int { return b.n }
   614  
   615  // Write writes the contents of p into the buffer.
   616  // It returns the number of bytes written.
   617  // If nn < len(p), it also returns an error explaining
   618  // why the write is short.
   619  func (b *Writer) Write(p []byte) (nn int, err error) {
   620  	for len(p) > b.Available() && b.err == nil {
   621  		var n int
   622  		if b.Buffered() == 0 {
   623  			// Large write, empty buffer.
   624  			// Write directly from p to avoid copy.
   625  			n, b.err = b.wr.Write(p)
   626  		} else {
   627  			n = copy(b.buf[b.n:], p)
   628  			b.n += n
   629  			b.Flush()
   630  		}
   631  		nn += n
   632  		p = p[n:]
   633  	}
   634  	if b.err != nil {
   635  		return nn, b.err
   636  	}
   637  	n := copy(b.buf[b.n:], p)
   638  	b.n += n
   639  	nn += n
   640  	return nn, nil
   641  }
   642  
   643  // WriteByte writes a single byte.
   644  func (b *Writer) WriteByte(c byte) error {
   645  	if b.err != nil {
   646  		return b.err
   647  	}
   648  	if b.Available() <= 0 && b.Flush() != nil {
   649  		return b.err
   650  	}
   651  	b.buf[b.n] = c
   652  	b.n++
   653  	return nil
   654  }
   655  
   656  // WriteRune writes a single Unicode code point, returning
   657  // the number of bytes written and any error.
   658  func (b *Writer) WriteRune(r rune) (size int, err error) {
   659  	if r < utf8.RuneSelf {
   660  		err = b.WriteByte(byte(r))
   661  		if err != nil {
   662  			return 0, err
   663  		}
   664  		return 1, nil
   665  	}
   666  	if b.err != nil {
   667  		return 0, b.err
   668  	}
   669  	n := b.Available()
   670  	if n < utf8.UTFMax {
   671  		if b.Flush(); b.err != nil {
   672  			return 0, b.err
   673  		}
   674  		n = b.Available()
   675  		if n < utf8.UTFMax {
   676  			// Can only happen if buffer is silly small.
   677  			return b.WriteString(string(r))
   678  		}
   679  	}
   680  	size = utf8.EncodeRune(b.buf[b.n:], r)
   681  	b.n += size
   682  	return size, nil
   683  }
   684  
   685  // WriteString writes a string.
   686  // It returns the number of bytes written.
   687  // If the count is less than len(s), it also returns an error explaining
   688  // why the write is short.
   689  func (b *Writer) WriteString(s string) (int, error) {
   690  	nn := 0
   691  	for len(s) > b.Available() && b.err == nil {
   692  		n := copy(b.buf[b.n:], s)
   693  		b.n += n
   694  		nn += n
   695  		s = s[n:]
   696  		b.Flush()
   697  	}
   698  	if b.err != nil {
   699  		return nn, b.err
   700  	}
   701  	n := copy(b.buf[b.n:], s)
   702  	b.n += n
   703  	nn += n
   704  	return nn, nil
   705  }
   706  
   707  // ReadFrom implements io.ReaderFrom. If the underlying writer
   708  // supports the ReadFrom method, and b has no buffered data yet,
   709  // this calls the underlying ReadFrom without buffering.
   710  func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) {
   711  	if b.Buffered() == 0 {
   712  		if w, ok := b.wr.(io.ReaderFrom); ok {
   713  			return w.ReadFrom(r)
   714  		}
   715  	}
   716  	var m int
   717  	for {
   718  		if b.Available() == 0 {
   719  			if err1 := b.Flush(); err1 != nil {
   720  				return n, err1
   721  			}
   722  		}
   723  		nr := 0
   724  		for nr < maxConsecutiveEmptyReads {
   725  			m, err = r.Read(b.buf[b.n:])
   726  			if m != 0 || err != nil {
   727  				break
   728  			}
   729  			nr++
   730  		}
   731  		if nr == maxConsecutiveEmptyReads {
   732  			return n, io.ErrNoProgress
   733  		}
   734  		b.n += m
   735  		n += int64(m)
   736  		if err != nil {
   737  			break
   738  		}
   739  	}
   740  	if err == io.EOF {
   741  		// If we filled the buffer exactly, flush preemptively.
   742  		if b.Available() == 0 {
   743  			err = b.Flush()
   744  		} else {
   745  			err = nil
   746  		}
   747  	}
   748  	return n, err
   749  }
   750  
   751  // buffered input and output
   752  
   753  // ReadWriter stores pointers to a Reader and a Writer.
   754  // It implements io.ReadWriter.
   755  type ReadWriter struct {
   756  	*Reader
   757  	*Writer
   758  }
   759  
   760  // NewReadWriter allocates a new ReadWriter that dispatches to r and w.
   761  func NewReadWriter(r *Reader, w *Writer) *ReadWriter {
   762  	return &ReadWriter{r, w}
   763  }
   764  

View as plain text