-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompress.go
157 lines (138 loc) · 2.65 KB
/
compress.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package utils
import (
"compress/gzip"
"compress/zlib"
"io/ioutil"
"sync"
"github.com/fufuok/utils/pools/bufferpool"
"github.com/fufuok/utils/pools/readerpool"
)
var (
gzipWritePool = newGzipWriterPool()
zlibWritePool = newZlibWriterPool()
gzipReaderPool = sync.Pool{
New: func() interface{} {
return new(gzip.Reader)
},
}
)
func Gzip(data []byte) ([]byte, error) {
return GzipLevel(data, gzip.BestSpeed)
}
func GzipLevel(data []byte, level int) (dst []byte, err error) {
buf := bufferpool.Get()
idx := getWriterPoolIndex(level)
zw := gzipWritePool[idx].Get().(*gzip.Writer)
zw.Reset(buf)
defer func() {
bufferpool.Put(buf)
gzipWritePool[idx].Put(zw)
}()
_, err = zw.Write(data)
if err != nil {
return
}
err = zw.Flush()
if err != nil {
return
}
err = zw.Close()
if err != nil {
return
}
dst = CopyBytes(buf.Bytes())
return
}
func Ungzip(data []byte) (src []byte, err error) {
rData := readerpool.New(data)
zr := gzipReaderPool.Get().(*gzip.Reader)
defer func() {
readerpool.Release(rData)
gzipReaderPool.Put(zr)
}()
err = zr.Reset(rData)
if err != nil {
return
}
defer func() {
_ = zr.Close()
}()
src, err = ioutil.ReadAll(zr)
if err != nil {
return
}
return
}
func Zip(data []byte) ([]byte, error) {
return ZipLevel(data, zlib.BestSpeed)
}
func ZipLevel(data []byte, level int) (dst []byte, err error) {
buf := bufferpool.Get()
idx := getWriterPoolIndex(level)
zw := zlibWritePool[idx].Get().(*zlib.Writer)
zw.Reset(buf)
defer func() {
bufferpool.Put(buf)
zlibWritePool[idx].Put(zw)
}()
_, err = zw.Write(data)
if err != nil {
return
}
err = zw.Flush()
if err != nil {
return
}
err = zw.Close()
if err != nil {
return
}
dst = CopyBytes(buf.Bytes())
return
}
func Unzip(data []byte) (src []byte, err error) {
rData := readerpool.New(data)
defer readerpool.Release(rData)
zr, err := zlib.NewReader(rData)
if err != nil {
return nil, err
}
defer func() {
_ = zr.Close()
}()
src, err = ioutil.ReadAll(zr)
if err != nil {
return
}
return
}
func newZlibWriterPool() (pools []*sync.Pool) {
for i := 0; i < 12; i++ {
level := i - 2
pools = append(pools, &sync.Pool{
New: func() interface{} {
zw, _ := zlib.NewWriterLevel(nil, level)
return zw
},
})
}
return
}
func newGzipWriterPool() (pools []*sync.Pool) {
for i := 0; i < 12; i++ {
level := i - 2
pools = append(pools, &sync.Pool{
New: func() interface{} {
zw, _ := gzip.NewWriterLevel(nil, level)
return zw
},
})
}
return
}
func getWriterPoolIndex(level int) int {
if level < gzip.HuffmanOnly || level > gzip.BestCompression {
level = gzip.DefaultCompression
}
return level + 2
}