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

View as plain text