forked from tdewolff/test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readers.go
78 lines (62 loc) · 1.78 KB
/
readers.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package test
import "io"
// PlainReader implements an io.Reader and wraps over an existing io.Reader to hide other functions it implements.
type PlainReader struct {
r io.Reader
}
// NewPlainReader returns a new PlainReader.
func NewPlainReader(r io.Reader) *PlainReader {
return &PlainReader{r}
}
// Read implements the io.Reader interface.
func (r *PlainReader) Read(p []byte) (int, error) {
return r.r.Read(p)
}
////////////////////////////////////////////////////////////////
// ErrorReader implements an io.Reader that will do N successive reads before it returns ErrPlain.
type ErrorReader struct {
n int
}
// NewErrorReader returns a new ErrorReader.
func NewErrorReader(n int) *ErrorReader {
return &ErrorReader{n}
}
// Read implements the io.Reader interface.
func (r *ErrorReader) Read(b []byte) (n int, err error) {
if len(b) == 0 {
return 0, nil
}
if r.n == 0 {
return 0, ErrPlain
}
r.n--
b[0] = '.'
return 1, nil
}
////////////////////////////////////////////////////////////////
// InfiniteReader implements an io.Reader that will always read-in one character.
type InfiniteReader struct{}
// NewInfiniteReader returns a new InfiniteReader.
func NewInfiniteReader() *InfiniteReader {
return &InfiniteReader{}
}
// Read implements the io.Reader interface.
func (r *InfiniteReader) Read(b []byte) (n int, err error) {
if len(b) == 0 {
return 0, nil
}
b[0] = '.'
return 1, nil
}
////////////////////////////////////////////////////////////////
// EmptyReader implements an io.Reader that will always return 0, nil.
type EmptyReader struct {
}
// NewEmptyReader returns a new EmptyReader.
func NewEmptyReader() *EmptyReader {
return &EmptyReader{}
}
// Read implements the io.Reader interface.
func (r *EmptyReader) Read(b []byte) (n int, err error) {
return 0, io.EOF
}