-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuff.go
More file actions
49 lines (43 loc) · 946 Bytes
/
buff.go
File metadata and controls
49 lines (43 loc) · 946 Bytes
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
package gopdf
// Buff for pdf content
type Buff struct {
position int
datas []byte
}
// Write : write []byte to buffer
func (b *Buff) Write(p []byte) (int, error) {
needed := b.position + len(p)
if needed > len(b.datas) {
if needed > cap(b.datas) {
// grow with exponential strategy to avoid O(n²) behavior
newCap := cap(b.datas) * 2
if newCap < needed {
newCap = needed
}
newData := make([]byte, needed, newCap)
copy(newData, b.datas)
b.datas = newData
} else {
b.datas = b.datas[:needed]
}
}
copy(b.datas[b.position:], p)
b.position += len(p)
return len(p), nil
}
// Len : len of buffer
func (b *Buff) Len() int {
return len(b.datas)
}
// Bytes : get bytes
func (b *Buff) Bytes() []byte {
return b.datas
}
// Position : get current position
func (b *Buff) Position() int {
return b.position
}
// SetPosition : set current position
func (b *Buff) SetPosition(pos int) {
b.position = pos
}