Source file src/bufio/bufio.go

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

View as plain text