forked from wanzo-mini/mini-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnappy_compressor.go
45 lines (39 loc) · 913 Bytes
/
snappy_compressor.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
// Copyright 2022 <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package compressor
import (
"bytes"
"io"
"io/ioutil"
"github.com/golang/snappy"
)
// SnappyCompressor implements the Compressor interface
type SnappyCompressor struct {
}
// Zip .
func (_ SnappyCompressor) Zip(data []byte) ([]byte, error) {
buf := bytes.NewBuffer(nil)
w := snappy.NewBufferedWriter(buf)
defer func() {
w.Close()
}()
_, err := w.Write(data)
if err != nil {
return nil, err
}
err = w.Flush()
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
// Unzip .
func (_ SnappyCompressor) Unzip(data []byte) ([]byte, error) {
r := snappy.NewReader(bytes.NewBuffer(data))
data, err := ioutil.ReadAll(r)
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
return nil, err
}
return data, nil
}