-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
53 lines (47 loc) · 955 Bytes
/
decoder.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
package main
import (
"compress/gzip"
zstdcgo "github.com/DataDog/zstd"
"github.com/klauspost/compress/zstd"
"io"
)
type Decoder int
const (
IdentityDecoder = iota
ZstdDecoder
ZstdCgoDecoder
GzipDecoder
)
func (d Decoder) String() string {
return [...]string{"IDENTITY", "ZSTD", "ZSTD CGO DEFAULT", "GZIP"}[d]
}
var zstdDecoder *zstd.Decoder
func (d Decoder) NewReader(r io.Reader) io.Reader {
switch d {
case IdentityDecoder:
return r
case ZstdDecoder:
// This is inherently not thread safe
if zstdDecoder == nil {
var err error
zstdDecoder, err = zstd.NewReader(nil, zstd.WithDecoderConcurrency(1))
if err != nil {
panic(err)
}
}
if err := zstdDecoder.Reset(r); err != nil {
panic(err)
}
return zstdDecoder
case ZstdCgoDecoder:
return zstdcgo.NewReader(r)
case GzipDecoder:
if reader, err := gzip.NewReader(r); err != nil {
panic(err)
} else {
return reader
}
default:
panic(d)
}
}