github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/protocol/compress.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package protocol
     4  
     5  type CompressTypes interface {
     6  	RegisterCompressType(ct CompressType)
     7  	GetCompressTypeByID(id uint64) (ct CompressType, err Error)
     8  	GetCompressTypeByMediaType(mt string) (ct CompressType, err Error)
     9  	GetCompressTypeByFileExtension(ex string) (ct CompressType, err Error)
    10  	GetCompressTypeByContentEncoding(ce string) (ct CompressType, err Error)
    11  	ContentEncodings() []string
    12  }
    13  
    14  // CompressType is standard shape of any compress coding type
    15  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
    16  // https://en.wikipedia.org/wiki/HTTP_compression
    17  type CompressType interface {
    18  	MediaType() MediaType
    19  	ContentEncoding() string
    20  	FileExtension() string // copy of MediaType().FileExtension() to improve performance
    21  
    22  	Compress(raw Codec, options CompressOptions) (compressed Codec, err Error)
    23  	CompressBySlice(raw []byte, options CompressOptions) (compressed Codec, err Error)
    24  	CompressByReader(raw Reader, options CompressOptions) (compressed Codec, err Error)
    25  
    26  	Decompress(compressed Codec) (raw Codec, err Error)
    27  	DecompressFromSlice(compressed []byte) (raw Codec, err Error)
    28  	DecompressFromReader(compressed Reader, compressedLen int) (raw Codec, err Error)
    29  }
    30  
    31  type CompressOptions struct {
    32  	CompressLevel CompressLevel
    33  }
    34  
    35  type CompressLevel int
    36  
    37  // Compress Levels
    38  const (
    39  	CompressLevel_NoCompression   CompressLevel = 0
    40  	CompressLevel_BestSpeed       CompressLevel = 1
    41  	CompressLevel_BestCompression CompressLevel = 9
    42  	CompressLevel_Default         CompressLevel = -1
    43  
    44  	// HuffmanOnly disables Lempel-Ziv match searching and only performs Huffman
    45  	// entropy encoding. This mode is useful in compressing data that has
    46  	// already been compressed with an LZ style algorithm (e.g. Snappy or LZ4)
    47  	// that lacks an entropy encoder. Compression gains are achieved when
    48  	// certain bytes in the input stream occur more frequently than others.
    49  	//
    50  	// Note that HuffmanOnly produces a compressed output that is
    51  	// RFC 1951 compliant. That is, any valid DEFLATE decompressor will
    52  	// continue to be able to decompress this output.
    53  	CompressLevel_HuffmanOnly CompressLevel = -2
    54  )