Files
kopia/cas/counting_reader.go
2016-03-31 05:49:39 -07:00

27 lines
472 B
Go

package cas
import (
"io"
"sync/atomic"
)
// countingReader wraps an io.ReadCloser and counts bytes read.
type countingReader struct {
io.ReadCloser
counter *int64
}
func (cr *countingReader) Read(b []byte) (int, error) {
n, err := cr.ReadCloser.Read(b)
atomic.AddInt64(cr.counter, int64(n))
return n, err
}
func newCountingReader(source io.ReadCloser, counter *int64) io.ReadCloser {
return &countingReader{
ReadCloser: source,
counter: counter,
}
}