-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiosizer.go
53 lines (46 loc) · 1.25 KB
/
iosizer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package iosizer
import (
"io"
"math"
"sync/atomic"
)
// SizeReadWriter implements io methods keeping total size metrics.
type SizeReadWriter struct {
total atomic.Uint64
rdr io.Reader
wtr io.Writer
}
// NewSizeReadWriter constructs a read/writer with size metrics.
func NewSizeReadWriter(rdr io.Reader, writer io.Writer) *SizeReadWriter {
return &SizeReadWriter{rdr: rdr, wtr: writer}
}
// TotalSize returns the total amount of data transferred.
func (s *SizeReadWriter) TotalSize() uint64 {
return s.total.Load()
}
// Read reads data from the source.
func (s *SizeReadWriter) Read(p []byte) (n int, err error) {
if s.rdr == nil {
return 0, io.EOF
}
n, err = s.rdr.Read(p)
// G115: Protect against integer overflow by checking n <= math.MaxUint32 before conversion to uint64
// G115: Protect against integer overflow by checking n <= math.MaxUint32 before conversion to uint64
if n > 0 && n <= math.MaxUint32 {
s.total.Add(uint64(n))
}
return
}
// Write writes data to the writer.
func (s *SizeReadWriter) Write(p []byte) (n int, err error) {
if s.wtr == nil {
return 0, io.EOF
}
n, err = s.wtr.Write(p)
if n > 0 && n <= math.MaxUint32 {
s.total.Add(uint64(n))
}
return
}
// _ is a type assertion
var _ io.ReadWriter = ((*SizeReadWriter)(nil))