aboutsummaryrefslogtreecommitdiff
path: root/CountWriter.go
diff options
context:
space:
mode:
Diffstat (limited to 'CountWriter.go')
-rw-r--r--CountWriter.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/CountWriter.go b/CountWriter.go
new file mode 100644
index 0000000..d942a8e
--- /dev/null
+++ b/CountWriter.go
@@ -0,0 +1,21 @@
+package kagus
+
+import (
+ "io"
+)
+
+// CountWriter wraps around an io.Writer and counts the amount of bytes written.
+type CountWriter struct {
+ W io.Writer
+ N int64 // Total number of bytes written
+}
+
+func NewCountWriter(w io.Writer) *CountWriter {
+ return &CountWriter{W: w}
+}
+
+func (cw *CountWriter) Write(p []byte) (int, error) {
+ n, err := cw.W.Write(p)
+ cw.N += int64(n)
+ return n, err
+}