-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
60 lines (53 loc) · 1.25 KB
/
encoder.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
package main
import (
"compress/gzip"
zstdcgo "github.com/DataDog/zstd"
"github.com/klauspost/compress/zstd"
"io"
)
type Encoder int
const (
Identity = iota
ZstdDefault
ZstdSpeed
ZstdCgoDefault
ZstdCgoSpeed
Gzip
)
func (e Encoder) String() string {
return [...]string{"IDENTITY", "ZSTD DEFAULT", "ZSTD BEST_SPEED", "ZSTD CGO DEFAULT", "ZSTD CGO SPEED", "GZIP"}[e]
}
var zstdEncoderDefault, zstdEncoderSpeedFastest *zstd.Encoder
func newEncoder(opts ...zstd.EOption) *zstd.Encoder {
encoder, err := zstd.NewWriter(nil, opts...)
if err != nil {
panic(err)
}
return encoder
}
func (e Encoder) NewWriter(w io.WriteCloser) io.WriteCloser {
switch e {
case Identity:
return w
case ZstdDefault:
if zstdEncoderDefault == nil {
zstdEncoderDefault = newEncoder()
}
zstdEncoderDefault.Reset(w)
return zstdEncoderDefault
case ZstdSpeed:
if zstdEncoderSpeedFastest == nil {
zstdEncoderSpeedFastest = newEncoder(zstd.WithEncoderLevel(zstd.SpeedFastest))
}
zstdEncoderSpeedFastest.Reset(w)
return zstdEncoderSpeedFastest
case ZstdCgoSpeed:
return zstdcgo.NewWriterLevel(w, zstdcgo.BestSpeed)
case ZstdCgoDefault:
return zstdcgo.NewWriterLevel(w, zstdcgo.DefaultCompression)
case Gzip:
return gzip.NewWriter(w)
default:
panic(e)
}
}