- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbytebuffer.go
More file actions
Latest commit
108 lines (92 loc) · 2.16 KB
/
Copy pathbytebuffer.go
File metadata and controls
108 lines (92 loc) · 2.16 KB
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
package gonet
const (
presize=0
initsize=10
)
typeByteBufferstruct {
_buffer []byte
_prependSizeint
_readerIndexint
_writerIndexint
}
funcNewByteBuffer() *ByteBuffer {
return&ByteBuffer{
_buffer: make([]byte, presize+initsize),
_prependSize: presize,
_readerIndex: presize,
_writerIndex: presize,
}
}
func (this*ByteBuffer) Append(buff...byte) {
size:=len(buff)
ifsize==0 {
return
}
this.WrGrow(size)
copy(this._buffer[this._writerIndex:], buff)
this.WrFlip(size)
}
func (this*ByteBuffer) WrBuf() []byte {
ifthis._writerIndex>=len(this._buffer) {
returnnil
}
returnthis._buffer[this._writerIndex:]
}
func (this*ByteBuffer) WrSize() int {
returnlen(this._buffer) -this._writerIndex
}
func (this*ByteBuffer) WrFlip(sizeint) {
this._writerIndex+=size
}
func (this*ByteBuffer) WrGrow(sizeint) {
ifsize>this.WrSize() {
this.wrreserve(size)
}
}
func (this*ByteBuffer) RdBuf() []byte {
ifthis._readerIndex>=len(this._buffer) {
returnnil
}
returnthis._buffer[this._readerIndex:]
}
func (this*ByteBuffer) RdReady() bool {
returnthis._writerIndex>this._readerIndex
}
func (this*ByteBuffer) RdSize() int {
returnthis._writerIndex-this._readerIndex
}
func (this*ByteBuffer) RdFlip(sizeint) {
ifsize<this.RdSize() {
this._readerIndex+=size
} else {
this.Reset()
}
}
func (this*ByteBuffer) Reset() {
this._readerIndex=this._prependSize
this._writerIndex=this._prependSize
}
func (this*ByteBuffer) MaxSize() int {
returnlen(this._buffer)
}
func (this*ByteBuffer) wrreserve(sizeint) {
ifthis.WrSize()+this._readerIndex<size+this._prependSize {
tmpbuff:=make([]byte, this._writerIndex+size)
copy(tmpbuff, this._buffer)
this._buffer=tmpbuff
} else {
readable:=this.RdSize()
copy(this._buffer[this._prependSize:], this._buffer[this._readerIndex:this._writerIndex])
this._readerIndex=this._prependSize
this._writerIndex=this._readerIndex+readable
}
}
func (this*ByteBuffer) Prepend(buff []byte) bool {
size:=len(buff)
ifthis._readerIndex<size {
returnfalse
}
this._readerIndex-=size
copy(this._buffer[this._readerIndex:], buff)
returntrue
}