-
Notifications
You must be signed in to change notification settings - Fork 17
/
compress.go
41 lines (35 loc) · 1.1 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
package compress
import (
"fmt"
"io"
"strings"
)
type Compress interface {
Read(buf []byte) (int, error)
Write(buf []byte) (int, error)
Close() error
}
type NewCompressReaderFunc func(r io.ReadCloser) (Compress, error)
type NewCompressWriterFunc func(w io.WriteCloser) (Compress, error)
var compressReaderFuncMap map[string]NewCompressReaderFunc
var compressWriterFuncMap map[string]NewCompressWriterFunc
func registerCompress(name string, rf NewCompressReaderFunc, wf NewCompressWriterFunc) {
name = strings.ToLower(name)
if compressReaderFuncMap == nil {
compressReaderFuncMap = make(map[string]NewCompressReaderFunc)
}
if compressWriterFuncMap == nil {
compressWriterFuncMap = make(map[string]NewCompressWriterFunc)
}
compressReaderFuncMap[name] = rf
compressWriterFuncMap[name] = wf
}
func GetCompress(name string) (NewCompressReaderFunc, NewCompressWriterFunc, error) {
name = strings.ToLower(name)
rf, rfOk := compressReaderFuncMap[name]
wf, wfOk := compressWriterFuncMap[name]
if !wfOk || !rfOk {
return nil, nil, fmt.Errorf("compress method %s not found", name)
}
return rf, wf, nil
}