-
Notifications
You must be signed in to change notification settings - Fork 9
/
decompressor.go
69 lines (62 loc) · 3.03 KB
/
decompressor.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
package libdeflate
import "github.com/4kills/go-libdeflate/native"
// Decompressor decompresses any DEFLATE, zlib or gzip compressed data at any level
//
// A single decompressor must not not be used across multiple threads concurrently.
// If you want to decompress concurrently, create a decompressor for each thread.
//
// Always Close() the decompressor to free c memory.
// One Decompressor allocates at least 32KiB.
type Decompressor struct {
dc *native.Decompressor
}
// NewDecompressor returns a new Decompressor used to decompress data at any compression level and with any Mode.
// Errors if out of memory. Allocates 32KiB.
func NewDecompressor() (Decompressor, error) {
dc, err := native.NewDecompressor()
return Decompressor{dc}, err
}
// NewDecompressorWithExtendedDecompression returns a new Decompressor used to decompress data at any compression level and with any Mode.
// maxDecompressionFactor customizes how much larger your output than your input may be. This is set to a sensible default,
// however, it might need some tweaking if you have a huge compression factor. Usually, NewDecompressor should suffice.
// Errors if out of memory. Allocates 32KiB.
func NewDecompressorWithExtendedDecompression(maxDecompressionFactor int) (Decompressor, error) {
dc, err := native.NewDecompressorWithExtendedDecompression(maxDecompressionFactor)
return Decompressor{dc}, err
}
// DecompressZlib decompresses the given zlib data from in to out and returns out or an error if something went wrong.
//
// If you pass a buffer to out, the size of this buffer must exactly match the length of the decompressed data.
// If you pass nil to out, this function will allocate a sufficient buffer and return it.
//
// If error != nil, the data in out is undefined.
func (dc Decompressor) DecompressZlib(in, out []byte) ([]byte, error) {
return dc.Decompress(in, out, ModeZlib)
}
// Decompress decompresses the given data from in to out and returns out or an error if something went wrong.
// Mode m specifies the format (e.g. zlib) of the data within in.
//
// If you pass a buffer to out, the size of this buffer must exactly match the length of the decompressed data.
// If you pass nil to out, this function will allocate a sufficient buffer and return it.
//
// If error != nil, the data in out is undefined.
func (dc Decompressor) Decompress(in, out []byte, m Mode) ([]byte, error) {
switch m {
case ModeZlib:
return dc.dc.Decompress(in, out, native.DecompressZlib)
case ModeDEFLATE:
return dc.dc.Decompress(in, out, native.DecompressDEFLATE)
case ModeGzip:
return dc.dc.Decompress(in, out, native.DecompressGzip)
default:
panic(errorInvalidModeDecompressor)
}
}
// Close closes the decompressor and releases all occupied resources.
// It is the users responsibility to close decompressors in order to free resources,
// as the underlying c objects are not subject to the go garbage collector. They have to be freed manually.
//
// After closing, the decompressor must not be used anymore, as the methods will panic.
func (dc Decompressor) Close() {
dc.dc.Close()
}