-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
173 lines (165 loc) · 3.89 KB
/
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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package buffer
import (
"errors"
"io"
"sync"
)
var ErrNodeEmpty = errors.New("empty")
var ErrBlockEmpty = errors.New("block empty")
var ErrBlockFull = errors.New("full")
var ErrBufferInputStreamHasFinish = errors.New("input stream finished")
type Block struct {
data []byte
totalWrite int
totalRead int
next *Block
blockSize int
mux sync.RWMutex
}
type Buffer struct {
pool *sync.Pool
head *Block
tail *Block
options *Options
inputStreamEnd bool
}
func NewBuffer(options ...Option) *Buffer {
buffer := &Buffer{}
buffer.options = loadOptions(options...)
buffer.fix()
blockSize := buffer.options.blockSize
node := &Block{data: make([]byte, blockSize), blockSize: blockSize}
buffer.head = node
buffer.tail = node
buffer.pool.New = func() interface{} {
return &Block{data: make([]byte, blockSize), blockSize: blockSize}
}
return buffer
}
func (buffer *Buffer) fix() {
if buffer.options.blockSize == 0 {
buffer.options.blockSize = 4096
}
if buffer.options.pool != nil {
buffer.pool = buffer.options.pool
} else {
buffer.pool = &sync.Pool{}
}
}
func (buffer *Buffer) InputStreamFinish() {
buffer.inputStreamEnd = true
}
func (buffer *Buffer) ReOpenInputStream() {
buffer.inputStreamEnd = false
}
func (buffer *Buffer) Read(data []byte) (readLen int, err error) {
readLen = 0
defer func() {
if err == nil && readLen == 0 {
err = buffer.options.emptyError
return
}
}()
for buffer.head != nil && readLen < len(data) {
buffer.head.mux.Lock()
rd, err := buffer.head.Read(data[readLen:])
readLen += rd
if err != nil {
if buffer.inputStreamEnd && buffer.head == buffer.tail && readLen == 0 {
buffer.head.mux.Unlock()
return 0, io.EOF
}
if err == ErrBlockEmpty {
if buffer.head.next == nil {
buffer.head.mux.Unlock()
return readLen, nil
}
_head := buffer.head
buffer.head = buffer.head.next
_head.close()
_head.mux.Unlock()
buffer.pool.Put(_head)
continue
}
if err == ErrNodeEmpty {
buffer.head.mux.Unlock()
return readLen, nil
}
return 0, err
}
buffer.head.mux.Unlock()
}
return readLen, nil
}
func (buffer *Buffer) Write(data []byte) (int, error) {
if buffer.inputStreamEnd {
return 0, ErrBufferInputStreamHasFinish
}
writeLen := 0
for buffer.tail != nil && writeLen < len(data) {
buffer.tail.mux.Lock()
wd, err := buffer.tail.Write(data[writeLen:])
if err != nil {
if err == ErrBlockFull {
newNode := buffer.pool.Get().(*Block)
_tail := buffer.tail
buffer.tail.next = newNode
buffer.tail = newNode
_tail.mux.Unlock()
continue
}
return 0, err
}
writeLen += wd
buffer.tail.mux.Unlock()
}
return writeLen, nil
}
func (buffer *Buffer) IsEmpty() bool {
if buffer.tail == buffer.head && buffer.tail.totalRead == buffer.tail.totalWrite {
return true
}
return false
}
func (bNode *Block) Read(data []byte) (int, error) {
if bNode.totalRead == bNode.blockSize {
return 0, ErrBlockEmpty
}
reset := bNode.totalWrite - bNode.totalRead
if reset == 0 {
return 0, ErrNodeEmpty
}
needRead := len(data)
if reset > needRead {
copy(data, bNode.data[bNode.totalRead:bNode.totalRead+needRead])
bNode.totalRead += needRead
return needRead, nil
} else {
copy(data, bNode.data[bNode.totalRead:bNode.totalWrite])
bNode.totalRead += reset
return reset, nil
}
}
func (bNode *Block) Write(data []byte) (int, error) {
needWrite := len(data)
alreadyWriten := bNode.totalWrite
resetWrite := bNode.blockSize - alreadyWriten
if resetWrite == 0 {
return 0, ErrBlockFull
}
if resetWrite > needWrite {
copy(bNode.data[alreadyWriten:], data)
bNode.totalWrite += needWrite
return needWrite, nil
} else {
copy(bNode.data[alreadyWriten:], data[:resetWrite])
bNode.totalWrite += resetWrite
return resetWrite, nil
}
}
func (bNode *Block) close() {
bNode.totalRead = 0
bNode.totalWrite = 0
bNode.next = nil
return
}