-
Notifications
You must be signed in to change notification settings - Fork 4
/
read_writer.go
61 lines (55 loc) · 1.16 KB
/
read_writer.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
package crt
import "io"
// ConcurrentRW is a concurrent read/write buffer via channels.
type ConcurrentRW struct {
input chan []byte
output chan []byte
}
// NewConcurrentRW creates a new concurrent read/write buffer.
func NewConcurrentRW() *ConcurrentRW {
return &ConcurrentRW{
input: make(chan []byte, 10),
output: make(chan []byte),
}
}
// Write writes data to the buffer.
func (rw *ConcurrentRW) Write(p []byte) (n int, err error) {
data := make([]byte, len(p))
copy(data, p)
rw.input <- data
return len(data), nil
}
// Read reads data from the buffer.
func (rw *ConcurrentRW) Read(p []byte) (n int, err error) {
data, ok := <-rw.output
if !ok {
return 0, io.EOF
}
n = copy(p, data)
return n, nil
}
// Run starts the concurrent read/write buffer.
func (rw *ConcurrentRW) Run() {
const bufferSize = 1024
buf := make([]byte, 0, bufferSize)
for {
select {
case data, ok := <-rw.input:
if !ok {
close(rw.output)
return
}
buf = append(buf, data...)
for len(buf) > 0 {
n := len(buf)
if n > bufferSize {
n = bufferSize
}
p := make([]byte, n)
copy(p, buf[:n])
buf = buf[n:]
rw.output <- p
}
}
}
}