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