-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_decoder.go
53 lines (40 loc) · 984 Bytes
/
stream_decoder.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
package cypress
import "io"
// A type which uses Probe and Decoder generate Messages
type StreamDecoder struct {
r io.Reader
init bool
dec *Decoder
Header *StreamHeader
}
// Create a new StreamDecoder from the data in r
func NewStreamDecoder(r io.Reader) (*StreamDecoder, error) {
return &StreamDecoder{r: r, dec: NewDecoder(r)}, nil
}
// Probe the stream and setup the decoder to read Messages
func (s *StreamDecoder) Probe() error {
s.init = true
probe := NewProbe(s.r)
err := probe.Probe()
if err != nil {
return err
}
s.Header = probe.Header
s.dec = NewDecoder(probe.Reader())
return nil
}
// Read the next Message in the stream. If the stream has not
// been initialized, Probe() is called first.
func (s *StreamDecoder) Generate() (*Message, error) {
if !s.init {
err := s.Probe()
if err != nil {
return nil, err
}
}
return s.dec.Decode()
}
// To satisify the Generator interface
func (s *StreamDecoder) Close() error {
return nil
}