forked from TyphoonMC/TyphoonCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
45 lines (38 loc) · 930 Bytes
/
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
package typhoon
type VarBuffer struct {
used int
buffer []byte
}
func newVarBuffer(size int) *VarBuffer {
return &VarBuffer{
used: 0,
buffer: make([]byte, size),
}
}
func array_cpy(dst []byte, dstindex int, src []byte, srcindex int, length int) {
copy(dst[dstindex:dstindex+length], src[srcindex:srcindex+length])
}
func (buff *VarBuffer) Len() int {
return buff.used
}
func (buff *VarBuffer) Bytes() []byte {
if len(buff.buffer) == buff.used {
return buff.buffer
} else {
return buff.buffer[:buff.used]
}
}
func (buff *VarBuffer) Write(p []byte) (n int, err error) {
if len(buff.buffer)-buff.used < len(p) {
size := len(p) + len(buff.buffer)
nbuffer := make([]byte, size)
copy(nbuffer, buff.buffer)
buff.buffer = nbuffer
}
array_cpy(buff.buffer, buff.used, p, 0, len(p))
buff.used += len(p)
return len(p), nil
}
func (buff *VarBuffer) Read(p []byte) (n int, err error) {
return 0, nil
}