源码

// ReadFrom reads data from r until EOF and appends it to the buffer, growing// the buffer as needed. The return value n is the number of bytes read. Any// error except io.EOF encountered during the read is also returned. If the// buffer becomes too large, ReadFrom will panic with ErrTooLarge.func(b*Buffer)ReadFrom(rio.Reader)(nint64,errerror){b.lastRead=opInvalid// If buffer is empty, reset to recover space.ifb.off>=len(b.buf){b.Truncate(0)}for{iffree:=cap(b.buf)-len(b.buf);free<MinRead{// not enough space at endnewBuf:=b.bufifb.off+free<MinRead{// not enough space using beginning of buffer;// double buffer capacitynewBuf=makeSlice(2*cap(b.buf)+MinRead)}copy(newBuf,b.buf[b.off:])b.buf=newBuf[:len(b.buf)-b.off]b.off=0}m,e:=r.Read(b.buf[len(b.buf):cap(b.buf)])b.buf=b.buf[0:len(b.buf)+m]n+=int64(m)ife==io.EOF{break}ife!=nil{returnn,e}}returnn,nil// err is EOF, so return nil explicitly}