Read is a fork of [io.LimitedReader.Read] that returns an error when limit exceeded.
(p []byte)
| 13 | |
| 14 | // Read is a fork of [io.LimitedReader.Read] that returns an error when limit exceeded. |
| 15 | func (l *limitedReader) Read(p []byte) (n int, err error) { |
| 16 | if l.N < 0 { |
| 17 | return 0, errors.New("read exceeds the defined limit") |
| 18 | } |
| 19 | if l.N == 0 { |
| 20 | return 0, io.EOF |
| 21 | } |
| 22 | // have to cap N + 1 otherwise we won't hit limit err |
| 23 | if int64(len(p)) > l.N+1 { |
| 24 | p = p[0 : l.N+1] |
| 25 | } |
| 26 | n, err = l.R.Read(p) |
| 27 | l.N -= int64(n) |
| 28 | return n, err |
| 29 | } |
no outgoing calls