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 | pub fn new(r: R, level: crate::Compression) -> ZlibEncoder<R> { |
46 | ZlibEncoder { |
47 | obj: r, |
48 | data: Compress::new(level, zlib_header:true), |
49 | } |
50 | } |
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 | pub fn new_with_compress(r: R, compression: Compress) -> ZlibEncoder<R> { |
55 | ZlibEncoder { |
56 | obj: r, |
57 | data: compression, |
58 | } |
59 | } |
60 | } |
61 | |
62 | pub fn reset_encoder_data<R>(zlib: &mut ZlibEncoder<R>) { |
63 | zlib.data.reset() |
64 | } |
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 | pub fn reset(&mut self, r: R) -> R { |
75 | reset_encoder_data(self); |
76 | mem::replace(&mut self.obj, r) |
77 | } |
78 | |
79 | /// Acquires a reference to the underlying reader |
80 | pub fn get_ref(&self) -> &R { |
81 | &self.obj |
82 | } |
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 | pub fn get_mut(&mut self) -> &mut R { |
89 | &mut self.obj |
90 | } |
91 | |
92 | /// Consumes this encoder, returning the underlying reader. |
93 | pub fn into_inner(self) -> R { |
94 | self.obj |
95 | } |
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 | pub fn total_in(&self) -> u64 { |
102 | self.data.total_in() |
103 | } |
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 | pub fn total_out(&self) -> u64 { |
110 | self.data.total_out() |
111 | } |
112 | } |
113 | |
114 | impl<R: BufRead> Read for ZlibEncoder<R> { |
115 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
116 | zio::read(&mut self.obj, &mut self.data, dst:buf) |
117 | } |
118 | } |
119 | |
120 | impl<R: BufRead + Write> Write for ZlibEncoder<R> { |
121 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
122 | self.get_mut().write(buf) |
123 | } |
124 | |
125 | fn flush(&mut self) -> io::Result<()> { |
126 | self.get_mut().flush() |
127 | } |
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 | pub fn new(r: R) -> ZlibDecoder<R> { |
179 | ZlibDecoder { |
180 | obj: r, |
181 | data: Decompress::new(zlib_header:true), |
182 | } |
183 | } |
184 | |
185 | /// Creates a new decoder which will decompress data read from the given |
186 | /// stream, using the given `decompression` settings. |
187 | pub fn new_with_decompress(r: R, decompression: Decompress) -> ZlibDecoder<R> { |
188 | ZlibDecoder { |
189 | obj: r, |
190 | data: decompression, |
191 | } |
192 | } |
193 | } |
194 | |
195 | pub fn reset_decoder_data<R>(zlib: &mut ZlibDecoder<R>) { |
196 | zlib.data = Decompress::new(zlib_header:true); |
197 | } |
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 | pub fn reset(&mut self, r: R) -> R { |
208 | reset_decoder_data(self); |
209 | mem::replace(&mut self.obj, r) |
210 | } |
211 | |
212 | /// Acquires a reference to the underlying stream |
213 | pub fn get_ref(&self) -> &R { |
214 | &self.obj |
215 | } |
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 | pub fn get_mut(&mut self) -> &mut R { |
222 | &mut self.obj |
223 | } |
224 | |
225 | /// Consumes this decoder, returning the underlying reader. |
226 | pub fn into_inner(self) -> R { |
227 | self.obj |
228 | } |
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 | pub fn total_in(&self) -> u64 { |
235 | self.data.total_in() |
236 | } |
237 | |
238 | /// Returns the number of bytes that the decompressor has produced. |
239 | pub fn total_out(&self) -> u64 { |
240 | self.data.total_out() |
241 | } |
242 | } |
243 | |
244 | impl<R: BufRead> Read for ZlibDecoder<R> { |
245 | fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { |
246 | zio::read(&mut self.obj, &mut self.data, dst:into) |
247 | } |
248 | } |
249 | |
250 | impl<R: BufRead + Write> Write for ZlibDecoder<R> { |
251 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
252 | self.get_mut().write(buf) |
253 | } |
254 | |
255 | fn flush(&mut self) -> io::Result<()> { |
256 | self.get_mut().flush() |
257 | } |
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 | } |
306 | |