zstd/stream/read/
mod.rs

1//! Implement pull-based [`Read`] trait for both compressing and decompressing.
2use std::io::{self, BufRead, BufReader, Read};
3
4use crate::dict::{DecoderDictionary, EncoderDictionary};
5use crate::stream::{raw, zio};
6use zstd_safe;
7
8#[cfg(test)]
9mod tests;
10
11/// A decoder that decompress input data from another `Read`.
12///
13/// This allows to read a stream of compressed data
14/// (good for files or heavy network stream).
15pub struct Decoder<'a, R> {
16    reader: zio::Reader<R, raw::Decoder<'a>>,
17}
18
19/// An encoder that compress input data from another `Read`.
20pub struct Encoder<'a, R> {
21    reader: zio::Reader<R, raw::Encoder<'a>>,
22}
23
24impl<R: Read> Decoder<'static, BufReader<R>> {
25    /// Creates a new decoder.
26    pub fn new(reader: R) -> io::Result<Self> {
27        let buffer_size = zstd_safe::DCtx::in_size();
28
29        Self::with_buffer(BufReader::with_capacity(buffer_size, reader))
30    }
31}
32
33impl<R: BufRead> Decoder<'static, R> {
34    /// Creates a new decoder around a `BufRead`.
35    pub fn with_buffer(reader: R) -> io::Result<Self> {
36        Self::with_dictionary(reader, &[])
37    }
38    /// Creates a new decoder, using an existing dictionary.
39    ///
40    /// The dictionary must be the same as the one used during compression.
41    pub fn with_dictionary(reader: R, dictionary: &[u8]) -> io::Result<Self> {
42        let decoder = raw::Decoder::with_dictionary(dictionary)?;
43        let reader = zio::Reader::new(reader, decoder);
44
45        Ok(Decoder { reader })
46    }
47}
48impl<'a, R: BufRead> Decoder<'a, R> {
49    /// Creates a new decoder which employs the provided context for deserialization.
50    pub fn with_context(
51        reader: R,
52        context: &'a mut zstd_safe::DCtx<'static>,
53    ) -> Self {
54        Self {
55            reader: zio::Reader::new(
56                reader,
57                raw::Decoder::with_context(context),
58            ),
59        }
60    }
61
62    /// Sets this `Decoder` to stop after the first frame.
63    ///
64    /// By default, it keeps concatenating frames until EOF is reached.
65    #[must_use]
66    pub fn single_frame(mut self) -> Self {
67        self.reader.set_single_frame();
68        self
69    }
70
71    /// Creates a new decoder, using an existing `DecoderDictionary`.
72    ///
73    /// The dictionary must be the same as the one used during compression.
74    pub fn with_prepared_dictionary<'b>(
75        reader: R,
76        dictionary: &DecoderDictionary<'b>,
77    ) -> io::Result<Self>
78    where
79        'b: 'a,
80    {
81        let decoder = raw::Decoder::with_prepared_dictionary(dictionary)?;
82        let reader = zio::Reader::new(reader, decoder);
83
84        Ok(Decoder { reader })
85    }
86
87    /// Creates a new decoder, using a ref prefix.
88    ///
89    /// The prefix must be the same as the one used during compression.
90    pub fn with_ref_prefix<'b>(
91        reader: R,
92        ref_prefix: &'b [u8],
93    ) -> io::Result<Self>
94    where
95        'b: 'a,
96    {
97        let decoder = raw::Decoder::with_ref_prefix(ref_prefix)?;
98        let reader = zio::Reader::new(reader, decoder);
99
100        Ok(Decoder { reader })
101    }
102
103    /// Recommendation for the size of the output buffer.
104    pub fn recommended_output_size() -> usize {
105        zstd_safe::DCtx::out_size()
106    }
107
108    /// Acquire a reference to the underlying reader.
109    pub fn get_ref(&self) -> &R {
110        self.reader.reader()
111    }
112
113    /// Acquire a mutable reference to the underlying reader.
114    ///
115    /// Note that mutation of the reader may result in surprising results if
116    /// this decoder is continued to be used.
117    pub fn get_mut(&mut self) -> &mut R {
118        self.reader.reader_mut()
119    }
120
121    /// Return the inner `Read`.
122    ///
123    /// Calling `finish()` is not *required* after reading a stream -
124    /// just use it if you need to get the `Read` back.
125    pub fn finish(self) -> R {
126        self.reader.into_inner()
127    }
128
129    crate::decoder_common!(reader);
130}
131
132impl<R: BufRead> Read for Decoder<'_, R> {
133    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
134        self.reader.read(buf)
135    }
136}
137
138impl<R: Read> Encoder<'static, BufReader<R>> {
139    /// Creates a new encoder.
140    pub fn new(reader: R, level: i32) -> io::Result<Self> {
141        let buffer_size = zstd_safe::CCtx::in_size();
142
143        Self::with_buffer(BufReader::with_capacity(buffer_size, reader), level)
144    }
145}
146
147impl<R: BufRead> Encoder<'static, R> {
148    /// Creates a new encoder around a `BufRead`.
149    pub fn with_buffer(reader: R, level: i32) -> io::Result<Self> {
150        Self::with_dictionary(reader, level, &[])
151    }
152
153    /// Creates a new encoder, using an existing dictionary.
154    ///
155    /// The dictionary must be the same as the one used during compression.
156    pub fn with_dictionary(
157        reader: R,
158        level: i32,
159        dictionary: &[u8],
160    ) -> io::Result<Self> {
161        let encoder = raw::Encoder::with_dictionary(level, dictionary)?;
162        let reader = zio::Reader::new(reader, encoder);
163
164        Ok(Encoder { reader })
165    }
166}
167
168impl<'a, R: BufRead> Encoder<'a, R> {
169    /// Creates a new encoder, using an existing `EncoderDictionary`.
170    ///
171    /// The dictionary must be the same as the one used during compression.
172    pub fn with_prepared_dictionary<'b>(
173        reader: R,
174        dictionary: &EncoderDictionary<'b>,
175    ) -> io::Result<Self>
176    where
177        'b: 'a,
178    {
179        let encoder = raw::Encoder::with_prepared_dictionary(dictionary)?;
180        let reader = zio::Reader::new(reader, encoder);
181
182        Ok(Encoder { reader })
183    }
184
185    /// Recommendation for the size of the output buffer.
186    pub fn recommended_output_size() -> usize {
187        zstd_safe::CCtx::out_size()
188    }
189
190    /// Acquire a reference to the underlying reader.
191    pub fn get_ref(&self) -> &R {
192        self.reader.reader()
193    }
194
195    /// Acquire a mutable reference to the underlying reader.
196    ///
197    /// Note that mutation of the reader may result in surprising results if
198    /// this encoder is continued to be used.
199    pub fn get_mut(&mut self) -> &mut R {
200        self.reader.reader_mut()
201    }
202
203    /// Flush any internal buffer.
204    ///
205    /// This ensures all input consumed so far is compressed.
206    ///
207    /// Since it prevents bundling currently buffered data with future input,
208    /// it may affect compression ratio.
209    ///
210    /// * Returns the number of bytes written to `out`.
211    /// * Returns `Ok(0)` when everything has been flushed.
212    pub fn flush(&mut self, out: &mut [u8]) -> io::Result<usize> {
213        self.reader.flush(out)
214    }
215
216    /// Return the inner `Read`.
217    ///
218    /// Calling `finish()` is not *required* after reading a stream -
219    /// just use it if you need to get the `Read` back.
220    pub fn finish(self) -> R {
221        self.reader.into_inner()
222    }
223
224    crate::encoder_common!(reader);
225}
226
227impl<R: BufRead> Read for Encoder<'_, R> {
228    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
229        self.reader.read(buf)
230    }
231}
232
233fn _assert_traits() {
234    use std::io::Cursor;
235
236    fn _assert_send<T: Send>(_: T) {}
237
238    _assert_send(Decoder::new(Cursor::new(Vec::new())));
239    _assert_send(Encoder::new(Cursor::new(Vec::new()), 1));
240}