/rust/registry/src/index.crates.io-1949cf8c6b5b557f/flate2-1.0.28/src/zlib/read.rs
Line | Count | Source |
1 | | use std::io; |
2 | | use std::io::prelude::*; |
3 | | |
4 | | use super::bufread; |
5 | | use crate::bufreader::BufReader; |
6 | | use crate::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 [`Read`] and provides the compressed data. |
12 | | /// |
13 | | /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html |
14 | | /// |
15 | | /// # Examples |
16 | | /// |
17 | | /// ``` |
18 | | /// use std::io::prelude::*; |
19 | | /// use flate2::Compression; |
20 | | /// use flate2::read::ZlibEncoder; |
21 | | /// use std::fs::File; |
22 | | /// |
23 | | /// // Open example file and compress the contents using Read interface |
24 | | /// |
25 | | /// # fn open_hello_world() -> std::io::Result<Vec<u8>> { |
26 | | /// let f = File::open("examples/hello_world.txt")?; |
27 | | /// let mut z = ZlibEncoder::new(f, Compression::fast()); |
28 | | /// let mut buffer = Vec::new(); |
29 | | /// z.read_to_end(&mut buffer)?; |
30 | | /// # Ok(buffer) |
31 | | /// # } |
32 | | /// ``` |
33 | | #[derive(Debug)] |
34 | | pub struct ZlibEncoder<R> { |
35 | | inner: bufread::ZlibEncoder<BufReader<R>>, |
36 | | } |
37 | | |
38 | | impl<R: Read> ZlibEncoder<R> { |
39 | | /// Creates a new encoder which will read uncompressed data from the given |
40 | | /// stream and emit the compressed stream. |
41 | 0 | pub fn new(r: R, level: crate::Compression) -> ZlibEncoder<R> { |
42 | 0 | ZlibEncoder { |
43 | 0 | inner: bufread::ZlibEncoder::new(BufReader::new(r), level), |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | /// Creates a new encoder with the given `compression` settings which will |
48 | | /// read uncompressed data from the given stream `r` and emit the compressed stream. |
49 | 0 | pub fn new_with_compress(r: R, compression: crate::Compress) -> ZlibEncoder<R> { |
50 | 0 | ZlibEncoder { |
51 | 0 | inner: bufread::ZlibEncoder::new_with_compress(BufReader::new(r), compression), |
52 | 0 | } |
53 | 0 | } |
54 | | } |
55 | | |
56 | | impl<R> ZlibEncoder<R> { |
57 | | /// Resets the state of this encoder entirely, swapping out the input |
58 | | /// stream for another. |
59 | | /// |
60 | | /// This function will reset the internal state of this encoder and replace |
61 | | /// the input stream with the one provided, returning the previous input |
62 | | /// stream. Future data read from this encoder will be the compressed |
63 | | /// version of `r`'s data. |
64 | | /// |
65 | | /// Note that there may be currently buffered data when this function is |
66 | | /// called, and in that case the buffered data is discarded. |
67 | 0 | pub fn reset(&mut self, r: R) -> R { |
68 | 0 | super::bufread::reset_encoder_data(&mut self.inner); |
69 | 0 | self.inner.get_mut().reset(r) |
70 | 0 | } |
71 | | |
72 | | /// Acquires a reference to the underlying stream |
73 | 0 | pub fn get_ref(&self) -> &R { |
74 | 0 | self.inner.get_ref().get_ref() |
75 | 0 | } |
76 | | |
77 | | /// Acquires a mutable reference to the underlying stream |
78 | | /// |
79 | | /// Note that mutation of the stream may result in surprising results if |
80 | | /// this encoder is continued to be used. |
81 | 0 | pub fn get_mut(&mut self) -> &mut R { |
82 | 0 | self.inner.get_mut().get_mut() |
83 | 0 | } |
84 | | |
85 | | /// Consumes this encoder, returning the underlying reader. |
86 | | /// |
87 | | /// Note that there may be buffered bytes which are not re-acquired as part |
88 | | /// of this transition. It's recommended to only call this function after |
89 | | /// EOF has been reached. |
90 | 0 | pub fn into_inner(self) -> R { |
91 | 0 | self.inner.into_inner().into_inner() |
92 | 0 | } |
93 | | |
94 | | /// Returns the number of bytes that have been read into this compressor. |
95 | | /// |
96 | | /// Note that not all bytes read from the underlying object may be accounted |
97 | | /// for, there may still be some active buffering. |
98 | 0 | pub fn total_in(&self) -> u64 { |
99 | 0 | self.inner.total_in() |
100 | 0 | } |
101 | | |
102 | | /// Returns the number of bytes that the compressor has produced. |
103 | | /// |
104 | | /// Note that not all bytes may have been read yet, some may still be |
105 | | /// buffered. |
106 | 0 | pub fn total_out(&self) -> u64 { |
107 | 0 | self.inner.total_out() |
108 | 0 | } |
109 | | } |
110 | | |
111 | | impl<R: Read> Read for ZlibEncoder<R> { |
112 | 0 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
113 | 0 | self.inner.read(buf) |
114 | 0 | } |
115 | | } |
116 | | |
117 | | impl<W: Read + Write> Write for ZlibEncoder<W> { |
118 | 0 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
119 | 0 | self.get_mut().write(buf) |
120 | 0 | } |
121 | | |
122 | 0 | fn flush(&mut self) -> io::Result<()> { |
123 | 0 | self.get_mut().flush() |
124 | 0 | } |
125 | | } |
126 | | |
127 | | /// A ZLIB decoder, or decompressor. |
128 | | /// |
129 | | /// This structure implements a [`Read`] interface. When read from, it reads |
130 | | /// compressed data from the underlying [`Read`] and provides the uncompressed data. |
131 | | /// |
132 | | /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html |
133 | | /// |
134 | | /// # Examples |
135 | | /// |
136 | | /// ``` |
137 | | /// use std::io::prelude::*; |
138 | | /// use std::io; |
139 | | /// # use flate2::Compression; |
140 | | /// # use flate2::write::ZlibEncoder; |
141 | | /// use flate2::read::ZlibDecoder; |
142 | | /// |
143 | | /// # fn main() { |
144 | | /// # let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); |
145 | | /// # e.write_all(b"Hello World").unwrap(); |
146 | | /// # let bytes = e.finish().unwrap(); |
147 | | /// # println!("{}", decode_reader(bytes).unwrap()); |
148 | | /// # } |
149 | | /// # |
150 | | /// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error |
151 | | /// // Here &[u8] implements Read |
152 | | /// |
153 | | /// fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { |
154 | | /// let mut z = ZlibDecoder::new(&bytes[..]); |
155 | | /// let mut s = String::new(); |
156 | | /// z.read_to_string(&mut s)?; |
157 | | /// Ok(s) |
158 | | /// } |
159 | | /// ``` |
160 | | #[derive(Debug)] |
161 | | pub struct ZlibDecoder<R> { |
162 | | inner: bufread::ZlibDecoder<BufReader<R>>, |
163 | | } |
164 | | |
165 | | impl<R: Read> ZlibDecoder<R> { |
166 | | /// Creates a new decoder which will decompress data read from the given |
167 | | /// stream. |
168 | 0 | pub fn new(r: R) -> ZlibDecoder<R> { |
169 | 0 | ZlibDecoder::new_with_buf(r, vec![0; 32 * 1024]) |
170 | 0 | } |
171 | | |
172 | | /// Creates a new decoder which will decompress data read from the given |
173 | | /// stream `r`, using `buf` as backing to speed up reading. |
174 | | /// |
175 | | /// Note that the specified buffer will only be used up to its current |
176 | | /// length. The buffer's capacity will also not grow over time. |
177 | 0 | pub fn new_with_buf(r: R, buf: Vec<u8>) -> ZlibDecoder<R> { |
178 | 0 | ZlibDecoder { |
179 | 0 | inner: bufread::ZlibDecoder::new(BufReader::with_buf(buf, r)), |
180 | 0 | } |
181 | 0 | } |
182 | | |
183 | | /// Creates a new decoder which will decompress data read from the given |
184 | | /// stream `r`, along with `decompression` settings. |
185 | 0 | pub fn new_with_decompress(r: R, decompression: Decompress) -> ZlibDecoder<R> { |
186 | 0 | ZlibDecoder::new_with_decompress_and_buf(r, vec![0; 32 * 1024], decompression) |
187 | 0 | } |
188 | | |
189 | | /// Creates a new decoder which will decompress data read from the given |
190 | | /// stream `r`, using `buf` as backing to speed up reading, |
191 | | /// along with `decompression` settings to configure decoder. |
192 | | /// |
193 | | /// Note that the specified buffer will only be used up to its current |
194 | | /// length. The buffer's capacity will also not grow over time. |
195 | 0 | pub fn new_with_decompress_and_buf( |
196 | 0 | r: R, |
197 | 0 | buf: Vec<u8>, |
198 | 0 | decompression: Decompress, |
199 | 0 | ) -> ZlibDecoder<R> { |
200 | 0 | ZlibDecoder { |
201 | 0 | inner: bufread::ZlibDecoder::new_with_decompress( |
202 | 0 | BufReader::with_buf(buf, r), |
203 | 0 | decompression, |
204 | 0 | ), |
205 | 0 | } |
206 | 0 | } |
207 | | } |
208 | | |
209 | | impl<R> ZlibDecoder<R> { |
210 | | /// Resets the state of this decoder entirely, swapping out the input |
211 | | /// stream for another. |
212 | | /// |
213 | | /// This will reset the internal state of this decoder and replace the |
214 | | /// input stream with the one provided, returning the previous input |
215 | | /// stream. Future data read from this decoder will be the decompressed |
216 | | /// version of `r`'s data. |
217 | | /// |
218 | | /// Note that there may be currently buffered data when this function is |
219 | | /// called, and in that case the buffered data is discarded. |
220 | 0 | pub fn reset(&mut self, r: R) -> R { |
221 | 0 | super::bufread::reset_decoder_data(&mut self.inner); |
222 | 0 | self.inner.get_mut().reset(r) |
223 | 0 | } |
224 | | |
225 | | /// Acquires a reference to the underlying stream |
226 | 0 | pub fn get_ref(&self) -> &R { |
227 | 0 | self.inner.get_ref().get_ref() |
228 | 0 | } |
229 | | |
230 | | /// Acquires a mutable reference to the underlying stream |
231 | | /// |
232 | | /// Note that mutation of the stream may result in surprising results if |
233 | | /// this decoder is continued to be used. |
234 | 0 | pub fn get_mut(&mut self) -> &mut R { |
235 | 0 | self.inner.get_mut().get_mut() |
236 | 0 | } |
237 | | |
238 | | /// Consumes this decoder, returning the underlying reader. |
239 | | /// |
240 | | /// Note that there may be buffered bytes which are not re-acquired as part |
241 | | /// of this transition. It's recommended to only call this function after |
242 | | /// EOF has been reached. |
243 | 0 | pub fn into_inner(self) -> R { |
244 | 0 | self.inner.into_inner().into_inner() |
245 | 0 | } |
246 | | |
247 | | /// Returns the number of bytes that the decompressor has consumed. |
248 | | /// |
249 | | /// Note that this will likely be smaller than what the decompressor |
250 | | /// actually read from the underlying stream due to buffering. |
251 | 0 | pub fn total_in(&self) -> u64 { |
252 | 0 | self.inner.total_in() |
253 | 0 | } |
254 | | |
255 | | /// Returns the number of bytes that the decompressor has produced. |
256 | 0 | pub fn total_out(&self) -> u64 { |
257 | 0 | self.inner.total_out() |
258 | 0 | } |
259 | | } |
260 | | |
261 | | impl<R: Read> Read for ZlibDecoder<R> { |
262 | 0 | fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { |
263 | 0 | self.inner.read(into) |
264 | 0 | } |
265 | | } |
266 | | |
267 | | impl<R: Read + Write> Write for ZlibDecoder<R> { |
268 | 0 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
269 | 0 | self.get_mut().write(buf) |
270 | 0 | } |
271 | | |
272 | 0 | fn flush(&mut self) -> io::Result<()> { |
273 | 0 | self.get_mut().flush() |
274 | 0 | } |
275 | | } |