-
Notifications
You must be signed in to change notification settings - Fork 2
/
brotli.go
54 lines (43 loc) · 1010 Bytes
/
brotli.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
package mse6
import (
"bytes"
"github.com/andybalholm/brotli"
"io/ioutil"
"sync"
)
const brotliLevel int = 1
var brotliEmpty = []byte{0}
var brotliEncPool = sync.Pool{
New: func() interface{} {
var buf bytes.Buffer
return brotli.NewWriterLevel(&buf, brotliLevel)
},
}
var brotliDecPool = sync.Pool{
New: func() interface{} {
buf := bytes.NewBuffer(brotliEmpty)
r := brotli.NewReader(buf)
return r
},
}
// BrotliEncode encodes to brotli from byte array.
func BrotliEncode(input []byte) *[]byte {
wrt, _ := brotliEncPool.Get().(*brotli.Writer)
buf := &bytes.Buffer{}
wrt.Reset(buf)
_, _ = wrt.Write(input)
_ = wrt.Close()
defer brotliEncPool.Put(wrt)
enc := buf.Bytes()
return &enc
}
// BrotliDecode decodes a []byte from Brotli binary format
func BrotliDecode(input []byte) *[]byte {
rd, _ := brotliDecPool.Get().(*brotli.Reader)
buf := bytes.NewBuffer(input)
_ = rd.Reset(buf)
dec, _ := ioutil.ReadAll(rd)
_ = rd.Reset(buf)
defer brotliDecPool.Put(rd)
return &dec
}