-
Notifications
You must be signed in to change notification settings - Fork 2
/
fixed.go
144 lines (127 loc) · 2.42 KB
/
fixed.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
package bytepool
import (
stdbytes "bytes"
"io"
)
type fixed struct {
r int
length int
capacity int
bytes []byte
onExpand func()
}
func (f *fixed) Len() int {
return f.length
}
func (f *fixed) Bytes() []byte {
return f.bytes[:f.length]
}
func (f *fixed) String() string {
return string(f.Bytes())
}
func (f *fixed) write(data []byte) (bytes, int, error) {
if l := len(data); f.hasSpace(l) == false {
buf := f.toBuffer()
n, err := buf.Write(data)
return buf, n, err
}
n := copy(f.bytes[f.length:], data)
f.length += n
return f, n, nil
}
func (f *fixed) writeByte(data byte) (bytes, error) {
if f.length == f.capacity {
buf := f.toBuffer()
err := buf.WriteByte(data)
return buf, err
}
f.bytes[f.length] = data
f.length++
return f, nil
}
func (f *fixed) position(n uint) bytes {
nn := int(n)
if nn >= f.capacity {
return f.toBuffer().position(n)
}
f.length = nn
return f
}
func (f *fixed) readNFrom(expected int64, reader io.Reader) (bytes, int64, error) {
ex := int(expected)
if f.hasSpace(ex) == false {
return f.toBuffer().readNFrom(expected, reader)
}
end := f.capacity
if ex != 0 {
end = ex + f.length
}
read := 0
for {
if f.full() {
buf := f.toBuffer()
_, n, err := buf.readNFrom(expected, reader)
return buf, int64(read) + n, err
}
r, err := reader.Read(f.bytes[f.length:end])
read += r
f.length += r
if err == io.EOF || (expected != 0 && read == ex) {
return f, int64(read), err
}
if err != nil {
return f, int64(read), err
}
}
}
func (f *fixed) WriteTo(w io.Writer) (n int64, err error) {
r := f.r
l := f.length
for r < l {
n, err := w.Write(f.bytes[r:l])
if err != nil {
break
}
r += n
}
f.reset()
f.r += int(n)
return n, err
}
func (f *fixed) Read(data []byte) (int, error) {
if f.r == f.length {
return 0, io.EOF
}
n := copy(data, f.bytes[f.r:f.length])
f.r += n
if f.r == f.length {
return n, io.EOF
}
return n, nil
}
func (f *fixed) ReadByte() (byte, error) {
if f.r == f.length {
return 0, io.EOF
}
b := f.bytes[f.r]
f.r += 1
return b, nil
}
func (f *fixed) toBuffer() *buffer {
if f.onExpand != nil {
f.onExpand()
}
buf := &buffer{stdbytes.NewBuffer(f.bytes[f.r:])}
buf.Truncate(f.length - f.r)
return buf
}
func (f *fixed) hasSpace(toAdd int) bool {
return f.length+toAdd <= f.capacity
}
func (f *fixed) full() bool {
return f.length == f.capacity
}
func (f *fixed) reset() {
f.r = 0
f.length = 0
}