-
Notifications
You must be signed in to change notification settings - Fork 4
/
tag_gzip.go
47 lines (43 loc) · 1.07 KB
/
tag_gzip.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
package gofast
import "compress/gzip"
import "bytes"
import "fmt"
import s "github.com/bnclabs/gosettings"
func makeGzip(t *Transport, settings s.Settings) (uint64, tagfn, tagfn) {
enc := func(in, out []byte) int {
level := int(settings.Uint64("gzip.level"))
if len(in) == 0 { // empty input
return 0
}
wbuf := bytes.NewBuffer(out[:0])
writer, err := gzip.NewWriterLevel(wbuf, level)
if err != nil {
panic(fmt.Errorf("error encoding gzip: %v", err))
}
_, err = writer.Write(in)
if err != nil {
panic(fmt.Errorf("error encoding gzip: %v", err))
} else if err = writer.Flush(); err != nil {
panic(fmt.Errorf("error encoding gzip: %v", err))
}
return wbuf.Len()
}
dec := func(in, out []byte) int {
if len(in) == 0 {
return 0
}
reader, err := gzip.NewReader(bytes.NewBuffer(in))
if err != nil {
panic(fmt.Errorf("error decoding gzip: %v", err))
}
n, err := reader.Read(out)
if err != nil {
panic(fmt.Errorf("error decoding gzip: %v", err))
}
return n
}
return tagGzip, enc, dec
}
func init() {
tagFactory["gzip"] = makeGzip
}