This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathbuffer.go
167 lines (137 loc) · 2.53 KB
/
buffer.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
package amqp
import (
"encoding/binary"
"io"
)
// buffer is similar to bytes.Buffer but specialized for this package
type buffer struct {
b []byte
i int
}
func (b *buffer) next(n int64) ([]byte, bool) {
if b.readCheck(n) {
buf := b.b[b.i:len(b.b)]
b.i = len(b.b)
return buf, false
}
buf := b.b[b.i : b.i+int(n)]
b.i += int(n)
return buf, true
}
func (b *buffer) skip(n int) {
b.i += n
}
func (b *buffer) reset() {
b.b = b.b[:0]
b.i = 0
}
// reclaim shifts used buffer space to the beginning of the
// underlying slice.
func (b *buffer) reclaim() {
l := b.len()
copy(b.b[:l], b.b[b.i:])
b.b = b.b[:l]
b.i = 0
}
func (b *buffer) readCheck(n int64) bool {
return int64(b.i)+n > int64(len(b.b))
}
func (b *buffer) readByte() (byte, error) {
if b.readCheck(1) {
return 0, io.EOF
}
byte_ := b.b[b.i]
b.i++
return byte_, nil
}
func (b *buffer) readType() (amqpType, error) {
n, err := b.readByte()
return amqpType(n), err
}
func (b *buffer) peekType() (amqpType, error) {
if b.readCheck(1) {
return 0, io.EOF
}
return amqpType(b.b[b.i]), nil
}
func (b *buffer) readUint16() (uint16, error) {
if b.readCheck(2) {
return 0, io.EOF
}
n := binary.BigEndian.Uint16(b.b[b.i:])
b.i += 2
return n, nil
}
func (b *buffer) readUint32() (uint32, error) {
if b.readCheck(4) {
return 0, io.EOF
}
n := binary.BigEndian.Uint32(b.b[b.i:])
b.i += 4
return n, nil
}
func (b *buffer) readUint64() (uint64, error) {
if b.readCheck(8) {
return 0, io.EOF
}
n := binary.BigEndian.Uint64(b.b[b.i : b.i+8])
b.i += 8
return n, nil
}
func (b *buffer) readFromOnce(r io.Reader) error {
const minRead = 512
l := len(b.b)
if cap(b.b)-l < minRead {
total := l * 2
if total == 0 {
total = minRead
}
new := make([]byte, l, total)
copy(new, b.b)
b.b = new
}
n, err := r.Read(b.b[l:cap(b.b)])
b.b = b.b[:l+n]
return err
}
func (b *buffer) write(p []byte) {
b.b = append(b.b, p...)
}
func (b *buffer) writeByte(byte_ byte) {
b.b = append(b.b, byte_)
}
func (b *buffer) writeString(s string) {
b.b = append(b.b, s...)
}
func (b *buffer) len() int {
return len(b.b) - b.i
}
func (b *buffer) bytes() []byte {
return b.b[b.i:]
}
func (b *buffer) writeUint16(n uint16) {
b.b = append(b.b,
byte(n>>8),
byte(n),
)
}
func (b *buffer) writeUint32(n uint32) {
b.b = append(b.b,
byte(n>>24),
byte(n>>16),
byte(n>>8),
byte(n),
)
}
func (b *buffer) writeUint64(n uint64) {
b.b = append(b.b,
byte(n>>56),
byte(n>>48),
byte(n>>40),
byte(n>>32),
byte(n>>24),
byte(n>>16),
byte(n>>8),
byte(n),
)
}