-
Notifications
You must be signed in to change notification settings - Fork 0
/
countwriter.go
43 lines (35 loc) · 1.07 KB
/
countwriter.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
package akhttpd
import "io"
// spellchecker:words akhttpd
// CountWriter is an io.Writer that wraps an underlying writer.
// It is not safe for concurrent writing.
//
// It counts the the total number of bytes written.
// Furthermore, once a single write fails, all future writes are silently suppressed.
type CountWriter struct {
Writer io.Writer
count int
err error
}
// Write writes b into Writer.
func (w *CountWriter) Write(b []byte) (int, error) {
if w.err != nil {
return len(b), nil
}
n, err := w.Writer.Write(b)
w.count += n
w.err = err
return n, err
}
// State returns the first error that occurred within the writer and the total number of bytes written up to that point.
func (w CountWriter) State() (int, error) {
return w.StateWith(nil)
}
// StateWith returns the first error that occurred within the writer and the total number of bytes written up to that point.
// When err is not nil, returns the provided error instead of the internal error.
func (w CountWriter) StateWith(err error) (int, error) {
if err == nil {
err = w.err
}
return w.count, err
}