Skip to content

Commit

Permalink
zstd: Add function to get decoder as io.ReadCloser (#191)
Browse files Browse the repository at this point in the history
* zstd: Add function to get decoder as io.ReadCloser
* Add io.WriterTo support.

This is mostly for convenience.
  • Loading branch information
klauspost authored Dec 10, 2019
1 parent 438db64 commit 4ace5cc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions zstd/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,35 @@ func (d *Decoder) Close() {
d.current.err = ErrDecoderClosed
}

// IOReadCloser returns the decoder as an io.ReadCloser for convenience.
// Any changes to the decoder will be reflected, so the returned ReadCloser
// can be reused along with the decoder.
// io.WriterTo is also supported by the returned ReadCloser.
func (d *Decoder) IOReadCloser() io.ReadCloser {
return closeWrapper{d: d}
}

// closeWrapper wraps a function call as a closer.
type closeWrapper struct {
d *Decoder
}

// WriteTo forwards WriteTo calls to the decoder.
func (c closeWrapper) WriteTo(w io.Writer) (n int64, err error) {
return c.d.WriteTo(w)
}

// Read forwards read calls to the decoder.
func (c closeWrapper) Read(p []byte) (n int, err error) {
return c.d.Read(p)
}

// Close closes the decoder.
func (c closeWrapper) Close() error {
c.d.Close()
return nil
}

type decodeOutput struct {
d *blockDec
b []byte
Expand Down

0 comments on commit 4ace5cc

Please sign in to comment.