1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
// package gzstd provides gzip compression through the standard library.
package gzstd
import (
"compress/gzip"
"io"
"sync"
"github.com/klauspost/compress/gzhttp/writer"
)
// gzipWriterPools stores a sync.Pool for each compression level for reuse of
// gzip.Writers. Use poolIndex to covert a compression level to an index into
// gzipWriterPools.
var gzipWriterPools [gzip.BestCompression - gzip.HuffmanOnly + 1]*sync.Pool
func init() {
for i := gzip.HuffmanOnly; i <= gzip.BestCompression; i++ {
addLevelPool(i)
}
}
// poolIndex maps a compression level to its index into gzipWriterPools. It
// assumes that level is a valid gzip compression level.
func poolIndex(level int) int {
return level - gzip.HuffmanOnly
}
func addLevelPool(level int) {
gzipWriterPools[poolIndex(level)] = &sync.Pool{
New: func() interface{} {
// NewWriterLevel only returns error on a bad level, we are guaranteeing
// that this will be a valid level so it is okay to ignore the returned
// error.
w, _ := gzip.NewWriterLevel(nil, level)
return w
},
}
}
type pooledWriter struct {
*gzip.Writer
index int
}
func (pw *pooledWriter) Close() error {
err := pw.Writer.Close()
gzipWriterPools[pw.index].Put(pw.Writer)
pw.Writer = nil
return err
}
func NewWriter(w io.Writer, level int) writer.GzipWriter {
index := poolIndex(level)
gzw := gzipWriterPools[index].Get().(*gzip.Writer)
gzw.Reset(w)
return &pooledWriter{
Writer: gzw,
index: index,
}
}
// SetHeader will override the gzip header on pw.
func (pw *pooledWriter) SetHeader(h writer.Header) {
pw.Name = h.Name
pw.Extra = h.Extra
pw.Comment = h.Comment
pw.ModTime = h.ModTime
pw.OS = h.OS
}
func Levels() (min, max int) {
return gzip.HuffmanOnly, gzip.BestCompression
}
func ImplementationInfo() string {
return "compress/gzip"
}
|