-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_bytes.go
146 lines (126 loc) · 2.6 KB
/
buffer_bytes.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
package anet
import (
"errors"
)
func NewBytesBuffer(cap int) ReadWriter {
return &bytesBuffer{
buffer: make([]byte, 0, cap),
start: 0,
end: 0,
cap: cap,
}
}
type bytesBuffer struct {
buffer []byte
start int
end int
cap int
}
var _ Reader = &bytesBuffer{}
var _ Writer = &bytesBuffer{}
var _ ReadWriter = &bytesBuffer{}
func (b *bytesBuffer) Seek(n int) ([]byte, error) {
if b.Len() < n {
return nil, errors.New("not enough data in buffer")
}
data := b.buffer[b.start : b.start+n]
return data, nil
}
func (b *bytesBuffer) SeekAck(n int) error {
if b.Len() < n {
return errors.New("not enough data in buffer")
}
b.start += n
return nil
}
func (b *bytesBuffer) SeekAll() ([]byte, error) {
data := b.buffer[b.start:b.end]
return data, nil
}
func (b *bytesBuffer) ReadAll() ([]byte, error) {
data := b.buffer[b.start:b.end]
b.start = 0
b.end = 0
return data, nil
}
func (b *bytesBuffer) ReadUtil(delim byte) ([]byte, error) {
panic("unreachable code")
}
func (b *bytesBuffer) ReadBytes(n int) ([]byte, error) {
if b.Len() < n {
return nil, errors.New("not enough data in buffer")
}
data := b.buffer[b.start : b.start+n]
b.start += n
return data, nil
}
func (b *bytesBuffer) ReadString(n int) (string, error) {
if b.Len() < n {
return "", errors.New("not enough data in buffer")
}
data := b.buffer[b.start : b.start+n]
b.start += n
return string(data), nil
}
func (b *bytesBuffer) Len() int {
return b.end - b.start
}
func (b *bytesBuffer) Release() {
if b.start == b.end {
b.start = 0
b.end = 0
return
}
if b.start > 0 {
copy(b.buffer, b.buffer[b.start:b.end])
b.end -= b.start
b.start = 0
}
}
func (b *bytesBuffer) Book(n int) []byte {
if b.remain() < n {
b.increase(n)
}
return b.buffer[b.end : b.end+n]
}
func (b *bytesBuffer) BookAck(n int) error {
if b.remain() < n {
return errors.New("not enough space in buffer")
}
b.end += n
return nil
}
func (b *bytesBuffer) WriteBytes(data []byte, n int) error {
if b.remain() < n {
b.increase(n)
}
copy(b.buffer[b.end:b.end+n], data)
b.end += n
return nil
}
func (b *bytesBuffer) WriteString(data string, n int) error {
if b.remain() < n {
b.increase(n)
}
copy(b.buffer[b.end:b.end+n], []byte(data))
b.end += n
return nil
}
func (b *bytesBuffer) Flush() error {
panic("unreachable code")
}
func (b *bytesBuffer) increase(n int) {
if b.cap < n {
b.cap = n * 2
} else {
b.cap = b.cap * 2
}
newBuffer := make([]byte, b.end, b.cap)
copy(newBuffer, b.buffer[b.start:b.end])
b.buffer = newBuffer
b.end -= b.start
b.start = 0
}
func (b *bytesBuffer) remain() int {
return b.cap - b.end
}