github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/readers/reader_bytes_counter.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package readers
     4  
     5  import "io"
     6  
     7  type BytesCounterReader struct {
     8  	rawReader io.Reader
     9  	count     int64
    10  }
    11  
    12  func NewBytesCounterReader(rawReader io.Reader) *BytesCounterReader {
    13  	return &BytesCounterReader{
    14  		rawReader: rawReader,
    15  	}
    16  }
    17  
    18  func (this *BytesCounterReader) Read(p []byte) (n int, err error) {
    19  	n, err = this.rawReader.Read(p)
    20  	this.count += int64(n)
    21  	return
    22  }
    23  
    24  func (this *BytesCounterReader) TotalBytes() int64 {
    25  	return this.count
    26  }