This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
/
zlib.go
84 lines (71 loc) · 1.54 KB
/
zlib.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package compression
import (
"bytes"
"compress/zlib"
"encoding/binary"
"io"
"go.ytsaurus.tech/library/go/core/xerrors"
)
type CodecZlib int
func (c CodecZlib) Compress(block []byte) ([]byte, error) {
var out bytes.Buffer
if err := binary.Write(&out, binary.LittleEndian, uint64(len(block))); err != nil {
return nil, err
}
w, err := zlib.NewWriterLevel(&out, int(c))
if err != nil {
return nil, err
}
if _, err := io.Copy(w, bytes.NewReader(block)); err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func (c CodecZlib) Decompress(block []byte) ([]byte, error) {
if len(block) < 8 {
return nil, xerrors.Errorf("invalid block length: %d", len(block))
}
decompressedSize := binary.LittleEndian.Uint64(block[:8])
r, err := zlib.NewReader(bytes.NewReader(block[8:]))
if err != nil {
return nil, err
}
var out bytes.Buffer
if _, err := io.Copy(&out, r); err != nil {
return nil, err
}
if err := r.Close(); err != nil {
return nil, err
}
if decompressedSize != uint64(out.Len()) {
return nil, xerrors.Errorf("decompressed data size mismatch: expected %d, got %d",
decompressedSize, out.Len())
}
return out.Bytes(), nil
}
func (c CodecZlib) GetID() CodecID {
switch c {
case 1:
return CodecIDZlib1
case 2:
return CodecIDZlib2
case 3:
return CodecIDZlib3
case 4:
return CodecIDZlib4
case 5:
return CodecIDZlib5
case 6:
return CodecIDZlib6
case 7:
return CodecIDZlib7
case 8:
return CodecIDZlib8
case 9:
return CodecIDZlib9
}
return 0
}