Peek returns the next n bytes without advancing the reader. The bytes stop being valid at the next read call. If Peek returns fewer than n bytes, it also returns an error explaining why the read is short. The error is ErrBufferFull if n is larger than b's buffer size.
(n int)
| 126 | // also returns an error explaining why the read is short. The error is |
| 127 | // ErrBufferFull if n is larger than b's buffer size. |
| 128 | func (b *Reader) Peek(n int) ([]byte, error) { |
| 129 | if n < 0 { |
| 130 | return nil, ErrNegativeCount |
| 131 | } |
| 132 | if n > len(b.buf) { |
| 133 | return nil, ErrBufferFull |
| 134 | } |
| 135 | // 0 <= n <= len(b.buf) |
| 136 | for b.w-b.r < n && b.err == nil { |
| 137 | b.fill() // b.w-b.r < len(b.buf) => buffer is not full |
| 138 | } |
| 139 | |
| 140 | var err error |
| 141 | if avail := b.w - b.r; avail < n { |
| 142 | // not enough data in buffer |
| 143 | n = avail |
| 144 | err = b.readErr() |
| 145 | if err == nil { |
| 146 | err = ErrBufferFull |
| 147 | } |
| 148 | } |
| 149 | return b.buf[b.r : b.r+n], err |
| 150 | } |
| 151 | |
| 152 | // Pop returns the next n bytes with advancing the reader. The bytes stop |
| 153 | // being valid at the next read call. If Pop returns fewer than n bytes, it |