1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
11 func NewCountingWriter(w io.Writer, f func(uint64)) io.WriteCloser {
12 return &countingReadWriter{
18 func NewCountingReader(r io.Reader, f func(uint64)) io.ReadCloser {
19 return &countingReadWriter{
25 func NewCountingReaderAtSeeker(r readerAtSeeker, f func(uint64)) *countingReaderAtSeeker {
26 return &countingReaderAtSeeker{readerAtSeeker: r, counter: f}
29 type countingReadWriter struct {
35 func (crw *countingReadWriter) Read(buf []byte) (int, error) {
36 n, err := crw.reader.Read(buf)
37 crw.counter(uint64(n))
41 func (crw *countingReadWriter) Write(buf []byte) (int, error) {
42 n, err := crw.writer.Write(buf)
43 crw.counter(uint64(n))
47 func (crw *countingReadWriter) Close() error {
48 if c, ok := crw.writer.(io.Closer); ok {
54 type readerAtSeeker interface {
59 type countingReaderAtSeeker struct {
64 func (crw *countingReaderAtSeeker) Read(buf []byte) (int, error) {
65 n, err := crw.readerAtSeeker.Read(buf)
66 crw.counter(uint64(n))
70 func (crw *countingReaderAtSeeker) ReadAt(buf []byte, off int64) (int, error) {
71 n, err := crw.readerAtSeeker.ReadAt(buf, off)
72 crw.counter(uint64(n))