github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/src/bufio/bufio.go (about) 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 err = nil 404 405 for { 406 var e error 407 frag, e = b.ReadSlice(delim) 408 if e == nil { // got final fragment 409 break 410 } 411 if e != ErrBufferFull { // unexpected error 412 err = e 413 break 414 } 415 416 // Make a copy of the buffer. 417 buf := make([]byte, len(frag)) 418 copy(buf, frag) 419 full = append(full, buf) 420 } 421 422 // Allocate new buffer to hold the full pieces and the fragment. 423 n := 0 424 for i := range full { 425 n += len(full[i]) 426 } 427 n += len(frag) 428 429 // Copy full pieces and fragment in. 430 buf := make([]byte, n) 431 n = 0 432 for i := range full { 433 n += copy(buf[n:], full[i]) 434 } 435 copy(buf[n:], frag) 436 return buf, err 437 } 438 439 // ReadString reads until the first occurrence of delim in the input, 440 // returning a string containing the data up to and including the delimiter. 441 // If ReadString encounters an error before finding a delimiter, 442 // it returns the data read before the error and the error itself (often io.EOF). 443 // ReadString returns err != nil if and only if the returned data does not end in 444 // delim. 445 // For simple uses, a Scanner may be more convenient. 446 func (b *Reader) ReadString(delim byte) (line string, err error) { 447 bytes, err := b.ReadBytes(delim) 448 line = string(bytes) 449 return line, 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 pre-emptively. 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 }