forked from xiaonanln/goworld
/
gwsnappy.go
44 lines (36 loc) · 988 Bytes
/
gwsnappy.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
package compress
import (
"os"
"bytes"
"github.com/xiaonanln/goworld/engine/gwioutil"
"github.com/xiaonanln/goworld/engine/lib/gwsnappy"
)
// NewGWSnappyCompressor creates a new Compressor in gwsnappy format
// gwsnappy is an optimized version of snappy format, but strips many useless fields
func NewGWSnappyCompressor() Compressor {
return &gwsnappyCompressor{
writer: gwsnappy.NewWriter(os.Stdout),
reader: gwsnappy.NewReader(os.Stdin),
}
}
type gwsnappyCompressor struct {
writer *gwsnappy.Writer
reader *gwsnappy.Reader
}
func (sc *gwsnappyCompressor) Compress(b []byte, c []byte) ([]byte, error) {
wb := bytes.NewBuffer(c)
sc.writer.Reset(wb)
n, err := sc.writer.Write(b)
if err != nil {
return nil, err
}
if n != len(b) {
return nil, errNotFullyCompressed
}
sc.writer.Flush()
return wb.Bytes(), nil
}
func (sc *gwsnappyCompressor) Decompress(c []byte, b []byte) error {
sc.reader.Reset(bytes.NewReader(c))
return gwioutil.ReadAll(sc.reader, b)
}