Coverage Report

Created: 2025-07-11 07:11

/rust/registry/src/index.crates.io-6f17d22bba15001f/flate2-1.1.2/src/zlib/bufread.rs
Line
Count
Source (jump to first uncovered line)
1
use std::io;
2
use std::io::prelude::*;
3
use std::mem;
4
5
use crate::zio;
6
use crate::{Compress, Decompress};
7
8
/// A ZLIB encoder, or compressor.
9
///
10
/// This structure implements a [`Read`] interface. When read from, it reads
11
/// uncompressed data from the underlying [`BufRead`] and provides the compressed data.
12
///
13
/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
14
/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
15
///
16
/// # Examples
17
///
18
/// ```
19
/// use std::io::prelude::*;
20
/// use flate2::Compression;
21
/// use flate2::bufread::ZlibEncoder;
22
/// use std::fs::File;
23
/// use std::io::BufReader;
24
///
25
/// // Use a buffered file to compress contents into a Vec<u8>
26
///
27
/// # fn open_hello_world() -> std::io::Result<Vec<u8>> {
28
/// let f = File::open("examples/hello_world.txt")?;
29
/// let b = BufReader::new(f);
30
/// let mut z = ZlibEncoder::new(b, Compression::fast());
31
/// let mut buffer = Vec::new();
32
/// z.read_to_end(&mut buffer)?;
33
/// # Ok(buffer)
34
/// # }
35
/// ```
36
#[derive(Debug)]
37
pub struct ZlibEncoder<R> {
38
    obj: R,
39
    data: Compress,
40
}
41
42
impl<R: BufRead> ZlibEncoder<R> {
43
    /// Creates a new encoder which will read uncompressed data from the given
44
    /// stream and emit the compressed stream.
45
0
    pub fn new(r: R, level: crate::Compression) -> ZlibEncoder<R> {
46
0
        ZlibEncoder {
47
0
            obj: r,
48
0
            data: Compress::new(level, true),
49
0
        }
50
0
    }
51
52
    /// Creates a new encoder with the given `compression` settings which will
53
    /// read uncompressed data from the given stream `r` and emit the compressed stream.
54
0
    pub fn new_with_compress(r: R, compression: Compress) -> ZlibEncoder<R> {
55
0
        ZlibEncoder {
56
0
            obj: r,
57
0
            data: compression,
58
0
        }
59
0
    }
60
}
61
62
0
pub fn reset_encoder_data<R>(zlib: &mut ZlibEncoder<R>) {
63
0
    zlib.data.reset()
64
0
}
65
66
impl<R> ZlibEncoder<R> {
67
    /// Resets the state of this encoder entirely, swapping out the input
68
    /// stream for another.
69
    ///
70
    /// This function will reset the internal state of this encoder and replace
71
    /// the input stream with the one provided, returning the previous input
72
    /// stream. Future data read from this encoder will be the compressed
73
    /// version of `r`'s data.
74
0
    pub fn reset(&mut self, r: R) -> R {
75
0
        reset_encoder_data(self);
76
0
        mem::replace(&mut self.obj, r)
77
0
    }
78
79
    /// Acquires a reference to the underlying reader
80
0
    pub fn get_ref(&self) -> &R {
81
0
        &self.obj
82
0
    }
83
84
    /// Acquires a mutable reference to the underlying stream
85
    ///
86
    /// Note that mutation of the stream may result in surprising results if
87
    /// this encoder is continued to be used.
88
0
    pub fn get_mut(&mut self) -> &mut R {
89
0
        &mut self.obj
90
0
    }
91
92
    /// Consumes this encoder, returning the underlying reader.
93
0
    pub fn into_inner(self) -> R {
94
0
        self.obj
95
0
    }
96
97
    /// Returns the number of bytes that have been read into this compressor.
98
    ///
99
    /// Note that not all bytes read from the underlying object may be accounted
100
    /// for, there may still be some active buffering.
101
0
    pub fn total_in(&self) -> u64 {
102
0
        self.data.total_in()
103
0
    }
104
105
    /// Returns the number of bytes that the compressor has produced.
106
    ///
107
    /// Note that not all bytes may have been read yet, some may still be
108
    /// buffered.
109
0
    pub fn total_out(&self) -> u64 {
110
0
        self.data.total_out()
111
0
    }
112
}
113
114
impl<R: BufRead> Read for ZlibEncoder<R> {
115
0
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
116
0
        zio::read(&mut self.obj, &mut self.data, buf)
117
0
    }
118
}
119
120
impl<R: BufRead + Write> Write for ZlibEncoder<R> {
121
0
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
122
0
        self.get_mut().write(buf)
123
0
    }
124
125
0
    fn flush(&mut self) -> io::Result<()> {
126
0
        self.get_mut().flush()
127
0
    }
128
}
129
130
/// A ZLIB decoder, or decompressor.
131
///
132
/// This structure implements a [`Read`] interface. When read from, it reads
133
/// compressed data from the underlying [`BufRead`] and provides the uncompressed data.
134
///
135
/// After reading a single member of the ZLIB data this reader will return
136
/// Ok(0) even if there are more bytes available in the underlying reader.
137
/// If you need the following bytes, call `into_inner()` after Ok(0) to
138
/// recover the underlying reader.
139
///
140
/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
141
/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
142
///
143
/// # Examples
144
///
145
/// ```
146
/// use std::io::prelude::*;
147
/// use std::io;
148
/// # use flate2::Compression;
149
/// # use flate2::write::ZlibEncoder;
150
/// use flate2::bufread::ZlibDecoder;
151
///
152
/// # fn main() {
153
/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
154
/// # e.write_all(b"Hello World").unwrap();
155
/// # let bytes = e.finish().unwrap();
156
/// # println!("{}", decode_bufreader(bytes).unwrap());
157
/// # }
158
/// #
159
/// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error
160
/// // Here &[u8] implements BufRead
161
///
162
/// fn decode_bufreader(bytes: Vec<u8>) -> io::Result<String> {
163
///     let mut z = ZlibDecoder::new(&bytes[..]);
164
///     let mut s = String::new();
165
///     z.read_to_string(&mut s)?;
166
///     Ok(s)
167
/// }
168
/// ```
169
#[derive(Debug)]
170
pub struct ZlibDecoder<R> {
171
    obj: R,
172
    data: Decompress,
173
}
174
175
impl<R: BufRead> ZlibDecoder<R> {
176
    /// Creates a new decoder which will decompress data read from the given
177
    /// stream.
178
0
    pub fn new(r: R) -> ZlibDecoder<R> {
179
0
        ZlibDecoder {
180
0
            obj: r,
181
0
            data: Decompress::new(true),
182
0
        }
183
0
    }
184
185
    /// Creates a new decoder which will decompress data read from the given
186
    /// stream, using the given `decompression` settings.
187
0
    pub fn new_with_decompress(r: R, decompression: Decompress) -> ZlibDecoder<R> {
188
0
        ZlibDecoder {
189
0
            obj: r,
190
0
            data: decompression,
191
0
        }
192
0
    }
193
}
194
195
0
pub fn reset_decoder_data<R>(zlib: &mut ZlibDecoder<R>) {
196
0
    zlib.data = Decompress::new(true);
197
0
}
198
199
impl<R> ZlibDecoder<R> {
200
    /// Resets the state of this decoder entirely, swapping out the input
201
    /// stream for another.
202
    ///
203
    /// This will reset the internal state of this decoder and replace the
204
    /// input stream with the one provided, returning the previous input
205
    /// stream. Future data read from this decoder will be the decompressed
206
    /// version of `r`'s data.
207
0
    pub fn reset(&mut self, r: R) -> R {
208
0
        reset_decoder_data(self);
209
0
        mem::replace(&mut self.obj, r)
210
0
    }
211
212
    /// Acquires a reference to the underlying stream
213
0
    pub fn get_ref(&self) -> &R {
214
0
        &self.obj
215
0
    }
216
217
    /// Acquires a mutable reference to the underlying stream
218
    ///
219
    /// Note that mutation of the stream may result in surprising results if
220
    /// this decoder is continued to be used.
221
0
    pub fn get_mut(&mut self) -> &mut R {
222
0
        &mut self.obj
223
0
    }
224
225
    /// Consumes this decoder, returning the underlying reader.
226
0
    pub fn into_inner(self) -> R {
227
0
        self.obj
228
0
    }
229
230
    /// Returns the number of bytes that the decompressor has consumed.
231
    ///
232
    /// Note that this will likely be smaller than what the decompressor
233
    /// actually read from the underlying stream due to buffering.
234
0
    pub fn total_in(&self) -> u64 {
235
0
        self.data.total_in()
236
0
    }
237
238
    /// Returns the number of bytes that the decompressor has produced.
239
0
    pub fn total_out(&self) -> u64 {
240
0
        self.data.total_out()
241
0
    }
242
}
243
244
impl<R: BufRead> Read for ZlibDecoder<R> {
245
0
    fn read(&mut self, into: &mut [u8]) -> io::Result<usize> {
246
0
        zio::read(&mut self.obj, &mut self.data, into)
247
0
    }
248
}
249
250
impl<R: BufRead + Write> Write for ZlibDecoder<R> {
251
0
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
252
0
        self.get_mut().write(buf)
253
0
    }
254
255
0
    fn flush(&mut self) -> io::Result<()> {
256
0
        self.get_mut().flush()
257
0
    }
258
}
259
260
#[cfg(test)]
261
mod test {
262
    use crate::bufread::ZlibDecoder;
263
    use crate::zlib::write;
264
    use crate::Compression;
265
    use std::io::{Read, Write};
266
267
    // ZlibDecoder consumes one zlib archive and then returns 0 for subsequent reads, allowing any
268
    // additional data to be consumed by the caller.
269
    #[test]
270
    fn decode_extra_data() {
271
        let expected = "Hello World";
272
273
        let compressed = {
274
            let mut e = write::ZlibEncoder::new(Vec::new(), Compression::default());
275
            e.write(expected.as_ref()).unwrap();
276
            let mut b = e.finish().unwrap();
277
            b.push(b'x');
278
            b
279
        };
280
281
        let mut output = Vec::new();
282
        let mut decoder = ZlibDecoder::new(compressed.as_slice());
283
        let decoded_bytes = decoder.read_to_end(&mut output).unwrap();
284
        assert_eq!(decoded_bytes, output.len());
285
        let actual = std::str::from_utf8(&output).expect("String parsing error");
286
        assert_eq!(
287
            actual, expected,
288
            "after decompression we obtain the original input"
289
        );
290
291
        output.clear();
292
        assert_eq!(
293
            decoder.read(&mut output).unwrap(),
294
            0,
295
            "subsequent read of decoder returns 0, but inner reader can return additional data"
296
        );
297
        let mut reader = decoder.into_inner();
298
        assert_eq!(
299
            reader.read_to_end(&mut output).unwrap(),
300
            1,
301
            "extra data is accessible in underlying buf-read"
302
        );
303
        assert_eq!(output, b"x");
304
    }
305
}