2022-06-08 18:47:36 +00:00
|
|
|
package bytecounter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Writer allows to count written bytes.
|
|
|
|
type Writer struct {
|
|
|
|
w io.Writer
|
|
|
|
count uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWriter allocates a Writer.
|
|
|
|
func NewWriter(w io.Writer) *Writer {
|
|
|
|
return &Writer{
|
|
|
|
w: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements io.Writer.
|
|
|
|
func (w *Writer) Write(p []byte) (int, error) {
|
|
|
|
n, err := w.w.Write(p)
|
|
|
|
w.count += uint32(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns written bytes.
|
|
|
|
func (w Writer) Count() uint32 {
|
|
|
|
return w.count
|
|
|
|
}
|
2022-07-16 10:42:48 +00:00
|
|
|
|
|
|
|
// SetCount sets written bytes.
|
|
|
|
func (w *Writer) SetCount(v uint32) {
|
|
|
|
w.count = v
|
|
|
|
}
|