...

Source file src/github.com/dsoprea/go-utility/v2/filesystem/write_counter.go

Documentation: github.com/dsoprea/go-utility/v2/filesystem

     1  package rifs
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // WriteCounter proxies write requests and maintains a counter of bytes written.
     8  type WriteCounter struct {
     9  	w       io.Writer
    10  	counter int
    11  }
    12  
    13  // NewWriteCounter returns a new `WriteCounter` struct wrapping a `Writer`.
    14  func NewWriteCounter(w io.Writer) *WriteCounter {
    15  	return &WriteCounter{
    16  		w: w,
    17  	}
    18  }
    19  
    20  // Count returns the total number of bytes read.
    21  func (wc *WriteCounter) Count() int {
    22  	return wc.counter
    23  }
    24  
    25  // Reset resets the counter to zero.
    26  func (wc *WriteCounter) Reset() {
    27  	wc.counter = 0
    28  }
    29  
    30  // Write forwards a write to the underlying `Writer` while bumping the counter.
    31  func (wc *WriteCounter) Write(b []byte) (n int, err error) {
    32  	n, err = wc.w.Write(b)
    33  	wc.counter += n
    34  
    35  	return n, err
    36  }
    37  

View as plain text