ioutil.go 541 B

12345678910111213141516171819
  1. package ioutil
  2. import "io"
  3. // TryReadFull acts like io.ReadFull with a couple of differences:
  4. // 1. It doesn't return io.ErrUnexpectedEOF if the reader returns less data than requested.
  5. // Instead, it returns the number of bytes read and the error from the last read operation.
  6. // 2. It always returns the number of bytes read regardless of the error.
  7. func TryReadFull(r io.Reader, b []byte) (n int, err error) {
  8. var nn int
  9. toRead := len(b)
  10. for n < toRead && err == nil {
  11. nn, err = r.Read(b[n:])
  12. n += nn
  13. }
  14. return n, err
  15. }