From 5e21b95ef1d85d45257a02c26dfe52ada60d3359 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Mon, 9 Dec 2019 03:37:42 -0800 Subject: [PATCH 1/2] zstd: Add function to get decoder as io.ReadCloser This is mostly for convenience. --- zstd/decoder.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/zstd/decoder.go b/zstd/decoder.go index 1de94eef0c..b910bc2eef 100644 --- a/zstd/decoder.go +++ b/zstd/decoder.go @@ -388,6 +388,29 @@ 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. +func (d *Decoder) IOReadCloser() io.ReadCloser { + return closeWrapper{d: d} +} + +// closeWrapper wraps a function call as a closer. +type closeWrapper struct { + d *Decoder +} + +// 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 From 26e3868e18611c28ccb75327cf5fdab3a4177e05 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Mon, 9 Dec 2019 04:09:08 -0800 Subject: [PATCH 2/2] Add io.WriterTo support. --- zstd/decoder.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/zstd/decoder.go b/zstd/decoder.go index b910bc2eef..35a3cda914 100644 --- a/zstd/decoder.go +++ b/zstd/decoder.go @@ -391,6 +391,7 @@ func (d *Decoder) Close() { // 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} } @@ -400,6 +401,11 @@ 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)