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