aboutsummaryrefslogtreecommitdiff
path: root/LimitedWriter.go
blob: 4d23322354d37b2c651fadbf5980bbe954313946 (plain)
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
package kagus

import (
	"errors"
	"io"
)

var (
	WriteLimitReached = errors.New("Write limit reached")
)

// LimitWriter wraps around a io.Writer and limits the amount of bytes that can be written to it.
type LimitedWriter struct {
	W io.Writer
	N int64
}

// Write writes at most lw.N bytes to LimitWriter. It updates lw.N on sucessful write operations.
// If the limit was reached, the error WriteLimitReached will be returned.
func (lw *LimitedWriter) Write(p []byte) (int, error) {
	towrite := int64(len(p))
	if lw.N < towrite {
		towrite = lw.N
	}

	n, err := lw.W.Write(p[:towrite])
	if err != nil {
		lw.N = 0
		return n, err
	}

	if n == len(p) {
		lw.N -= int64(n)
		return n, nil
	}

	lw.N = 0
	return n, WriteLimitReached
}