readfill.go 426 B

12345678910111213141516171819
  1. package readers
  2. import "io"
  3. // ReadFill reads as much data from r into buf as it can
  4. //
  5. // It reads until the buffer is full or r.Read returned an error.
  6. //
  7. // This is io.ReadFull but when you just want as much data as
  8. // possible, not an exact size of block.
  9. func ReadFill(r io.Reader, buf []byte) (n int, err error) {
  10. var nn int
  11. for n < len(buf) && err == nil {
  12. nn, err = r.Read(buf[n:])
  13. n += nn
  14. }
  15. return n, err
  16. }