/
compress.go
133 lines (126 loc) · 3.09 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
// Copyright (C) 2020 - 2023 iDigitalFlame
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
package wrapper
import (
"compress/gzip"
"compress/zlib"
"io"
"sync"
)
const compLevel = zlib.BestSpeed
const (
// Zlib is the default Zlib Wrapper. This wrapper uses the default compression level. Use the 'NewZlib'
// function to create a wrapper with a different level.
Zlib = compress(0x0)
// Gzip is the default Gzip Wrapper. This wrapper uses the default compression level. Use the 'NewGzip'
// function to create a wrapper with a different level.
Gzip = compress(0x1)
)
var (
zlibWriterPool = sync.Pool{
New: func() interface{} {
w, _ := zlib.NewWriterLevel(nil, compLevel)
return w
},
}
gzipWriterPool = sync.Pool{
New: func() interface{} {
w, _ := gzip.NewWriterLevel(nil, compLevel)
return w
},
}
zlibReaderPool, gzipReaderPool sync.Pool
)
type compress uint8
type reader struct {
_ [0]func()
p *sync.Pool
io.ReadCloser
}
type writer struct {
_ [0]func()
p *sync.Pool
io.WriteCloser
}
func (r *reader) Close() error {
if r.ReadCloser == nil {
return nil
}
err := r.ReadCloser.Close()
r.p.Put(r.ReadCloser)
r.ReadCloser, r.p = nil, nil
return err
}
func (w *writer) Close() error {
if w.WriteCloser == nil {
return nil
}
err := w.WriteCloser.Close()
w.p.Put(w.WriteCloser)
w.WriteCloser, w.p = nil, nil
return err
}
func (c compress) Unwrap(r io.Reader) (io.Reader, error) {
switch c {
case Zlib:
c := zlibReaderPool.Get()
if c == nil {
n, err := zlib.NewReader(r)
if err != nil {
return nil, err
}
return &reader{ReadCloser: n, p: &zlibReaderPool}, nil
}
var ()
if err := c.(zlib.Resetter).Reset(r, nil); err != nil {
zlibReaderPool.Put(c)
return nil, err
}
return &reader{ReadCloser: c.(io.ReadCloser), p: &zlibReaderPool}, nil
case Gzip:
c := gzipReaderPool.Get()
if c == nil {
n, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
return &reader{ReadCloser: n, p: &gzipReaderPool}, nil
}
var (
n = c.(*gzip.Reader)
err = n.Reset(r)
)
if err == nil {
return &reader{ReadCloser: n, p: &gzipReaderPool}, nil
}
gzipReaderPool.Put(c)
return nil, err
}
return r, nil
}
func (c compress) Wrap(w io.WriteCloser) (io.WriteCloser, error) {
switch c {
case Zlib:
c := zlibWriterPool.Get().(*zlib.Writer)
c.Reset(w)
return &writer{WriteCloser: c, p: &zlibWriterPool}, nil
case Gzip:
c := gzipWriterPool.Get().(*gzip.Writer)
c.Reset(w)
return &writer{WriteCloser: c, p: &gzipWriterPool}, nil
}
return w, nil
}