-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstream.go
56 lines (46 loc) · 860 Bytes
/
stream.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
package bring
import (
"bytes"
"encoding/base64"
"image"
)
type onEndFunc func(s *stream)
type stream struct {
buffer *bytes.Buffer
onEnd onEndFunc
}
func (s *stream) image() (image.Image, error) {
dec := base64.NewDecoder(base64.StdEncoding, s.buffer)
img, _, err := image.Decode(dec)
return img, err
}
type streams map[int]*stream
func newStreams() streams {
return make(map[int]*stream)
}
func (ss streams) get(id int) *stream {
if s, ok := ss[id]; ok {
return s
}
s := &stream{
buffer: &bytes.Buffer{},
}
ss[id] = s
return s
}
func (ss streams) append(id int, data string) error {
s := ss.get(id)
_, err := s.buffer.WriteString(data)
return err
}
func (ss streams) end(id int) {
s := ss.get(id)
if s.onEnd != nil {
s.onEnd(s)
}
}
func (ss streams) delete(id int) {
ss[id].buffer = nil
ss[id] = nil
delete(ss, id)
}