]> git.proxmox.com Git - rustc.git/blob - src/vendor/flate2/src/zlib/bufread.rs
New upstream version 1.22.1+dfsg1
[rustc.git] / src / vendor / flate2 / src / zlib / bufread.rs
1 use std::io::prelude::*;
2 use std::io;
3 use std::mem;
4
5 #[cfg(feature = "tokio")]
6 use futures::Poll;
7 #[cfg(feature = "tokio")]
8 use tokio_io::{AsyncRead, AsyncWrite};
9
10 use zio;
11 use {Compress, Decompress};
12
13 /// A ZLIB encoder, or compressor.
14 ///
15 /// This structure implements a [`BufRead`] interface and will read uncompressed
16 /// data from an underlying stream and emit a stream of compressed data.
17 ///
18 /// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
19 ///
20 /// # Examples
21 ///
22 /// ```
23 /// use std::io::prelude::*;
24 /// use flate2::Compression;
25 /// use flate2::bufread::ZlibEncoder;
26 /// use std::fs::File;
27 /// use std::io::BufReader;
28 ///
29 /// // Use a buffered file to compress contents into a Vec<u8>
30 ///
31 /// # fn open_hello_world() -> std::io::Result<Vec<u8>> {
32 /// let f = File::open("examples/hello_world.txt")?;
33 /// let b = BufReader::new(f);
34 /// let mut z = ZlibEncoder::new(b, Compression::Fast);
35 /// let mut buffer = Vec::new();
36 /// z.read_to_end(&mut buffer)?;
37 /// # Ok(buffer)
38 /// # }
39 /// ```
40 #[derive(Debug)]
41 pub struct ZlibEncoder<R> {
42 obj: R,
43 data: Compress,
44 }
45
46
47 impl<R: BufRead> ZlibEncoder<R> {
48 /// Creates a new encoder which will read uncompressed data from the given
49 /// stream and emit the compressed stream.
50 pub fn new(r: R, level: ::Compression) -> ZlibEncoder<R> {
51 ZlibEncoder {
52 obj: r,
53 data: Compress::new(level, true),
54 }
55 }
56 }
57
58 pub fn reset_encoder_data<R>(zlib: &mut ZlibEncoder<R>) {
59 zlib.data.reset()
60 }
61
62 impl<R> ZlibEncoder<R> {
63 /// Resets the state of this encoder entirely, swapping out the input
64 /// stream for another.
65 ///
66 /// This function will reset the internal state of this encoder and replace
67 /// the input stream with the one provided, returning the previous input
68 /// stream. Future data read from this encoder will be the compressed
69 /// version of `r`'s data.
70 pub fn reset(&mut self, r: R) -> R {
71 reset_encoder_data(self);
72 mem::replace(&mut self.obj, r)
73 }
74
75 /// Acquires a reference to the underlying reader
76 pub fn get_ref(&self) -> &R {
77 &self.obj
78 }
79
80 /// Acquires a mutable reference to the underlying stream
81 ///
82 /// Note that mutation of the stream may result in surprising results if
83 /// this encoder is continued to be used.
84 pub fn get_mut(&mut self) -> &mut R {
85 &mut self.obj
86 }
87
88 /// Consumes this encoder, returning the underlying reader.
89 pub fn into_inner(self) -> R {
90 self.obj
91 }
92
93 /// Returns the number of bytes that have been read into this compressor.
94 ///
95 /// Note that not all bytes read from the underlying object may be accounted
96 /// for, there may still be some active buffering.
97 pub fn total_in(&self) -> u64 {
98 self.data.total_in()
99 }
100
101 /// Returns the number of bytes that the compressor has produced.
102 ///
103 /// Note that not all bytes may have been read yet, some may still be
104 /// buffered.
105 pub fn total_out(&self) -> u64 {
106 self.data.total_out()
107 }
108 }
109
110 impl<R: BufRead> Read for ZlibEncoder<R> {
111 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
112 zio::read(&mut self.obj, &mut self.data, buf)
113 }
114 }
115
116 #[cfg(feature = "tokio")]
117 impl<R: AsyncRead + BufRead> AsyncRead for ZlibEncoder<R> {}
118
119 impl<R: BufRead + Write> Write for ZlibEncoder<R> {
120 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
121 self.get_mut().write(buf)
122 }
123
124 fn flush(&mut self) -> io::Result<()> {
125 self.get_mut().flush()
126 }
127 }
128
129 #[cfg(feature = "tokio")]
130 impl<R: AsyncWrite + BufRead> AsyncWrite for ZlibEncoder<R> {
131 fn shutdown(&mut self) -> Poll<(), io::Error> {
132 self.get_mut().shutdown()
133 }
134 }
135
136 /// A ZLIB decoder, or decompressor.
137 ///
138 /// This structure implements a [`BufRead`] interface and takes a stream of
139 /// compressed data as input, providing the decompressed data when read from.
140 ///
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(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(true),
182 }
183 }
184 }
185
186 pub fn reset_decoder_data<R>(zlib: &mut ZlibDecoder<R>) {
187 zlib.data = Decompress::new(true);
188 }
189
190 impl<R> ZlibDecoder<R> {
191 /// Resets the state of this decoder entirely, swapping out the input
192 /// stream for another.
193 ///
194 /// This will reset the internal state of this decoder and replace the
195 /// input stream with the one provided, returning the previous input
196 /// stream. Future data read from this decoder will be the decompressed
197 /// version of `r`'s data.
198 pub fn reset(&mut self, r: R) -> R {
199 reset_decoder_data(self);
200 mem::replace(&mut self.obj, r)
201 }
202
203 /// Acquires a reference to the underlying stream
204 pub fn get_ref(&self) -> &R {
205 &self.obj
206 }
207
208 /// Acquires a mutable reference to the underlying stream
209 ///
210 /// Note that mutation of the stream may result in surprising results if
211 /// this encoder is continued to be used.
212 pub fn get_mut(&mut self) -> &mut R {
213 &mut self.obj
214 }
215
216 /// Consumes this decoder, returning the underlying reader.
217 pub fn into_inner(self) -> R {
218 self.obj
219 }
220
221 /// Returns the number of bytes that the decompressor has consumed.
222 ///
223 /// Note that this will likely be smaller than what the decompressor
224 /// actually read from the underlying stream due to buffering.
225 pub fn total_in(&self) -> u64 {
226 self.data.total_in()
227 }
228
229 /// Returns the number of bytes that the decompressor has produced.
230 pub fn total_out(&self) -> u64 {
231 self.data.total_out()
232 }
233 }
234
235 impl<R: BufRead> Read for ZlibDecoder<R> {
236 fn read(&mut self, into: &mut [u8]) -> io::Result<usize> {
237 zio::read(&mut self.obj, &mut self.data, into)
238 }
239 }
240
241 #[cfg(feature = "tokio")]
242 impl<R: AsyncRead + BufRead> AsyncRead for ZlibDecoder<R> {}
243
244 impl<R: BufRead + Write> Write for ZlibDecoder<R> {
245 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
246 self.get_mut().write(buf)
247 }
248
249 fn flush(&mut self) -> io::Result<()> {
250 self.get_mut().flush()
251 }
252 }
253
254 #[cfg(feature = "tokio")]
255 impl<R: AsyncWrite + BufRead> AsyncWrite for ZlibDecoder<R> {
256 fn shutdown(&mut self) -> Poll<(), io::Error> {
257 self.get_mut().shutdown()
258 }
259 }