flate

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec[T any] struct {
	// contains filtered or unexported fields
}

Codec is a Codec[T] that applies DEFLATE compression on top of another Codec[T]. It is safe for concurrent use.

Example
type Data struct {
	Name string
}

c := flate.NewCodec(json.NewCodec[Data]())

encoded, err := c.Encode(Data{Name: "example-123"})
if err != nil {
	fmt.Printf("Encode failed: %v\n", err)
	return
}

var decoded Data
if err := c.Decode(encoded, &decoded); err != nil {
	fmt.Printf("Decode failed: %v\n", err)
	return
}

fmt.Printf("Decoded Name: %s\n", decoded.Name)
Output:
Decoded Name: example-123

func NewCodec

func NewCodec[T any](codec encoding.Codec[T], opts ...Option) *Codec[T]

NewCodec returns a flate compression codec that delegates serialization to codec.

func (*Codec[T]) Decode

func (c *Codec[T]) Decode(b []byte, v *T) error

func (*Codec[T]) Encode

func (c *Codec[T]) Encode(v T) ([]byte, error)

type Option

type Option func(o *options)

Option configures a flate Codec.

func WithLevel

func WithLevel(level int) Option

WithLevel sets the DEFLATE compression level. Use compress/flate constants: flate.NoCompression, flate.BestSpeed, flate.BestCompression, flate.DefaultCompression, flate.HuffmanOnly.

func WithMaxDecodedSize

func WithMaxDecodedSize(n int64) Option

WithMaxDecodedSize sets the maximum allowed size of decompressed data in bytes. If the decompressed data exceeds this limit, Decode returns an error. A value of 0 (the default) means no limit.

type StreamCodec

type StreamCodec[T any] struct {
	// contains filtered or unexported fields
}

StreamCodec is a StreamCodec[T] that applies DEFLATE compression on top of another StreamCodec[T]. It is safe for concurrent use.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/foomo/goencode/flate"
	json "github.com/foomo/goencode/json/v1"
)

func main() {
	type Data struct {
		Name string
	}

	c := flate.NewStreamCodec(json.NewStreamCodec[Data]())

	var buf bytes.Buffer
	if err := c.Encode(&buf, Data{Name: "example-123"}); err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	var decoded Data
	if err := c.Decode(&buf, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded Name: %s\n", decoded.Name)
}
Output:
Decoded Name: example-123

func NewStreamCodec

func NewStreamCodec[T any](codec encoding.StreamCodec[T], opts ...Option) *StreamCodec[T]

NewStreamCodec returns a flate compression stream codec that delegates serialization to codec.

func (*StreamCodec[T]) Decode

func (c *StreamCodec[T]) Decode(r io.Reader, v *T) error

func (*StreamCodec[T]) Encode

func (c *StreamCodec[T]) Encode(w io.Writer, v T) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL