/
io_compress.go
64 lines (58 loc) · 1.3 KB
/
io_compress.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
package recorder
import (
"compress/zlib"
"context"
"fmt"
"io"
)
var (
errFormat = fmt.Errorf("invalid foramt")
)
// NewReaderWithCompress return io.ReadCloser that will be save the stream with delay information.
// will compress and need to be closed manually.
func NewReaderWithCompress(ctx context.Context, r io.Reader) (io.ReadCloser, error) {
zrc, err := zlib.NewReader(r)
if err != nil {
if err == zlib.ErrChecksum || err == zlib.ErrDictionary || err == zlib.ErrHeader {
err = errFormat
}
return nil, err
}
ctx, cancel := context.WithCancel(ctx)
r = NewReader(ctx, zrc)
rc := struct {
io.Reader
io.Closer
}{
Reader: r,
Closer: withCloser(func() error {
err := zrc.Close()
cancel()
return err
}),
}
return rc, nil
}
// NewWriterWithCompress return io.WriteCloser that will be reproduce the stream with delay information.
// will compress and need to be closed manually.
func NewWriterWithCompress(ctx context.Context, w io.Writer) io.WriteCloser {
zwc := zlib.NewWriter(w)
ctx, cancel := context.WithCancel(ctx)
w = NewWriter(ctx, zwc)
wc := struct {
io.Writer
io.Closer
}{
Writer: w,
Closer: withCloser(func() error {
err := zwc.Close()
cancel()
return err
}),
}
return wc
}
type withCloser func() error
func (w withCloser) Close() error {
return w()
}