Codebase list golang-github-vbauerster-mpb / 3ac6abb proxyreader.go
3ac6abb

Tree @3ac6abb (Download .tar.gz)

proxyreader.go @3ac6abbraw · history · blame

package mpb

import (
	"io"
	"time"
)

// Reader is io.Reader wrapper, for proxy read bytes
type Reader struct {
	io.Reader
	bar        *Bar
	sbChannels []chan<- time.Time
}

func (r *Reader) Read(p []byte) (int, error) {
	now := time.Now()
	for _, ch := range r.sbChannels {
		ch <- now
	}
	n, err := r.Reader.Read(p)
	r.bar.IncrBy(n)
	return n, err
}

// Close the reader when it implements io.Closer
func (r *Reader) Close() error {
	if closer, ok := r.Reader.(io.Closer); ok {
		return closer.Close()
	}
	return nil
}