github.com/aergoio/aergo@v1.3.1/p2p/p2putil/ioutil.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2putil
     7  
     8  import "io"
     9  
    10  // ReadToLen read bytes from io.Reader until bf is filled.
    11  func ReadToLen(rd io.Reader, bf []byte) (int, error) {
    12  	remain := len(bf)
    13  	offset := 0
    14  	for remain > 0 {
    15  		read, err := rd.Read(bf[offset:])
    16  		if err != nil || read == 0 {
    17  			return offset, err
    18  		}
    19  		remain -= read
    20  		offset += read
    21  	}
    22  	return offset, nil
    23  }