-
Notifications
You must be signed in to change notification settings - Fork 2
/
flate.go
56 lines (45 loc) · 954 Bytes
/
flate.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
package mse6
import (
"bytes"
"github.com/klauspost/compress/flate"
"io"
"io/ioutil"
"sync"
)
const flateLevel int = 1
var flateEmpty = []byte{0}
var deflatePool = sync.Pool{
New: func() interface{} {
var buf bytes.Buffer
w, _ := flate.NewWriter(&buf, flateLevel)
return w
},
}
var inflatePool = sync.Pool{
New: func() interface{} {
buf := bytes.NewBuffer(flateEmpty)
r := flate.NewReader(buf)
return r
},
}
//Flate compress a []byte
func Deflate(input []byte) *[]byte {
wrt, _ := deflatePool.Get().(*flate.Writer)
buf := &bytes.Buffer{}
wrt.Reset(buf)
_, _ = wrt.Write(input)
_ = wrt.Close()
defer deflatePool.Put(wrt)
enc := buf.Bytes()
return &enc
}
// Inflate a []byte
func Inflate(input []byte) *[]byte {
rd, _ := inflatePool.Get().(io.ReadCloser)
buf := bytes.NewBuffer(input)
_ = rd.(flate.Resetter).Reset(buf, nil)
dec, _ := ioutil.ReadAll(rd)
_ = rd.Close()
defer inflatePool.Put(rd)
return &dec
}