-
Notifications
You must be signed in to change notification settings - Fork 0
/
gozlib_support_test.go
58 lines (44 loc) · 1.08 KB
/
gozlib_support_test.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
package gozlib
import (
"bytes"
"compress/gzip"
"math/rand"
"io"
)
func makeTestData(len uint32) []byte {
data := make([]byte, len)
for i := uint32(0); i < len; i++ {
data[i] = byte(rand.Int() % 128)
}
return data
}
func stdLibGZipUncompress(compressed *bytes.Buffer, size int64) ([]byte, error) {
decompressed := bytes.NewBuffer(make([]byte, size, size))
decompressed.Reset()
reader, err := gzip.NewReader(compressed)
if err != nil {
return nil, err
}
_, werr := io.Copy(decompressed, reader)
return decompressed.Bytes(), werr
}
func stdLibGZipCompressSlice(data []byte) ([]byte, error) {
compressed := &bytes.Buffer{}
writer, gerr := gzip.NewWriterLevel(compressed, gzip.BestCompression)
if gerr != nil {
return nil, gerr
}
_, err := io.Copy(writer, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
err = writer.Close()
return compressed.Bytes(), err
}
func stdLibGZipCompress(data []byte) (*bytes.Buffer, error) {
compressed, err := stdLibGZipCompressSlice(data)
if err != nil {
return nil, err
}
return bytes.NewBuffer(compressed), nil
}