-
Notifications
You must be signed in to change notification settings - Fork 0
/
zstd.go
54 lines (49 loc) · 1.25 KB
/
zstd.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
package bstates
import (
"bytes"
"io"
"github.com/klauspost/compress/zstd"
"github.com/nayarsystems/buffer/buffer"
)
// ZstdEnc compresses the provided buffer using Zstandard (Zstd) compression
// and returns a new buffer containing the compressed data.
func ZstdEnc(b *buffer.Buffer) (*buffer.Buffer, error) {
buf := new(bytes.Buffer)
wr, err := zstd.NewWriter(buf, zstd.WithEncoderConcurrency(1))
if err != nil {
return nil, err
}
_, err = wr.Write(b.GetRawBuffer()[:b.GetByteSize()])
wr.Close()
if err != nil {
return nil, err
}
var out []byte
out, err = io.ReadAll(buf)
if err != nil {
return nil, err
}
outb := &buffer.Buffer{}
outb.InitFromRawBuffer(out)
return outb, nil
}
// ZstdDec decompresses the provided buffer which is expected to be in
// Zstandard (Zstd) format and returns a new buffer containing the
// decompressed data.
func ZstdDec(b *buffer.Buffer) (*buffer.Buffer, error) {
r := bytes.NewReader(b.GetRawBuffer()[:b.GetByteSize()])
var gzr *zstd.Decoder
gzr, err := zstd.NewReader(r, zstd.WithDecoderConcurrency(1))
if err != nil {
return nil, err
}
var out []byte
out, err = io.ReadAll(gzr)
gzr.Close()
if err != nil {
return nil, err
}
outb := &buffer.Buffer{}
outb.InitFromRawBuffer(out)
return outb, nil
}