-
Notifications
You must be signed in to change notification settings - Fork 56
/
byteslice.go
81 lines (70 loc) · 2.12 KB
/
byteslice.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
package bpool
// WrapByteSlice wraps a []byte as a ByteSlice
func WrapByteSlice(full []byte, headerLength int) ByteSlice {
return ByteSlice{
full: full,
current: full[headerLength:],
head: headerLength,
end: len(full),
}
}
// ByteSlice provides a wrapper around []byte with some added convenience
type ByteSlice struct {
full []byte
current []byte
head int
end int
}
// ResliceTo reslices the end of the current slice.
func (b ByteSlice) ResliceTo(end int) ByteSlice {
return ByteSlice{
full: b.full,
current: b.current[:end],
head: b.head,
end: b.head + end,
}
}
// Bytes returns the current slice
func (b ByteSlice) Bytes() []byte {
return b.current
}
// BytesWithHeader returns the current slice preceded by the header
func (b ByteSlice) BytesWithHeader() []byte {
return b.full[:b.end]
}
// Full returns the full original buffer underlying the ByteSlice
func (b ByteSlice) Full() []byte {
return b.full
}
// ByteSlicePool is a bool of byte slices
type ByteSlicePool interface {
// Get gets a byte slice from the pool
GetSlice() ByteSlice
// Put returns a byte slice to the pool
PutSlice(ByteSlice)
// NumPooled returns the number of currently pooled items
NumPooled() int
}
// NewByteSlicePool creates a new ByteSlicePool bounded to the
// given maxSize, with new byte arrays sized based on width
func NewByteSlicePool(maxSize int, width int) ByteSlicePool {
return NewHeaderPreservingByteSlicePool(maxSize, width, 0)
}
// NewHeaderPreservingByteSlicePool creates a new ByteSlicePool bounded to the
// given maxSize, with new byte arrays sized based on width and headerLength
// preserved at the beginning of the slice.
func NewHeaderPreservingByteSlicePool(maxSize int, width int, headerLength int) ByteSlicePool {
return &BytePool{
c: make(chan []byte, maxSize),
w: width + headerLength,
h: headerLength,
}
}
// GetSlice implements the method from interface ByteSlicePool
func (bp *BytePool) GetSlice() ByteSlice {
return WrapByteSlice(bp.Get(), bp.h)
}
// PutSlice implements the method from interface ByteSlicePool
func (bp *BytePool) PutSlice(b ByteSlice) {
bp.Put(b.Full())
}