2 Star 2 Fork 3

trackertrader/rpcx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
compressor.go 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
wulengbing@163.com 提交于 2022-06-07 11:30 +08:00 . modify
package protocol
import (
"bytes"
"io/ioutil"
"gitee.com/trackertrader/rpcx/util"
"github.com/golang/snappy"
)
// Compressor defines a common compression interface.
type Compressor interface {
Zip([]byte) ([]byte, error)
Unzip([]byte) ([]byte, error)
}
// GzipCompressor implements gzip compressor.
type GzipCompressor struct {
}
func (c GzipCompressor) Zip(data []byte) ([]byte, error) {
return util.Zip(data)
}
func (c GzipCompressor) Unzip(data []byte) ([]byte, error) {
return util.Unzip(data)
}
type RawDataCompressor struct {
}
func (c RawDataCompressor) Zip(data []byte) ([]byte, error) {
return data, nil
}
func (c RawDataCompressor) Unzip(data []byte) ([]byte, error) {
return data, nil
}
// SnappyCompressor implements snappy compressor
type SnappyCompressor struct {
}
func (c *SnappyCompressor) Zip(data []byte) ([]byte, error) {
if len(data) == 0 {
return data, nil
}
var buffer bytes.Buffer
writer := snappy.NewBufferedWriter(&buffer)
_, err := writer.Write(data)
if err != nil {
writer.Close()
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func (c *SnappyCompressor) Unzip(data []byte) ([]byte, error) {
if len(data) == 0 {
return data, nil
}
reader := snappy.NewReader(bytes.NewReader(data))
out, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return out, err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/trackertrader/rpcx.git
git@gitee.com:trackertrader/rpcx.git
trackertrader
rpcx
rpcx
v1.2.1

搜索帮助