bufio.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. )
  13. const (
  14. defaultBufSize = 4096
  15. )
  16. var (
  17. // ErrInvalidUnreadByte invalid use of UnreadByete
  18. ErrInvalidUnreadByte = errors.New("bufio: invalid use of UnreadByte")
  19. // ErrInvalidUnreadRune invalid use of UnreadRune
  20. ErrInvalidUnreadRune = errors.New("bufio: invalid use of UnreadRune")
  21. // ErrBufferFull buffer full
  22. ErrBufferFull = errors.New("bufio: buffer full")
  23. // ErrNegativeCount negative count
  24. ErrNegativeCount = errors.New("bufio: negative count")
  25. )
  26. // Buffered input.
  27. // Reader implements buffering for an io.Reader object.
  28. type Reader struct {
  29. buf []byte
  30. rd io.Reader // reader provided by the client
  31. r, w int // buf read and write positions
  32. err error
  33. }
  34. const minReadBufferSize = 16
  35. const maxConsecutiveEmptyReads = 100
  36. // NewReaderSize returns a new Reader whose buffer has at least the specified
  37. // size. If the argument io.Reader is already a Reader with large enough
  38. // size, it returns the underlying Reader.
  39. func NewReaderSize(rd io.Reader, size int) *Reader {
  40. // Is it already a Reader?
  41. b, ok := rd.(*Reader)
  42. if ok && len(b.buf) >= size {
  43. return b
  44. }
  45. if size < minReadBufferSize {
  46. size = minReadBufferSize
  47. }
  48. r := new(Reader)
  49. r.reset(make([]byte, size), rd)
  50. return r
  51. }
  52. // NewReader returns a new Reader whose buffer has the default size.
  53. func NewReader(rd io.Reader) *Reader {
  54. return NewReaderSize(rd, defaultBufSize)
  55. }
  56. // Reset discards any buffered data, resets all state, and switches
  57. // the buffered reader to read from r.
  58. func (b *Reader) Reset(r io.Reader) {
  59. b.reset(b.buf, r)
  60. }
  61. // ResetBuffer discards any buffered data, resets all state, and switches
  62. // the buffered reader to read from r.
  63. func (b *Reader) ResetBuffer(r io.Reader, buf []byte) {
  64. b.reset(buf, r)
  65. }
  66. func (b *Reader) reset(buf []byte, r io.Reader) {
  67. *b = Reader{
  68. buf: buf,
  69. rd: r,
  70. }
  71. }
  72. var errNegativeRead = errors.New("bufio: reader returned negative count from Read")
  73. // fill reads a new chunk into the buffer.
  74. func (b *Reader) fill() {
  75. // Slide existing data to beginning.
  76. if b.r > 0 {
  77. copy(b.buf, b.buf[b.r:b.w])
  78. b.w -= b.r
  79. b.r = 0
  80. }
  81. if b.w >= len(b.buf) {
  82. panic("bufio: tried to fill full buffer")
  83. }
  84. // Read new data: try a limited number of times.
  85. for i := maxConsecutiveEmptyReads; i > 0; i-- {
  86. n, err := b.rd.Read(b.buf[b.w:])
  87. if n < 0 {
  88. panic(errNegativeRead)
  89. }
  90. b.w += n
  91. if err != nil {
  92. b.err = err
  93. return
  94. }
  95. if n > 0 {
  96. return
  97. }
  98. }
  99. b.err = io.ErrNoProgress
  100. }
  101. func (b *Reader) readErr() error {
  102. err := b.err
  103. b.err = nil
  104. return err
  105. }
  106. // Peek returns the next n bytes without advancing the reader. The bytes stop
  107. // being valid at the next read call. If Peek returns fewer than n bytes, it
  108. // also returns an error explaining why the read is short. The error is
  109. // ErrBufferFull if n is larger than b's buffer size.
  110. func (b *Reader) Peek(n int) ([]byte, error) {
  111. if n < 0 {
  112. return nil, ErrNegativeCount
  113. }
  114. if n > len(b.buf) {
  115. return nil, ErrBufferFull
  116. }
  117. // 0 <= n <= len(b.buf)
  118. for b.w-b.r < n && b.err == nil {
  119. b.fill() // b.w-b.r < len(b.buf) => buffer is not full
  120. }
  121. var err error
  122. if avail := b.w - b.r; avail < n {
  123. // not enough data in buffer
  124. n = avail
  125. err = b.readErr()
  126. if err == nil {
  127. err = ErrBufferFull
  128. }
  129. }
  130. return b.buf[b.r : b.r+n], err
  131. }
  132. // Pop returns the next n bytes with advancing the reader. The bytes stop
  133. // being valid at the next read call. If Pop returns fewer than n bytes, it
  134. // also returns an error explaining why the read is short. The error is
  135. // ErrBufferFull if n is larger than b's buffer size.
  136. func (b *Reader) Pop(n int) ([]byte, error) {
  137. d, err := b.Peek(n)
  138. if err == nil {
  139. b.r += n
  140. return d, err
  141. }
  142. return nil, err
  143. }
  144. // Discard skips the next n bytes, returning the number of bytes discarded.
  145. //
  146. // If Discard skips fewer than n bytes, it also returns an error.
  147. // If 0 <= n <= b.Buffered(), Discard is guaranteed to succeed without
  148. // reading from the underlying io.Reader.
  149. func (b *Reader) Discard(n int) (discarded int, err error) {
  150. if n < 0 {
  151. return 0, ErrNegativeCount
  152. }
  153. if n == 0 {
  154. return
  155. }
  156. remain := n
  157. for {
  158. skip := b.Buffered()
  159. if skip == 0 {
  160. b.fill()
  161. skip = b.Buffered()
  162. }
  163. if skip > remain {
  164. skip = remain
  165. }
  166. b.r += skip
  167. remain -= skip
  168. if remain == 0 {
  169. return n, nil
  170. }
  171. if b.err != nil {
  172. return n - remain, b.readErr()
  173. }
  174. }
  175. }
  176. // Read reads data into p.
  177. // It returns the number of bytes read into p.
  178. // It calls Read at most once on the underlying Reader,
  179. // hence n may be less than len(p).
  180. // At EOF, the count will be zero and err will be io.EOF.
  181. func (b *Reader) Read(p []byte) (n int, err error) {
  182. n = len(p)
  183. if n == 0 {
  184. return 0, b.readErr()
  185. }
  186. if b.r == b.w {
  187. if b.err != nil {
  188. return 0, b.readErr()
  189. }
  190. if len(p) >= len(b.buf) {
  191. // Large read, empty buffer.
  192. // Read directly into p to avoid copy.
  193. n, b.err = b.rd.Read(p)
  194. if n < 0 {
  195. panic(errNegativeRead)
  196. }
  197. return n, b.readErr()
  198. }
  199. b.fill() // buffer is empty
  200. if b.r == b.w {
  201. return 0, b.readErr()
  202. }
  203. }
  204. // copy as much as we can
  205. n = copy(p, b.buf[b.r:b.w])
  206. b.r += n
  207. return n, nil
  208. }
  209. // ReadByte reads and returns a single byte.
  210. // If no byte is available, returns an error.
  211. func (b *Reader) ReadByte() (c byte, err error) {
  212. //b.lastRuneSize = -1
  213. for b.r == b.w {
  214. if b.err != nil {
  215. return 0, b.readErr()
  216. }
  217. b.fill() // buffer is empty
  218. }
  219. c = b.buf[b.r]
  220. b.r++
  221. //b.lastByte = int(c)
  222. return c, nil
  223. }
  224. // ReadSlice reads until the first occurrence of delim in the input,
  225. // returning a slice pointing at the bytes in the buffer.
  226. // The bytes stop being valid at the next read.
  227. // If ReadSlice encounters an error before finding a delimiter,
  228. // it returns all the data in the buffer and the error itself (often io.EOF).
  229. // ReadSlice fails with error ErrBufferFull if the buffer fills without a delim.
  230. // Because the data returned from ReadSlice will be overwritten
  231. // by the next I/O operation, most clients should use
  232. // ReadBytes or ReadString instead.
  233. // ReadSlice returns err != nil if and only if line does not end in delim.
  234. func (b *Reader) ReadSlice(delim byte) (line []byte, err error) {
  235. for {
  236. // Search buffer.
  237. if i := bytes.IndexByte(b.buf[b.r:b.w], delim); i >= 0 {
  238. line = b.buf[b.r : b.r+i+1]
  239. b.r += i + 1
  240. break
  241. }
  242. // Pending error?
  243. if b.err != nil {
  244. line = b.buf[b.r:b.w]
  245. b.r = b.w
  246. err = b.readErr()
  247. break
  248. }
  249. // Buffer full?
  250. if b.Buffered() >= len(b.buf) {
  251. b.r = b.w
  252. line = b.buf
  253. err = ErrBufferFull
  254. break
  255. }
  256. b.fill() // buffer is not full
  257. }
  258. return
  259. }
  260. // ReadLine is a low-level line-reading primitive. Most callers should use
  261. // ReadBytes('\n') or ReadString('\n') instead or use a Scanner.
  262. //
  263. // ReadLine tries to return a single line, not including the end-of-line bytes.
  264. // If the line was too long for the buffer then isPrefix is set and the
  265. // beginning of the line is returned. The rest of the line will be returned
  266. // from future calls. isPrefix will be false when returning the last fragment
  267. // of the line. The returned buffer is only valid until the next call to
  268. // ReadLine. ReadLine either returns a non-nil line or it returns an error,
  269. // never both.
  270. //
  271. // The text returned from ReadLine does not include the line end ("\r\n" or "\n").
  272. // No indication or error is given if the input ends without a final line end.
  273. // Calling UnreadByte after ReadLine will always unread the last byte read
  274. // (possibly a character belonging to the line end) even if that byte is not
  275. // part of the line returned by ReadLine.
  276. func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) {
  277. line, err = b.ReadSlice('\n')
  278. if err == ErrBufferFull {
  279. // Handle the case where "\r\n" straddles the buffer.
  280. if len(line) > 0 && line[len(line)-1] == '\r' {
  281. // Put the '\r' back on buf and drop it from line.
  282. // Let the next call to ReadLine check for "\r\n".
  283. if b.r == 0 {
  284. // should be unreachable
  285. panic("bufio: tried to rewind past start of buffer")
  286. }
  287. b.r--
  288. line = line[:len(line)-1]
  289. }
  290. return line, true, nil
  291. }
  292. if len(line) == 0 {
  293. if err != nil {
  294. line = nil
  295. }
  296. return
  297. }
  298. err = nil
  299. if line[len(line)-1] == '\n' {
  300. drop := 1
  301. if len(line) > 1 && line[len(line)-2] == '\r' {
  302. drop = 2
  303. }
  304. line = line[:len(line)-drop]
  305. }
  306. return
  307. }
  308. // Buffered returns the number of bytes that can be read from the current buffer.
  309. func (b *Reader) Buffered() int { return b.w - b.r }
  310. // buffered output
  311. // Writer implements buffering for an io.Writer object.
  312. // If an error occurs writing to a Writer, no more data will be
  313. // accepted and all subsequent writes will return the error.
  314. // After all data has been written, the client should call the
  315. // Flush method to guarantee all data has been forwarded to
  316. // the underlying io.Writer.
  317. type Writer struct {
  318. err error
  319. buf []byte
  320. n int
  321. wr io.Writer
  322. }
  323. // NewWriterSize returns a new Writer whose buffer has at least the specified
  324. // size. If the argument io.Writer is already a Writer with large enough
  325. // size, it returns the underlying Writer.
  326. func NewWriterSize(w io.Writer, size int) *Writer {
  327. // Is it already a Writer?
  328. b, ok := w.(*Writer)
  329. if ok && len(b.buf) >= size {
  330. return b
  331. }
  332. if size <= 0 {
  333. size = defaultBufSize
  334. }
  335. return &Writer{
  336. buf: make([]byte, size),
  337. wr: w,
  338. }
  339. }
  340. // NewWriter returns a new Writer whose buffer has the default size.
  341. func NewWriter(w io.Writer) *Writer {
  342. return NewWriterSize(w, defaultBufSize)
  343. }
  344. // Reset discards any unflushed buffered data, clears any error, and
  345. // resets b to write its output to w.
  346. func (b *Writer) Reset(w io.Writer) {
  347. b.err = nil
  348. b.n = 0
  349. b.wr = w
  350. }
  351. // ResetBuffer discards any unflushed buffered data, clears any error, and
  352. // resets b to write its output to w.
  353. func (b *Writer) ResetBuffer(w io.Writer, buf []byte) {
  354. b.buf = buf
  355. b.err = nil
  356. b.n = 0
  357. b.wr = w
  358. }
  359. // Flush writes any buffered data to the underlying io.Writer.
  360. func (b *Writer) Flush() error {
  361. err := b.flush()
  362. return err
  363. }
  364. func (b *Writer) flush() error {
  365. if b.err != nil {
  366. return b.err
  367. }
  368. if b.n == 0 {
  369. return nil
  370. }
  371. n, err := b.wr.Write(b.buf[0:b.n])
  372. if n < b.n && err == nil {
  373. err = io.ErrShortWrite
  374. }
  375. if err != nil {
  376. if n > 0 && n < b.n {
  377. copy(b.buf[0:b.n-n], b.buf[n:b.n])
  378. }
  379. b.n -= n
  380. b.err = err
  381. return err
  382. }
  383. b.n = 0
  384. return nil
  385. }
  386. // Available returns how many bytes are unused in the buffer.
  387. func (b *Writer) Available() int { return len(b.buf) - b.n }
  388. // Buffered returns the number of bytes that have been written into the current buffer.
  389. func (b *Writer) Buffered() int { return b.n }
  390. // Write writes the contents of p into the buffer.
  391. // It returns the number of bytes written.
  392. // If nn < len(p), it also returns an error explaining
  393. // why the write is short.
  394. func (b *Writer) Write(p []byte) (nn int, err error) {
  395. for len(p) > b.Available() && b.err == nil {
  396. var n int
  397. if b.Buffered() == 0 {
  398. // Large write, empty buffer.
  399. // Write directly from p to avoid copy.
  400. n, b.err = b.wr.Write(p)
  401. } else {
  402. n = copy(b.buf[b.n:], p)
  403. b.n += n
  404. b.flush()
  405. }
  406. nn += n
  407. p = p[n:]
  408. }
  409. if b.err != nil {
  410. return nn, b.err
  411. }
  412. n := copy(b.buf[b.n:], p)
  413. b.n += n
  414. nn += n
  415. return nn, nil
  416. }
  417. // WriteRaw writes the contents of p into the raw io.Writer without buffer.
  418. // It returns the number of bytes written.
  419. // If nn < len(p), it also returns an error explaining
  420. // why the write is short.
  421. func (b *Writer) WriteRaw(p []byte) (nn int, err error) {
  422. if b.err != nil {
  423. return 0, b.err
  424. }
  425. if b.Buffered() == 0 {
  426. // if no buffer data, write raw writer
  427. nn, err = b.wr.Write(p)
  428. b.err = err
  429. } else {
  430. nn, err = b.Write(p)
  431. }
  432. return
  433. }
  434. // Peek returns the next n bytes with advancing the writer. The bytes stop
  435. // being used at the next write call. If Peek returns fewer than n bytes, it
  436. // also returns an error explaining why the read is short. The error is
  437. // ErrBufferFull if n is larger than b's buffer size.
  438. func (b *Writer) Peek(n int) ([]byte, error) {
  439. if n < 0 {
  440. return nil, ErrNegativeCount
  441. }
  442. if n > len(b.buf) {
  443. return nil, ErrBufferFull
  444. }
  445. for b.Available() < n && b.err == nil {
  446. b.flush()
  447. }
  448. if b.err != nil {
  449. return nil, b.err
  450. }
  451. d := b.buf[b.n : b.n+n]
  452. b.n += n
  453. return d, nil
  454. }
  455. // WriteString writes a string.
  456. // It returns the number of bytes written.
  457. // If the count is less than len(s), it also returns an error explaining
  458. // why the write is short.
  459. func (b *Writer) WriteString(s string) (int, error) {
  460. nn := 0
  461. for len(s) > b.Available() && b.err == nil {
  462. n := copy(b.buf[b.n:], s)
  463. b.n += n
  464. nn += n
  465. s = s[n:]
  466. b.flush()
  467. }
  468. if b.err != nil {
  469. return nn, b.err
  470. }
  471. n := copy(b.buf[b.n:], s)
  472. b.n += n
  473. nn += n
  474. return nn, nil
  475. }