-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
io.go
187 lines (165 loc) · 4.23 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package streambuf
import (
"io"
"unicode/utf8"
)
func (b *Buffer) ioErr() error {
err := b.Err()
if err == ErrUnexpectedEOB || err == ErrNoMoreBytes {
return io.EOF
}
return err
}
func (b *Buffer) ioBufferEndError() error {
err := b.bufferEndError()
if err == ErrUnexpectedEOB || err == ErrNoMoreBytes {
return io.EOF
}
return err
}
// ReadByte reads and returns next byte from the buffer.
// If no byte is available returns either ErrNoMoreBytes (if buffer allows
// adding more bytes) or io.EOF
func (b *Buffer) ReadByte() (byte, error) {
if b.Failed() {
return 0, b.ioErr()
}
if !b.Avail(1) {
return 0, b.ioBufferEndError()
}
c := b.data[b.mark]
b.Advance(1)
return c, nil
}
// Unreads the last byte returned by most recent read operation.
func (b *Buffer) UnreadByte() error {
err := b.ioErr()
if err != nil && err != io.EOF {
return err
}
if b.mark == 0 {
return ErrOutOfRange
}
if b.mark == b.offset {
b.offset--
}
b.mark--
b.available++
return nil
}
// WriteByte appends the byte c to the buffer if buffer is not fixed.
func (b *Buffer) WriteByte(c byte) error {
p := [1]byte{c}
_, err := b.Write(p[:])
return err
}
// Read reads up to len(p) bytes into p if buffer is not in a failed state.
// Returns ErrNoMoreBytes or io.EOF (fixed buffer) if no bytes are available.
func (b *Buffer) Read(p []byte) (int, error) {
if b.Failed() {
return 0, b.ioErr()
}
if b.Len() == 0 {
return 0, b.ioBufferEndError()
}
tmp := b.Bytes()
n := copy(p, tmp)
b.Advance(n)
return n, nil
}
// Write writes p to the buffer if buffer is not fixed. Returns the number of
// bytes written or ErrOperationNotAllowed if buffer is fixed.
func (b *Buffer) Write(p []byte) (int, error) {
err := b.doAppend(p, false, -1)
if err != nil {
return 0, b.ioErr()
}
return len(p), nil
}
// ReadFrom reads data from r until error or io.EOF and appends it to the buffer.
// The amount of bytes read is returned plus any error except io.EOF.
func (b *Buffer) ReadFrom(r io.Reader) (int64, error) {
err := b.err
if err != nil && err != ErrNoMoreBytes {
return 0, b.ioErr()
}
if b.fixed {
return 0, ErrOperationNotAllowed
}
var buf [4096]byte
var total int64
for {
n, err := r.Read(buf[:])
if err != nil {
if err == io.EOF {
break
}
return total, err
}
_, err = b.Write(buf[:n])
if err != nil {
return total, err
}
total += int64(n)
}
return total, nil
}
// ReadRune reads and returns the next UTF-8-encoded Unicode code point from the
// buffer. If no bytes are available, the error returned is ErrNoMoreBytes (if
// buffer supports adding more bytes) or io.EOF. If the bytes are an erroneous
// UTF-8 encoding, it consumes one byte and returns U+FFFD, 1.
func (b *Buffer) ReadRune() (rune, int, error) {
if b.err != nil {
return 0, 0, b.ioErr()
}
if b.available == 0 {
return 0, 0, b.ioBufferEndError()
}
if c := b.data[b.mark]; c < utf8.RuneSelf {
b.Advance(1)
return rune(c), 1, nil
}
c, size := utf8.DecodeRune(b.data[b.mark:])
b.Advance(size)
return c, size, nil
}
// ReadAt reads bytes at off into p starting at the buffer its read marker.
// The read marker is not updated. If number of bytes returned is less len(p) or
// no bytes are available at off, io.EOF will be returned in err. If off is < 0,
// err is set to ErrOutOfRange.
func (b *Buffer) ReadAt(p []byte, off int64) (n int, err error) {
if b.err != nil {
return 0, b.ioErr()
}
if off < 0 {
return 0, ErrOutOfRange
}
off += int64(b.mark)
if off >= int64(len(b.data)) {
return 0, ErrOutOfRange
}
end := off + int64(len(p))
if end > int64(len(b.data)) {
err = io.EOF
end = int64(len(b.data))
}
copy(p, b.data[off:end])
return int(end - off), err
}
// WriteAt writes the content of p at off starting at recent read marker
// (already consumed bytes). Returns number of bytes written n = len(p) and err
// is nil if off and off+len(p) are within bounds, else n=0 and err is set to
// ErrOutOfRange.
func (b *Buffer) WriteAt(p []byte, off int64) (n int, err error) {
if b.err != nil {
return 0, b.ioErr()
}
end := off + int64(b.mark) + int64(len(p))
maxInt := int((^uint(0)) >> 1)
if off < 0 || end > int64(maxInt) {
return 0, ErrOutOfRange
}
// copy p into buffer
n = copy(b.sliceAt(int(off), len(p)), p)
return n, nil
}