-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.go
81 lines (71 loc) · 1.51 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package connector
import(
"io"
"net"
"compress/flate"
"fmt"
"github.com/golang/snappy"
)
type CompressType byte
const (
// CompressNone represents no compression
CompressNone CompressType = iota
// CompressFlate represents zip
CompressFlate
// CompressSnappy represents snappy
CompressSnappy
)
type writeFlusher struct {
w *flate.Writer
}
func (wf *writeFlusher) Write(p []byte) (int, error) {
n, err := wf.w.Write(p)
if err != nil {
return n, err
}
if err := wf.w.Flush(); err != nil {
return 0, err
}
return n, nil
}
type CompressConn struct {
net.Conn
r io.Reader
w io.Writer
compressType CompressType
}
func (c *CompressConn) Read(b []byte) (n int, err error) {
return c.r.Read(b)
}
func (c *CompressConn) Write(b []byte) (n int, err error) {
return c.w.Write(b)
}
func (c *CompressConn) Close() error {
return c.Conn.Close()
}
func NewCompressConn(conn net.Conn, compressType CompressType) net.Conn {
cc := &CompressConn{Conn: conn}
r := io.Reader(cc.Conn)
switch compressType {
case CompressNone:
case CompressFlate:
r = flate.NewReader(r)
case CompressSnappy:
r = snappy.NewReader(r)
}
cc.r = r
w := io.Writer(cc.Conn)
switch compressType {
case CompressNone:
case CompressFlate:
zw, err := flate.NewWriter(w, flate.DefaultCompression)
if err != nil {
panic(fmt.Sprintf("BUG: flate.NewWriter(%d) returned non-nil err: %s", flate.DefaultCompression, err))
}
w = &writeFlusher{w: zw}
case CompressSnappy:
w = snappy.NewWriter(w)
}
cc.w = w
return cc
}