github.com/parquet-go/parquet-go@v0.20.0/compress/gzip/gzip.go (about)

     1  // Package gzip implements the GZIP parquet compression codec.
     2  package gzip
     3  
     4  import (
     5  	"io"
     6  	"strings"
     7  
     8  	"github.com/klauspost/compress/gzip"
     9  	"github.com/parquet-go/parquet-go/compress"
    10  	"github.com/parquet-go/parquet-go/format"
    11  )
    12  
    13  const (
    14  	emptyGzip = "\x1f\x8b\b\x00\x00\x00\x00\x00\x02\xff\x01\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00"
    15  )
    16  
    17  const (
    18  	NoCompression      = gzip.NoCompression
    19  	BestSpeed          = gzip.BestSpeed
    20  	BestCompression    = gzip.BestCompression
    21  	DefaultCompression = gzip.DefaultCompression
    22  	HuffmanOnly        = gzip.HuffmanOnly
    23  )
    24  
    25  type Codec struct {
    26  	Level int
    27  
    28  	r compress.Decompressor
    29  	w compress.Compressor
    30  }
    31  
    32  func (c *Codec) String() string {
    33  	return "GZIP"
    34  }
    35  
    36  func (c *Codec) CompressionCodec() format.CompressionCodec {
    37  	return format.Gzip
    38  }
    39  
    40  func (c *Codec) Encode(dst, src []byte) ([]byte, error) {
    41  	return c.w.Encode(dst, src, func(w io.Writer) (compress.Writer, error) {
    42  		return gzip.NewWriterLevel(w, c.Level)
    43  	})
    44  }
    45  
    46  func (c *Codec) Decode(dst, src []byte) ([]byte, error) {
    47  	return c.r.Decode(dst, src, func(r io.Reader) (compress.Reader, error) {
    48  		z, err := gzip.NewReader(r)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  		return &reader{Reader: z}, nil
    53  	})
    54  }
    55  
    56  type reader struct {
    57  	*gzip.Reader
    58  	emptyGzip strings.Reader
    59  }
    60  
    61  func (r *reader) Reset(rr io.Reader) error {
    62  	if rr == nil {
    63  		r.emptyGzip.Reset(emptyGzip)
    64  		rr = &r.emptyGzip
    65  	}
    66  	return r.Reader.Reset(rr)
    67  }