-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbyte_buffer.go
230 lines (202 loc) · 4.88 KB
/
byte_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package network
import (
"encoding/binary"
)
const (
cheapPrependSize = 8
initialSize = 1024
)
// ByteBuffer 字节buff
type ByteBuffer struct {
mBuffer []byte
mCapacity int32
readIndex int32
writeIndex int32
reservedPrependSize int32
littleEndian bool
}
// NewByteBuffer 创建一个字节buffer
func NewByteBuffer() *ByteBuffer {
return &ByteBuffer{
mBuffer: make([]byte, cheapPrependSize+initialSize),
mCapacity: cheapPrependSize + initialSize,
readIndex: cheapPrependSize,
writeIndex: cheapPrependSize,
reservedPrependSize: cheapPrependSize,
littleEndian: true,
}
}
// SetByteOrder It's dangerous to call the method on reading or writing
func (bf *ByteBuffer) SetByteOrder(littleEndian bool) {
bf.littleEndian = littleEndian
}
// Length ...
func (bf *ByteBuffer) Length() int32 {
return bf.writeIndex - bf.readIndex
}
// Swap ...
func (bf *ByteBuffer) Swap(other *ByteBuffer) {
}
// Skip advances the reading index of the buffer
func (bf *ByteBuffer) Skip(len int32) {
if len < bf.Length() {
bf.readIndex = bf.readIndex + len
} else {
bf.Reset()
}
}
// Retrieve ...
func (bf *ByteBuffer) Retrieve(len int32) {
bf.Skip(len)
}
// Reset ...
func (bf *ByteBuffer) Reset() {
bf.Truncate(0)
}
// Truncate ...
func (bf *ByteBuffer) Truncate(n int32) {
if n == 0 {
bf.readIndex = bf.reservedPrependSize
bf.writeIndex = bf.reservedPrependSize
} else if bf.writeIndex > (bf.readIndex + n) {
bf.writeIndex = bf.readIndex + n
}
}
// Reserve ...
func (bf *ByteBuffer) Reserve(len int32) {
if bf.mCapacity >= len+bf.reservedPrependSize {
return
}
bf.grow(len + bf.reservedPrependSize)
}
// Append ...
func (bf *ByteBuffer) Append(buff []byte) {
size := len(buff)
if size == 0 {
return
}
bf.write(buff, int32(size))
}
// AppendInt64 ...
func (bf *ByteBuffer) AppendInt64(x int64) {
buff := make([]byte, 8)
if bf.littleEndian {
binary.LittleEndian.PutUint64(buff, uint64(x))
} else {
binary.BigEndian.PutUint64(buff, uint64(x))
}
bf.write(buff, 8)
}
// AppendInt32 ...
func (bf *ByteBuffer) AppendInt32(x int32) {
buff := make([]byte, 4)
if bf.littleEndian {
binary.LittleEndian.PutUint32(buff, uint32(x))
} else {
binary.BigEndian.PutUint32(buff, uint32(x))
}
bf.write(buff, 4)
}
// AppendInt16 ...
func (bf *ByteBuffer) AppendInt16(x int16) {
buff := make([]byte, 2)
if bf.littleEndian {
binary.LittleEndian.PutUint16(buff, uint16(x))
} else {
binary.BigEndian.PutUint16(buff, uint16(x))
}
bf.write(buff, 2)
}
// ReadInt64 ...
func (bf *ByteBuffer) ReadInt64() int64 {
buff := bf.mBuffer[bf.readIndex : bf.readIndex+8]
var result uint64
if bf.littleEndian {
result = binary.LittleEndian.Uint64(buff)
} else {
result = binary.BigEndian.Uint64(buff)
}
bf.Skip(8)
return int64(result)
}
// ReadInt32 ...
func (bf *ByteBuffer) ReadInt32() int32 {
buff := bf.mBuffer[bf.readIndex : bf.readIndex+4]
var result uint32
if bf.littleEndian {
result = binary.LittleEndian.Uint32(buff)
} else {
result = binary.BigEndian.Uint32(buff)
}
bf.Skip(4)
return int32(result)
}
// ReadInt16 ...
func (bf *ByteBuffer) ReadInt16() int16 {
buff := bf.mBuffer[bf.readIndex : bf.readIndex+2]
var result uint16
if bf.littleEndian {
result = binary.LittleEndian.Uint16(buff)
} else {
result = binary.BigEndian.Uint16(buff)
}
bf.Skip(2)
return int16(result)
}
// NextBytes 读取N字节
func (bf *ByteBuffer) NextBytes(len int32) []byte {
msgData := bf.mBuffer[bf.readIndex : bf.readIndex+len]
bf.readIndex += len
return msgData
}
// EnsureWritableBytes ...
func (bf *ByteBuffer) EnsureWritableBytes(len int32) {
if bf.writableBytes() < len {
bf.grow(len)
}
}
func (bf *ByteBuffer) grow(len int32) {
if bf.writableBytes()+bf.prependableBytes() < len+bf.reservedPrependSize {
newCap := (bf.mCapacity << 1) + len
buff := make([]byte, newCap)
copy(buff, bf.mBuffer)
bf.mCapacity = newCap
bf.mBuffer = buff
} else {
readable := bf.Length()
copy(bf.mBuffer[bf.reservedPrependSize:], bf.mBuffer[bf.readIndex:bf.writeIndex])
bf.readIndex = bf.reservedPrependSize
bf.writeIndex = bf.readIndex + readable
}
}
func (bf *ByteBuffer) write(buff []byte, len int32) {
bf.EnsureWritableBytes(len)
copy(bf.mBuffer[bf.writeIndex:], buff)
bf.writeIndex = bf.writeIndex + len
}
func (bf *ByteBuffer) writableBytes() int32 {
return bf.mCapacity - bf.writeIndex
}
func (bf *ByteBuffer) prependableBytes() int32 {
return bf.readIndex
}
// WriteBuff ...
func (bf *ByteBuffer) WriteBuff() []byte {
buffLen := int32(len(bf.mBuffer))
if bf.writeIndex >= buffLen {
return nil
}
return bf.mBuffer[bf.writeIndex:]
}
// ReadBuff ...
func (bf *ByteBuffer) ReadBuff() []byte {
buffLen := int32(len(bf.mBuffer))
if bf.readIndex >= buffLen {
return nil
}
return bf.mBuffer[bf.readIndex:]
}
// WriteBytes 写入n字节
func (bf *ByteBuffer) WriteBytes(n int32) {
bf.writeIndex += n
}