ReadAllWithEstimate is a fork of https://go.googlesource.com/go/+/go1.16.3/src/io/io.go#626 with a starting buffer size. if none is provided it uses the existing default of 512
(r io.Reader, estimatedBytes int64)
| 8 | // ReadAllWithEstimate is a fork of https://go.googlesource.com/go/+/go1.16.3/src/io/io.go#626 |
| 9 | // with a starting buffer size. if none is provided it uses the existing default of 512 |
| 10 | func ReadAllWithEstimate(r io.Reader, estimatedBytes int64) ([]byte, error) { |
| 11 | if estimatedBytes <= 0 { |
| 12 | estimatedBytes = 512 |
| 13 | } |
| 14 | |
| 15 | b := make([]byte, 0, estimatedBytes+1) // if the calling code knows the exact bytes needed the below logic will do one extra allocation unless we add 1 |
| 16 | for { |
| 17 | if len(b) == cap(b) { |
| 18 | // Add more capacity (let append pick how much). |
| 19 | b = append(b, 0)[:len(b)] |
| 20 | } |
| 21 | n, err := r.Read(b[len(b):cap(b)]) |
| 22 | b = b[:len(b)+n] |
| 23 | if err != nil { |
| 24 | if errors.Is(err, io.EOF) { |
| 25 | err = nil |
| 26 | } |
| 27 | return b, err |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // ReadAllWithBuffer is a fork of https://go.googlesource.com/go/+/go1.16.3/src/io/io.go#626 |
| 33 | // with a buffer to read into. if the provided buffer is not large enough it will be extended |