10 // Compression is an interface that Diskv uses to implement compression of
11 // data. Writer takes a destination io.Writer and returns a WriteCloser that
12 // compresses all data written through it. Reader takes a source io.Reader and
13 // returns a ReadCloser that decompresses all data read through it. You may
14 // define these methods on your own type, or use one of the NewCompression
16 type Compression interface {
17 Writer(dst io.Writer) (io.WriteCloser, error)
18 Reader(src io.Reader) (io.ReadCloser, error)
21 // NewGzipCompression returns a Gzip-based Compression.
22 func NewGzipCompression() Compression {
23 return NewGzipCompressionLevel(flate.DefaultCompression)
26 // NewGzipCompressionLevel returns a Gzip-based Compression with the given level.
27 func NewGzipCompressionLevel(level int) Compression {
28 return &genericCompression{
29 wf: func(w io.Writer) (io.WriteCloser, error) { return gzip.NewWriterLevel(w, level) },
30 rf: func(r io.Reader) (io.ReadCloser, error) { return gzip.NewReader(r) },
34 // NewZlibCompression returns a Zlib-based Compression.
35 func NewZlibCompression() Compression {
36 return NewZlibCompressionLevel(flate.DefaultCompression)
39 // NewZlibCompressionLevel returns a Zlib-based Compression with the given level.
40 func NewZlibCompressionLevel(level int) Compression {
41 return NewZlibCompressionLevelDict(level, nil)
44 // NewZlibCompressionLevelDict returns a Zlib-based Compression with the given
45 // level, based on the given dictionary.
46 func NewZlibCompressionLevelDict(level int, dict []byte) Compression {
47 return &genericCompression{
48 func(w io.Writer) (io.WriteCloser, error) { return zlib.NewWriterLevelDict(w, level, dict) },
49 func(r io.Reader) (io.ReadCloser, error) { return zlib.NewReaderDict(r, dict) },
53 type genericCompression struct {
54 wf func(w io.Writer) (io.WriteCloser, error)
55 rf func(r io.Reader) (io.ReadCloser, error)
58 func (g *genericCompression) Writer(dst io.Writer) (io.WriteCloser, error) {
62 func (g *genericCompression) Reader(src io.Reader) (io.ReadCloser, error) {