-
Notifications
You must be signed in to change notification settings - Fork 5
/
countingreader.go
58 lines (52 loc) · 1.26 KB
/
countingreader.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
54
55
56
57
58
package arcreader
import (
"io"
"sync/atomic"
)
// CountingReader counts the bytes read through it.
type CountingReader struct {
bytesRead int64
maxBytes int64
ioReader io.Reader
}
// NewCountingReader makes a new CountingReader that counts the bytes
// read through it.
func NewCountingReader(r io.Reader) *CountingReader {
return &CountingReader{
ioReader: r,
maxBytes: -1,
}
}
// NewLimitedCountingReader makes a new CountingReader that counts the bytes
// read through it.
//
// When maxBytes bytes are read, the next read will
// return io.EOF even though the underlying reader has more data.
func NewLimitedCountingReader(r io.Reader, maxBytes int64) *CountingReader {
return &CountingReader{
ioReader: r,
maxBytes: maxBytes,
}
}
func (r *CountingReader) Read(p []byte) (n int, err error) {
if r.maxBytes >= 0 {
remaining := r.maxBytes - r.N()
if remaining <= 0 {
return 0, io.EOF
}
if int64(len(p)) > remaining {
p = p[:remaining]
}
n, err = r.ioReader.Read(p)
atomic.AddInt64(&r.bytesRead, int64(n))
} else {
n, err = r.ioReader.Read(p)
atomic.AddInt64(&r.bytesRead, int64(n))
}
return
}
// N gets the number of bytes that have been read
// so far.
func (r *CountingReader) N() int64 {
return atomic.LoadInt64(&r.bytesRead)
}