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