github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/lib/readers/counting_reader.go (about)

     1  package readers
     2  
     3  import "io"
     4  
     5  // NewCountingReader returns a CountingReader, which will read from the given
     6  // reader while keeping track of how many bytes were read.
     7  func NewCountingReader(in io.Reader) *CountingReader {
     8  	return &CountingReader{in: in}
     9  }
    10  
    11  // CountingReader holds a reader and a read count of how many bytes were read
    12  // so far.
    13  type CountingReader struct {
    14  	in   io.Reader
    15  	read uint64
    16  }
    17  
    18  // Read reads from the underlying reader.
    19  func (cr *CountingReader) Read(b []byte) (int, error) {
    20  	n, err := cr.in.Read(b)
    21  	cr.read += uint64(n)
    22  	return n, err
    23  }
    24  
    25  // BytesRead returns how many bytes were read from the underlying reader so far.
    26  func (cr *CountingReader) BytesRead() uint64 {
    27  	return cr.read
    28  }