-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeflate.go
62 lines (49 loc) · 847 Bytes
/
deflate.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
package compress
import (
"compress/flate"
"io"
"net"
)
type flusher interface {
Flush() error
}
type conn struct {
net.Conn
r io.ReadCloser
w *flate.Writer
}
func (c *conn) Read(b []byte) (int, error) {
return c.r.Read(b)
}
func (c *conn) Write(b []byte) (int, error) {
return c.w.Write(b)
}
func (c *conn) Flush() error {
if f, ok := c.Conn.(flusher); ok {
if err := f.Flush(); err != nil {
return err
}
}
return c.w.Flush()
}
func (c *conn) Close() error {
if err := c.r.Close(); err != nil {
return err
}
if err := c.w.Close(); err != nil {
return err
}
return c.Conn.Close()
}
func createDeflateConn(c net.Conn, level int) (net.Conn, error) {
r := flate.NewReader(c)
w, err := flate.NewWriter(c, level)
if err != nil {
return nil, err
}
return &conn{
Conn: c,
r: r,
w: w,
}, nil
}