...
Run Format

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

View as plain text