github.com/grailbio/base@v0.0.11/compress/zstd/zstd_cgo.go (about) 1 // Package zstd wraps github.com/DataDog/zstd and 2 // github.com/klauspost/compress/zstd. It uses DataDog/zstd in cgo mode, and 3 // klauspost/compress/zstd in noncgo mode. 4 5 // +build cgo 6 7 package zstd 8 9 import ( 10 "io" 11 12 cgozstd "github.com/DataDog/zstd" 13 ) 14 15 // Compress compresses the given source data. Scratch can be passed to prevent 16 // prevent allocation. If it is too small, or if nil is passed, a new buffer 17 // will be allocated and returned. Arg level specifies the compression 18 // level. level < 0 means the default compression level. 19 func CompressLevel(scratch []byte, in []byte, level int) ([]byte, error) { 20 if level < 0 { 21 level = cgozstd.DefaultCompression 22 } 23 if cap(scratch) == 0 { 24 scratch = nil 25 } else { 26 scratch = scratch[:cap(scratch)] 27 } 28 return cgozstd.CompressLevel(scratch, in, level) 29 } 30 31 // Decompress uncompresses the given source data. Scratch can be passed to 32 // prevent allocation. If it is too small, or if nil is passed, a new buffer 33 // will be allocated and returned. 34 func Decompress(scratch []byte, in []byte) ([]byte, error) { 35 if cap(scratch) == 0 { 36 scratch = nil 37 } else { 38 scratch = scratch[:cap(scratch)] 39 } 40 return cgozstd.Decompress(scratch, in) 41 } 42 43 // NewReader creates a ReadCloser that uncompresses data. The returned object 44 // must be Closed after use. 45 func NewReader(r io.Reader) (io.ReadCloser, error) { 46 return cgozstd.NewReader(r), nil 47 } 48 49 // NewWriter creates a WriterCloser that compresses data. The returned object 50 // must be Closed after use. 51 func NewWriter(w io.Writer) (io.WriteCloser, error) { 52 return cgozstd.NewWriter(w), nil 53 }