Documentation
¶
Overview ¶
Package compression provides data compression and decompression using Zstd and S2 algorithms.
Example (RoundTrip) ¶
package main
import (
"fmt"
"github.com/primandproper/platform-go/v10/compression"
)
func main() {
c, err := compression.NewCompressor("zstd")
if err != nil {
panic(err)
}
original := []byte("hello, world!")
compressed, err := c.CompressBytes(original)
if err != nil {
panic(err)
}
decompressed, err := c.DecompressBytes(compressed)
if err != nil {
panic(err)
}
fmt.Println(string(decompressed))
}
Output: hello, world!
Index ¶
Examples ¶
Constants ¶
const ( // AlgorithmZstd selects the Zstandard compression algorithm. AlgorithmZstd Algorithm = "zstd" // AlgorithmS2 selects the S2 compression algorithm. AlgorithmS2 Algorithm = "s2" // DefaultMaxDecompressedBytes bounds how many bytes DecompressBytes will produce for a // single input, guarding against decompression bombs (a small hostile payload that expands // to gigabytes). It matches zstd's own default decoder memory limit. Override per-Compressor // with WithMaxDecompressedBytes. DefaultMaxDecompressedBytes uint64 = 64 << 20 // 64 MiB )
Variables ¶
var ( // ErrInvalidAlgorithm is returned when an unsupported compression algorithm is requested. // // Algorithm is a runtime-config selection seam like any provider name, so this // wraps errors.ErrUnknownProvider: startup code that branches on one sentinel // for "the config named something this build does not have" catches this too, // and a caller that wants to say "compression" specifically still can. ErrInvalidAlgorithm = errors.Wrap(errors.ErrUnknownProvider, "invalid compression algorithm") // ErrDecompressedTooLarge is returned when decompressing an input would exceed the // configured maximum decompressed size. ErrDecompressedTooLarge = errors.New("decompressed output exceeds configured maximum") )
Functions ¶
This section is empty.
Types ¶
type Algorithm ¶
type Algorithm string
Algorithm identifies a supported compression algorithm. It is a named string type so callers can select an algorithm from a runtime config string via a plain conversion (e.g. Algorithm(cfg.Algorithm)).
type Compressor ¶
type Compressor interface {
CompressBytes(in []byte) ([]byte, error)
DecompressBytes(in []byte) ([]byte, error)
}
Compressor compresses and decompresses byte slices.
type Option ¶
type Option func(*StandardCompressor)
Option configures a Compressor.
func WithMaxDecompressedBytes ¶
WithMaxDecompressedBytes overrides the maximum number of bytes DecompressBytes will produce for a single input. A value of 0 leaves the default (DefaultMaxDecompressedBytes) in place.
type StandardCompressor ¶
type StandardCompressor struct {
// contains filtered or unexported fields
}
StandardCompressor is the Compressor for every supported Algorithm. It is exported, and returned by NewCompressor, so a caller can depend on the compressor it built rather than on the Compressor seam.
func NewCompressor ¶
func NewCompressor(a Algorithm, opts ...Option) (*StandardCompressor, error)
NewCompressor returns a new Compressor for the given Algorithm. An unknown or empty Algorithm yields ErrInvalidAlgorithm.
func (*StandardCompressor) CompressBytes ¶
func (c *StandardCompressor) CompressBytes(in []byte) ([]byte, error)
func (*StandardCompressor) DecompressBytes ¶
func (c *StandardCompressor) DecompressBytes(in []byte) ([]byte, error)