-
Notifications
You must be signed in to change notification settings - Fork 1
/
probe.go
57 lines (45 loc) · 1.11 KB
/
probe.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
package cypress
import "io"
// A type which can look at a reader and detect the format
type Probe struct {
r io.Reader
Header *StreamHeader
Stream io.Reader
}
// Create a new Probe from the data in r
func NewProbe(r io.Reader) *Probe {
return &Probe{
r: r,
Stream: r,
Header: new(StreamHeader),
}
}
// Inspect the stream and populate Header with the data
func (p *Probe) Probe() error {
buf := pbBufPool.Get().([]byte)
_, err := p.r.Read(buf[:1])
if err != nil {
pbBufPool.Put(buf)
return err
}
b := buf[0]
pbBufPool.Put(buf)
if b != '-' {
p.Stream = &bytePeekReader{b: b, r: p.Stream}
return nil
}
return p.Header.UnmarshalFrom(p.r)
}
// Indicate the compression in use
func (p *Probe) Compression() StreamHeader_Compression {
return p.Header.GetCompression()
}
// Create an io.Reader for the remainder of the stream
func (p *Probe) Reader() io.Reader {
return ReadCompressed(p.Stream, p.Compression())
}
// Create an io.Writer that will match the parameters of the probed
// stream.
func (p *Probe) Writer(w io.WriteCloser) io.WriteCloser {
return WriteCompressed(w, p.Compression())
}