@@ -8,21 +8,22 @@ import (
8
8
// Buffer is A variable-sized buffer of bytes with Read and Write methods.
9
9
// Buffer is not thread safe for multi goroutine operation.
10
10
type Buffer struct {
11
- buf []byte // contents are the bytes buf[0 : woff] in write, are the bytes buf[roff: len(buf)] in read
12
- rpos int // read position
13
- wpos int // write position
14
- order binary.ByteOrder
15
- temp []byte
11
+ buf []byte // contents are the bytes buf[0 : wpos] in write, are the bytes buf[rpos: len(buf)] in read
12
+ rpos int // read position
13
+ wpos int // write position
14
+ order binary.ByteOrder
15
+ temp []byte
16
+ context * Context
16
17
}
17
18
18
19
// NewBuffer create A empty Buffer with initial size
19
- func NewBuffer (initsize int ) * Buffer {
20
- return NewBufferWithOrder (initsize , binary .BigEndian )
20
+ func NewBuffer (initSize int ) * Buffer {
21
+ return NewBufferWithOrder (initSize , binary .BigEndian )
21
22
}
22
23
23
24
// NewBufferWithOrder create A empty Buffer with initial size and byte order
24
- func NewBufferWithOrder (initsize int , order binary.ByteOrder ) * Buffer {
25
- return & Buffer {buf : make ([]byte , initsize ),
25
+ func NewBufferWithOrder (initSize int , order binary.ByteOrder ) * Buffer {
26
+ return & Buffer {buf : make ([]byte , initSize ),
26
27
order : order ,
27
28
temp : make ([]byte , 8 ),
28
29
}
@@ -77,11 +78,13 @@ func (b *Buffer) WriteByte(c byte) {
77
78
// Write write A byte array append the Buffer, and the wpos will increase len(bytes)
78
79
func (b * Buffer ) Write (bytes []byte ) {
79
80
l := len (bytes )
80
- if len (b .buf ) < b .wpos + l {
81
- b .grow (l )
81
+ if l > 0 {
82
+ if len (b .buf ) < b .wpos + l {
83
+ b .grow (l )
84
+ }
85
+ copy (b .buf [b .wpos :], bytes )
86
+ b .wpos += l
82
87
}
83
- copy (b .buf [b .wpos :], bytes )
84
- b .wpos += l
85
88
}
86
89
87
90
// WriteUint16 write A uint16 append the Buffer according to buffer's order
@@ -116,16 +119,16 @@ func (b *Buffer) WriteUint64(u uint64) {
116
119
117
120
// WriteZigzag32 write a uint32 append to the Buffer with zigzag algorithm
118
121
func (b * Buffer ) WriteZigzag32 (u uint32 ) int {
119
- return b .WriteVarint (uint64 ((u << 1 ) ^ uint32 (int32 (u )>> 31 )))
122
+ return b .WriteVarInt (uint64 ((u << 1 ) ^ uint32 (int32 (u )>> 31 )))
120
123
}
121
124
122
125
// WriteZigzag64 write a uint64 append to the Buffer with zigzag algorithm
123
126
func (b * Buffer ) WriteZigzag64 (u uint64 ) int {
124
- return b .WriteVarint (uint64 ((u << 1 ) ^ uint64 (int64 (u )>> 63 )))
127
+ return b .WriteVarInt (uint64 ((u << 1 ) ^ uint64 (int64 (u )>> 63 )))
125
128
}
126
129
127
- // WriteVarint write a uint64 into buffer with variable length
128
- func (b * Buffer ) WriteVarint (u uint64 ) int {
130
+ // WriteVarInt write a uint64 into buffer with variable length
131
+ func (b * Buffer ) WriteVarInt (u uint64 ) int {
129
132
l := 0
130
133
for u >= 1 << 7 {
131
134
b .WriteByte (uint8 (u & 0x7f | 0x80 ))
@@ -143,7 +146,7 @@ func (b *Buffer) grow(n int) {
143
146
b .buf = buf
144
147
}
145
148
146
- // Bytes return a bytes slice of under bytebuffer .
149
+ // Bytes return a bytes slice of under byte buffer .
147
150
func (b * Buffer ) Bytes () []byte { return b .buf [:b .wpos ] }
148
151
149
152
// Read read buffer's byte to byte array. return value n is read size.
@@ -206,28 +209,28 @@ func (b *Buffer) ReadUint64() (n uint64, err error) {
206
209
return n , nil
207
210
}
208
211
209
- // ReadZigzag64 read a zigzaged uint64 from buffer.
212
+ // ReadZigzag64 read a zigzag uint64 from buffer.
210
213
func (b * Buffer ) ReadZigzag64 () (x uint64 , err error ) {
211
- x , err = b .ReadVarint ()
214
+ x , err = b .ReadVarInt ()
212
215
if err != nil {
213
216
return
214
217
}
215
218
x = (x >> 1 ) ^ uint64 (- int64 (x & 1 ))
216
219
return
217
220
}
218
221
219
- // ReadZigzag32 read a zigzaged uint32 from buffer.
222
+ // ReadZigzag32 read a zigzag uint32 from buffer.
220
223
func (b * Buffer ) ReadZigzag32 () (x uint64 , err error ) {
221
- x , err = b .ReadVarint ()
224
+ x , err = b .ReadVarInt ()
222
225
if err != nil {
223
226
return
224
227
}
225
228
x = uint64 ((uint32 (x ) >> 1 ) ^ uint32 (- int32 (x & 1 )))
226
229
return
227
230
}
228
231
229
- // ReadVarint read a variable length uint64 form buffer
230
- func (b * Buffer ) ReadVarint () (x uint64 , err error ) {
232
+ // ReadVarInt read a variable length uint64 form buffer
233
+ func (b * Buffer ) ReadVarInt () (x uint64 , err error ) {
231
234
var temp byte
232
235
for offset := uint (0 ); offset < 64 ; offset += 7 {
233
236
temp , err = b .ReadByte ()
@@ -280,5 +283,13 @@ func (b *Buffer) Remain() int { return b.wpos - b.rpos }
280
283
// Len return the len of buffer' bytes,
281
284
func (b * Buffer ) Len () int { return b .wpos - 0 }
282
285
283
- // Cap return the capacity of the under bytebuffer
286
+ // Cap return the capacity of the under byte buffer
284
287
func (b * Buffer ) Cap () int { return cap (b .buf ) }
288
+
289
+ // GetContext get breeze context
290
+ func (b * Buffer ) GetContext () * Context {
291
+ if b .context == nil {
292
+ b .context = & Context {}
293
+ }
294
+ return b .context
295
+ }
0 commit comments