forked from simonmittag/j8a
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgzip.go
57 lines (46 loc) · 1.05 KB
/
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
48
49
50
51
52
53
54
55
56
57
package jabba
import (
"bytes"
"compress/gzip"
"io/ioutil"
"sync"
"github.com/rs/zerolog/log"
)
var gzipMagicBytes = []byte{0x1f, 0x8b}
var gzipSmall = []byte{31,139,8,0,0,0,0,0,0,255,170,174,5,4,0,0,255,255,67,191,166,163,2,0,0,0}
var zipPool = sync.Pool{
New: func() interface{} {
var buf bytes.Buffer
return gzip.NewWriter(&buf)
},
}
var unzipPool = sync.Pool{
New: func() interface{} {
buf := bytes.NewBuffer(gzipSmall)
r, _ := gzip.NewReader(buf)
return r
},
}
// Gzip a []byte
func Gzip(input []byte) []byte {
wrt, _ := zipPool.Get().(*gzip.Writer)
buf := &bytes.Buffer{}
wrt.Reset(buf)
_, _ = wrt.Write(input)
_ = wrt.Close()
defer zipPool.Put(wrt)
enc := buf.Bytes()
log.Trace().Msgf("zipped byte buffer size %d", len(enc))
return enc
}
// Gunzip a []byte
func Gunzip(input []byte) []byte {
rd, _ := unzipPool.Get().(*gzip.Reader)
buf := bytes.NewBuffer(input)
_ = rd.Reset(buf)
dec, _ := ioutil.ReadAll(rd)
_ = rd.Close()
defer unzipPool.Put(rd)
log.Trace().Msgf("unzipped byte buffer size %d", len(dec))
return dec
}