-
Notifications
You must be signed in to change notification settings - Fork 191
/
io.go
161 lines (134 loc) · 2.84 KB
/
io.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package fakeobj
import (
"errors"
"github.com/gookit/goutil/byteutil"
)
// IOWriter only implements the io.Writer
type IOWriter struct {
Buf []byte
// ErrOnWrite return error on write, useful for testing
ErrOnWrite bool
}
// NewIOWriter instance
func NewIOWriter() *IOWriter {
return &IOWriter{
Buf: make([]byte, 0, 1024),
}
}
// Write implements
func (w *IOWriter) Write(p []byte) (n int, err error) {
if w.ErrOnWrite {
return 0, errors.New("fake write error")
}
w.Buf = append(w.Buf, p...)
return len(p), nil
}
// Reset the buffer
func (w *IOWriter) Reset() {
w.Buf = w.Buf[:0]
}
// Reader implements the io.Reader, io.Closer
type Reader struct {
byteutil.Buffer
// ErrOnRead return error on read, useful for testing
ErrOnRead bool
}
// NewReader instance
func NewReader() *Reader {
return &Reader{}
}
// NewStrReader instance
func NewStrReader(s string) *Reader {
buf := byteutil.NewBuffer()
buf.WriteStr1(s)
return &Reader{
Buffer: *buf,
}
}
// SetErrOnRead mark
func (r *Reader) SetErrOnRead() {
r.ErrOnRead = true
}
// Read implements the io.Reader
func (r *Reader) Read(p []byte) (n int, err error) {
if r.ErrOnRead {
return 0, errors.New("fake read error")
}
return r.Buffer.Read(p)
}
// Writer implements the io.Writer, stdio.Flusher, io.Closer.
type Writer struct {
byteutil.Buffer
// ErrOnWrite return error on write, useful for testing
ErrOnWrite bool
// ErrOnFlush return error on flush, useful for testing
ErrOnFlush bool
// ErrOnSync return error on flush, useful for testing
ErrOnSync bool
// ErrOnClose return error on close, useful for testing
ErrOnClose bool
}
// NewBuffer instance. alias of NewWriter()
func NewBuffer() *Writer {
return NewWriter()
}
// NewWriter instance
func NewWriter() *Writer {
return &Writer{}
}
// SetErrOnWrite method
func (w *Writer) SetErrOnWrite() *Writer {
w.ErrOnWrite = true
return w
}
// SetErrOnFlush method
func (w *Writer) SetErrOnFlush() *Writer {
w.ErrOnFlush = true
return w
}
// SetErrOnSync method
func (w *Writer) SetErrOnSync() *Writer {
w.ErrOnSync = true
return w
}
// SetErrOnClose method
func (w *Writer) SetErrOnClose() *Writer {
w.ErrOnClose = true
return w
}
// ResetGet buffer string.
func (w *Writer) ResetGet() string {
s := w.String()
w.Reset()
return s
}
// Write implements
func (w *Writer) Write(p []byte) (n int, err error) {
if w.ErrOnWrite {
return 0, errors.New("fake write error")
}
return w.Buffer.Write(p)
}
// Close implements
func (w *Writer) Close() error {
if w.ErrOnClose {
return errors.New("fake close error")
}
return nil
}
// Flush implements stdio.Flusher
func (w *Writer) Flush() error {
if w.ErrOnFlush {
return errors.New("fake flush error")
}
w.Reset()
return nil
}
// Sync implements stdio.Syncer
func (w *Writer) Sync() error {
if w.ErrOnSync {
return errors.New("fake sync error")
}
w.Reset()
return nil
}