-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzstd_writer.go
More file actions
42 lines (35 loc) · 952 Bytes
/
Copy pathzstd_writer.go
File metadata and controls
42 lines (35 loc) · 952 Bytes
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
package warc
import (
"fmt"
"io"
"github.com/klauspost/compress/zstd"
)
// sizedZstdWriter wraps a zstd encoder and remembers its current output writer
// so callers can set Frame_Content_Size before writing a record.
type sizedZstdWriter struct {
*zstd.Encoder
output io.Writer
}
func newSizedZstdWriter(w io.Writer, dictionary []byte) (*sizedZstdWriter, error) {
opts := []zstd.EOption{
zstd.WithEncoderLevel(zstd.SpeedBetterCompression),
}
if len(dictionary) > 0 {
opts = append(opts, zstd.WithEncoderDict(dictionary))
}
enc, err := zstd.NewWriter(w, opts...)
if err != nil {
return nil, fmt.Errorf("creating zstd writer: %w", err)
}
return &sizedZstdWriter{Encoder: enc, output: w}, nil
}
func (w *sizedZstdWriter) Reset(output io.Writer) {
w.output = output
w.Encoder.Reset(output)
}
func (w *sizedZstdWriter) SetContentSize(size int64) {
if w.output == nil {
return
}
w.Encoder.ResetContentSize(w.output, size)
}