github.com/sandwich-go/boost@v1.3.29/xcompress/snappy.go (about)

     1  package xcompress
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/golang/snappy"
     6  	"github.com/sandwich-go/boost/xpanic"
     7  )
     8  
     9  type snappyCompressor struct{}
    10  
    11  func newSnappyCompressor() (Compressor, error) {
    12  	return &snappyCompressor{}, nil
    13  }
    14  
    15  func (c *snappyCompressor) Flat(data []byte) ([]byte, error) {
    16  	if len(data) == 0 {
    17  		return data, nil
    18  	}
    19  	var err error
    20  	var out []byte
    21  	xpanic.Try(func() {
    22  		out = snappy.Encode(nil, data)
    23  	}).Catch(func(e xpanic.E) {
    24  		err = fmt.Errorf("snappy flat error, %v", e)
    25  	})
    26  	return out, err
    27  }
    28  
    29  func (c *snappyCompressor) Inflate(data []byte) ([]byte, error) {
    30  	if len(data) == 0 {
    31  		return data, nil
    32  	}
    33  	return snappy.Decode(nil, data)
    34  }