...
Run Format

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

View as plain text