-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreader.go
More file actions
30 lines (25 loc) · 708 Bytes
/
creader.go
File metadata and controls
30 lines (25 loc) · 708 Bytes
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
package cachefs
import "io"
// countingReader is a reader that counts how many bytes have been read.
// It was created to be used with gob and groupcache so that we can
// encode results as well as return a byte stream.
type countingReader struct {
io.Reader
count int
}
// Read implements io.Reader.
func (c *countingReader) Read(buf []byte) (int, error) {
n, err := c.Reader.Read(buf)
c.count += n
return n, err
}
// ReadByte implements io.ByteReader so that gob doesn't buffer.
func (c *countingReader) ReadByte() (byte, error) {
var buf [1]byte
_, err := c.Read(buf[:])
return buf[0], err
}
// Count returns the number of bytes read.
func (c *countingReader) Count() int {
return c.count
}