1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
zstd.go 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-11-01 20:57 . 上传文件调整
package compress
import (
"fmt"
"github.com/DataDog/zstd"
)
// ZstdLevel compression level used by Zstd
const ZstdLevel = 1 // fastest
type noOp struct{}
func (n noOp) Name() string { return "Noop" }
func (n noOp) CompressBound(l int) int { return l }
func (n noOp) Compress(dst, src []byte) (int, error) {
if len(dst) < len(src) {
return 0, fmt.Errorf("buffer too short: %d < %d", len(dst), len(src))
}
copy(dst, src)
return len(src), nil
}
func (n noOp) Decompress(dst, src []byte) (int, error) {
if len(dst) < len(src) {
return 0, fmt.Errorf("buffer too short: %d < %d", len(dst), len(src))
}
copy(dst, src)
return len(src), nil
}
// ZStandard implements Compressor interface using zstd library
type ZStandard struct {
level int
}
// Name returns name of the algorithm Zstd
func (n ZStandard) Name() string { return "Zstd" }
// CompressBound max size of compressed data
func (n ZStandard) CompressBound(l int) int { return zstd.CompressBound(l) }
// Compress using Zstd
func (n ZStandard) Compress(dst, src []byte) (int, error) {
d, err := zstd.CompressLevel(dst, src, n.level)
if err != nil {
return 0, err
}
if len(d) > 0 && len(dst) > 0 && &d[0] != &dst[0] {
return 0, fmt.Errorf("buffer too short: %d < %d", cap(dst), cap(d))
}
return len(d), err
}
// Decompress using Zstd
func (n ZStandard) Decompress(dst, src []byte) (int, error) {
d, err := zstd.Decompress(dst, src)
if err != nil {
return 0, err
}
if len(d) > 0 && len(dst) > 0 && &d[0] != &dst[0] {
return 0, fmt.Errorf("buffer too short: %d < %d", len(dst), len(d))
}
return len(d), err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.16

搜索帮助

344bd9b3 5694891 D2dac590 5694891