You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

728 lines
17 KiB

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