...
Run Format

Source file src/pkg/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
    34		r, w         int
    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		m := b.w - b.r
   135		if m > n {
   136			m = n
   137		}
   138		var err error
   139		if m < n {
   140			err = b.readErr()
   141			if err == nil {
   142				err = ErrBufferFull
   143			}
   144		}
   145		return b.buf[b.r : b.r+m], err
   146	}
   147	
   148	// Read reads data into p.
   149	// It returns the number of bytes read into p.
   150	// It calls Read at most once on the underlying Reader,
   151	// hence n may be less than len(p).
   152	// At EOF, the count will be zero and err will be io.EOF.
   153	func (b *Reader) Read(p []byte) (n int, err error) {
   154		n = len(p)
   155		if n == 0 {
   156			return 0, b.readErr()
   157		}
   158		if b.r == b.w {
   159			if b.err != nil {
   160				return 0, b.readErr()
   161			}
   162			if len(p) >= len(b.buf) {
   163				// Large read, empty buffer.
   164				// Read directly into p to avoid copy.
   165				n, b.err = b.rd.Read(p)
   166				if n < 0 {
   167					panic(errNegativeRead)
   168				}
   169				if n > 0 {
   170					b.lastByte = int(p[n-1])
   171					b.lastRuneSize = -1
   172				}
   173				return n, b.readErr()
   174			}
   175			b.fill() // buffer is empty
   176			if b.w == b.r {
   177				return 0, b.readErr()
   178			}
   179		}
   180	
   181		if n > b.w-b.r {
   182			n = b.w - b.r
   183		}
   184		copy(p[0:n], b.buf[b.r:])
   185		b.r += n
   186		b.lastByte = int(b.buf[b.r-1])
   187		b.lastRuneSize = -1
   188		return n, nil
   189	}
   190	
   191	// ReadByte reads and returns a single byte.
   192	// If no byte is available, returns an error.
   193	func (b *Reader) ReadByte() (c byte, err error) {
   194		b.lastRuneSize = -1
   195		for b.r == b.w {
   196			if b.err != nil {
   197				return 0, b.readErr()
   198			}
   199			b.fill() // buffer is empty
   200		}
   201		c = b.buf[b.r]
   202		b.r++
   203		b.lastByte = int(c)
   204		return c, nil
   205	}
   206	
   207	// UnreadByte unreads the last byte.  Only the most recently read byte can be unread.
   208	func (b *Reader) UnreadByte() error {
   209		if b.lastByte < 0 || b.r == 0 && b.w > 0 {
   210			return ErrInvalidUnreadByte
   211		}
   212		// b.r > 0 || b.w == 0
   213		if b.r > 0 {
   214			b.r--
   215		} else {
   216			// b.r == 0 && b.w == 0
   217			b.w = 1
   218		}
   219		b.buf[b.r] = byte(b.lastByte)
   220		b.lastByte = -1
   221		b.lastRuneSize = -1
   222		return nil
   223	}
   224	
   225	// ReadRune reads a single UTF-8 encoded Unicode character and returns the
   226	// rune and its size in bytes. If the encoded rune is invalid, it consumes one byte
   227	// and returns unicode.ReplacementChar (U+FFFD) with a size of 1.
   228	func (b *Reader) ReadRune() (r rune, size int, err error) {
   229		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) {
   230			b.fill() // b.w-b.r < len(buf) => buffer is not full
   231		}
   232		b.lastRuneSize = -1
   233		if b.r == b.w {
   234			return 0, 0, b.readErr()
   235		}
   236		r, size = rune(b.buf[b.r]), 1
   237		if r >= 0x80 {
   238			r, size = utf8.DecodeRune(b.buf[b.r:b.w])
   239		}
   240		b.r += size
   241		b.lastByte = int(b.buf[b.r-1])
   242		b.lastRuneSize = size
   243		return r, size, nil
   244	}
   245	
   246	// UnreadRune unreads the last rune.  If the most recent read operation on
   247	// the buffer was not a ReadRune, UnreadRune returns an error.  (In this
   248	// regard it is stricter than UnreadByte, which will unread the last byte
   249	// from any read operation.)
   250	func (b *Reader) UnreadRune() error {
   251		if b.lastRuneSize < 0 || b.r < b.lastRuneSize {
   252			return ErrInvalidUnreadRune
   253		}
   254		b.r -= b.lastRuneSize
   255		b.lastByte = -1
   256		b.lastRuneSize = -1
   257		return nil
   258	}
   259	
   260	// Buffered returns the number of bytes that can be read from the current buffer.
   261	func (b *Reader) Buffered() int { return b.w - b.r }
   262	
   263	// ReadSlice reads until the first occurrence of delim in the input,
   264	// returning a slice pointing at the bytes in the buffer.
   265	// The bytes stop being valid at the next read.
   266	// If ReadSlice encounters an error before finding a delimiter,
   267	// it returns all the data in the buffer and the error itself (often io.EOF).
   268	// ReadSlice fails with error ErrBufferFull if the buffer fills without a delim.
   269	// Because the data returned from ReadSlice will be overwritten
   270	// by the next I/O operation, most clients should use
   271	// ReadBytes or ReadString instead.
   272	// ReadSlice returns err != nil if and only if line does not end in delim.
   273	func (b *Reader) ReadSlice(delim byte) (line []byte, err error) {
   274		for {
   275			// Search buffer.
   276			if i := bytes.IndexByte(b.buf[b.r:b.w], delim); i >= 0 {
   277				line = b.buf[b.r : b.r+i+1]
   278				b.r += i + 1
   279				break
   280			}
   281	
   282			// Pending error?
   283			if b.err != nil {
   284				line = b.buf[b.r:b.w]
   285				b.r = b.w
   286				err = b.readErr()
   287				break
   288			}
   289	
   290			// Buffer full?
   291			if n := b.Buffered(); n >= len(b.buf) {
   292				b.r = b.w
   293				line = b.buf
   294				err = ErrBufferFull
   295				break
   296			}
   297	
   298			b.fill() // buffer is not full
   299		}
   300	
   301		// Handle last byte, if any.
   302		if i := len(line) - 1; i >= 0 {
   303			b.lastByte = int(line[i])
   304		}
   305	
   306		return
   307	}
   308	
   309	// ReadLine is a low-level line-reading primitive. Most callers should use
   310	// ReadBytes('\n') or ReadString('\n') instead or use a Scanner.
   311	//
   312	// ReadLine tries to return a single line, not including the end-of-line bytes.
   313	// If the line was too long for the buffer then isPrefix is set and the
   314	// beginning of the line is returned. The rest of the line will be returned
   315	// from future calls. isPrefix will be false when returning the last fragment
   316	// of the line. The returned buffer is only valid until the next call to
   317	// ReadLine. ReadLine either returns a non-nil line or it returns an error,
   318	// never both.
   319	//
   320	// The text returned from ReadLine does not include the line end ("\r\n" or "\n").
   321	// No indication or error is given if the input ends without a final line end.
   322	// Calling UnreadByte after ReadLine will always unread the last byte read
   323	// (possibly a character belonging to the line end) even if that byte is not
   324	// part of the line returned by ReadLine.
   325	func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) {
   326		line, err = b.ReadSlice('\n')
   327		if err == ErrBufferFull {
   328			// Handle the case where "\r\n" straddles the buffer.
   329			if len(line) > 0 && line[len(line)-1] == '\r' {
   330				// Put the '\r' back on buf and drop it from line.
   331				// Let the next call to ReadLine check for "\r\n".
   332				if b.r == 0 {
   333					// should be unreachable
   334					panic("bufio: tried to rewind past start of buffer")
   335				}
   336				b.r--
   337				line = line[:len(line)-1]
   338			}
   339			return line, true, nil
   340		}
   341	
   342		if len(line) == 0 {
   343			if err != nil {
   344				line = nil
   345			}
   346			return
   347		}
   348		err = nil
   349	
   350		if line[len(line)-1] == '\n' {
   351			drop := 1
   352			if len(line) > 1 && line[len(line)-2] == '\r' {
   353				drop = 2
   354			}
   355			line = line[:len(line)-drop]
   356		}
   357		return
   358	}
   359	
   360	// ReadBytes reads until the first occurrence of delim in the input,
   361	// returning a slice containing the data up to and including the delimiter.
   362	// If ReadBytes encounters an error before finding a delimiter,
   363	// it returns the data read before the error and the error itself (often io.EOF).
   364	// ReadBytes returns err != nil if and only if the returned data does not end in
   365	// delim.
   366	// For simple uses, a Scanner may be more convenient.
   367	func (b *Reader) ReadBytes(delim byte) (line []byte, err error) {
   368		// Use ReadSlice to look for array,
   369		// accumulating full buffers.
   370		var frag []byte
   371		var full [][]byte
   372		err = nil
   373	
   374		for {
   375			var e error
   376			frag, e = b.ReadSlice(delim)
   377			if e == nil { // got final fragment
   378				break
   379			}
   380			if e != ErrBufferFull { // unexpected error
   381				err = e
   382				break
   383			}
   384	
   385			// Make a copy of the buffer.
   386			buf := make([]byte, len(frag))
   387			copy(buf, frag)
   388			full = append(full, buf)
   389		}
   390	
   391		// Allocate new buffer to hold the full pieces and the fragment.
   392		n := 0
   393		for i := range full {
   394			n += len(full[i])
   395		}
   396		n += len(frag)
   397	
   398		// Copy full pieces and fragment in.
   399		buf := make([]byte, n)
   400		n = 0
   401		for i := range full {
   402			n += copy(buf[n:], full[i])
   403		}
   404		copy(buf[n:], frag)
   405		return buf, err
   406	}
   407	
   408	// ReadString reads until the first occurrence of delim in the input,
   409	// returning a string containing the data up to and including the delimiter.
   410	// If ReadString encounters an error before finding a delimiter,
   411	// it returns the data read before the error and the error itself (often io.EOF).
   412	// ReadString returns err != nil if and only if the returned data does not end in
   413	// delim.
   414	// For simple uses, a Scanner may be more convenient.
   415	func (b *Reader) ReadString(delim byte) (line string, err error) {
   416		bytes, err := b.ReadBytes(delim)
   417		line = string(bytes)
   418		return line, err
   419	}
   420	
   421	// WriteTo implements io.WriterTo.
   422	func (b *Reader) WriteTo(w io.Writer) (n int64, err error) {
   423		n, err = b.writeBuf(w)
   424		if err != nil {
   425			return
   426		}
   427	
   428		if r, ok := b.rd.(io.WriterTo); ok {
   429			m, err := r.WriteTo(w)
   430			n += m
   431			return n, err
   432		}
   433	
   434		if w, ok := w.(io.ReaderFrom); ok {
   435			m, err := w.ReadFrom(b.rd)
   436			n += m
   437			return n, err
   438		}
   439	
   440		if b.w-b.r < len(b.buf) {
   441			b.fill() // buffer not full
   442		}
   443	
   444		for b.r < b.w {
   445			// b.r < b.w => buffer is not empty
   446			m, err := b.writeBuf(w)
   447			n += m
   448			if err != nil {
   449				return n, err
   450			}
   451			b.fill() // buffer is empty
   452		}
   453	
   454		if b.err == io.EOF {
   455			b.err = nil
   456		}
   457	
   458		return n, b.readErr()
   459	}
   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 < b.r-b.w {
   465			panic(errors.New("bufio: writer did not write all data"))
   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