forked from niubaoshu/gotiny
-
Notifications
You must be signed in to change notification settings - Fork 3
/
compress.go
95 lines (84 loc) · 2.08 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
package gotiny
import (
"bytes"
"compress/gzip"
"io"
"sync"
)
var (
Gzip = GzipPool{
readers: sync.Pool{},
writers: sync.Pool{},
bufferPool: sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
}
)
// GzipPool manages a pool of gzip.Writer.
// The pool uses sync.Pool internally.
type GzipPool struct {
readers sync.Pool
writers sync.Pool
bufferPool sync.Pool
}
// get a buffer from pool
func (pool *GzipPool) Getbuffer() *bytes.Buffer {
b := pool.bufferPool.Get().(*bytes.Buffer)
b.Reset()
return b
}
// put back a buffer to the pool
func (pool *GzipPool) Putbuffer(b *bytes.Buffer) {
pool.bufferPool.Put(b)
}
// GetReader returns gzip.Reader from the pool, or creates a new one
// if the pool is empty.
func (pool *GzipPool) GetReader(src io.Reader) (reader *gzip.Reader) {
if r := pool.readers.Get(); r != nil {
reader = r.(*gzip.Reader)
reader.Reset(src)
} else {
reader, _ = gzip.NewReader(src)
}
return reader
}
// PutReader closes and returns a gzip.Reader to the pool
// so that it can be reused via GetReader.
func (pool *GzipPool) PutReader(reader *gzip.Reader) {
reader.Close()
pool.readers.Put(reader)
}
// GetWriter returns gzip.Writer from the pool, or creates a new one
// with gzip.BestCompression if the pool is empty.
func (pool *GzipPool) GetWriter(dst io.Writer) (writer *gzip.Writer) {
if w := pool.writers.Get(); w != nil {
writer = w.(*gzip.Writer)
writer.Reset(dst)
} else {
return gzip.NewWriter(dst)
}
return writer
}
// PutWriter closes and returns a gzip.Writer to the pool
// so that it can be reused via GetWriter.
func (pool *GzipPool) PutWriter(writer *gzip.Writer) {
writer.Close()
pool.writers.Put(writer)
}
// Gziper compress input and write to output
func Gziper(outPut *bytes.Buffer, inPut []byte) error {
gz := Gzip.GetWriter(outPut)
defer Gzip.PutWriter(gz)
defer gz.Flush()
gz.Write(inPut)
return nil
}
// Gunziper unzip input and write to output
func Gunziper(outPut *bytes.Buffer, input *bytes.Buffer) error {
gz := Gzip.GetReader(input)
defer Gzip.PutReader(gz)
io.Copy(outPut, gz)
return nil
}