Peek returns the next n bytes without advancing the reader. Peek appends results to the provided res slice and returns the updated slice. This pattern allows re-using the storage of res if it has sufficient capacity. The returned subslices are views into the underlying buffers and are only valid u
(n int, res [][]byte)
| 325 | // |
| 326 | // If Peek returns fewer than n bytes, it also returns an error. |
| 327 | func (r *Reader) Peek(n int, res [][]byte) ([][]byte, error) { |
| 328 | for i := 0; n > 0 && i < len(r.data); i++ { |
| 329 | curData := r.data[i].ReadOnlyData() |
| 330 | start := 0 |
| 331 | if i == 0 { |
| 332 | start = r.bufferIdx |
| 333 | } |
| 334 | curSize := min(n, len(curData)-start) |
| 335 | if curSize == 0 { |
| 336 | continue |
| 337 | } |
| 338 | res = append(res, curData[start:start+curSize]) |
| 339 | n -= curSize |
| 340 | } |
| 341 | if n > 0 { |
| 342 | return nil, fmt.Errorf("insufficient bytes in reader") |
| 343 | } |
| 344 | return res, nil |
| 345 | } |