Skip to content

Commit d6fdbca

Browse files
committed
refactor the deflate, gz and zlib modules
1 parent e534385 commit d6fdbca

16 files changed

+3509
-3428
lines changed

src/deflate.rs

-1,058
This file was deleted.

src/deflate/bufread.rs

+262
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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 DEFLATE 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 std::io;
25+
/// use flate2::Compression;
26+
/// use flate2::bufread::DeflateEncoder;
27+
/// use std::fs::File;
28+
/// use std::io::BufReader;
29+
///
30+
/// # fn main() {
31+
/// # println!("{:?}", open_hello_world().unwrap());
32+
/// # }
33+
/// #
34+
/// // Opens sample file, compresses the contents and returns a Vector
35+
/// fn open_hello_world() -> io::Result<Vec<u8>> {
36+
/// let f = File::open("examples/hello_world.txt")?;
37+
/// let b = BufReader::new(f);
38+
/// let mut deflater = DeflateEncoder::new(b, Compression::Fast);
39+
/// let mut buffer = Vec::new();
40+
/// deflater.read_to_end(&mut buffer)?;
41+
/// Ok(buffer)
42+
/// }
43+
/// ```
44+
#[derive(Debug)]
45+
pub struct DeflateEncoder<R> {
46+
pub(crate) obj: R,
47+
pub(crate) data: Compress,
48+
}
49+
50+
51+
impl<R: BufRead> DeflateEncoder<R> {
52+
/// Creates a new encoder which will read uncompressed data from the given
53+
/// stream and emit the compressed stream.
54+
pub fn new(r: R, level: ::Compression) -> DeflateEncoder<R> {
55+
DeflateEncoder {
56+
obj: r,
57+
data: Compress::new(level, false),
58+
}
59+
}
60+
}
61+
62+
impl<R> DeflateEncoder<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+
self.data.reset();
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 DeflateEncoder<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 DeflateEncoder<R> {}
118+
119+
impl<W: BufRead + Write> Write for DeflateEncoder<W> {
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 DeflateEncoder<R> {
131+
fn shutdown(&mut self) -> Poll<(), io::Error> {
132+
self.get_mut().shutdown()
133+
}
134+
}
135+
136+
/// A DEFLATE 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::DeflateEncoder;
150+
/// use flate2::bufread::DeflateDecoder;
151+
///
152+
/// # fn main() {
153+
/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default);
154+
/// # e.write(b"Hello World").unwrap();
155+
/// # let bytes = e.finish().unwrap();
156+
/// # println!("{}", decode_reader(bytes).unwrap());
157+
/// # }
158+
/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error
159+
/// // Here &[u8] implements Read
160+
/// fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
161+
/// let mut deflater = DeflateDecoder::new(&bytes[..]);
162+
/// let mut s = String::new();
163+
/// deflater.read_to_string(&mut s)?;
164+
/// Ok(s)
165+
/// }
166+
/// ```
167+
#[derive(Debug)]
168+
pub struct DeflateDecoder<R> {
169+
pub(crate) obj: R,
170+
pub(crate) data: Decompress,
171+
}
172+
173+
174+
impl<R: BufRead> DeflateDecoder<R> {
175+
/// Creates a new decoder which will decompress data read from the given
176+
/// stream.
177+
pub fn new(r: R) -> DeflateDecoder<R> {
178+
DeflateDecoder {
179+
obj: r,
180+
data: Decompress::new(false),
181+
}
182+
}
183+
}
184+
185+
impl<R> DeflateDecoder<R> {
186+
/// Resets the state of this decoder entirely, swapping out the input
187+
/// stream for another.
188+
///
189+
/// This will reset the internal state of this decoder and replace the
190+
/// input stream with the one provided, returning the previous input
191+
/// stream. Future data read from this decoder will be the decompressed
192+
/// version of `r`'s data.
193+
pub fn reset(&mut self, r: R) -> R {
194+
self.data = Decompress::new(false);
195+
mem::replace(&mut self.obj, r)
196+
}
197+
198+
/// Resets the state of this decoder's data
199+
///
200+
/// This will reset the internal state of this decoder. It will continue
201+
/// reading from the same stream.
202+
pub fn reset_data(&mut self) {
203+
self.data = Decompress::new(false);
204+
}
205+
206+
/// Acquires a reference to the underlying stream
207+
pub fn get_ref(&self) -> &R {
208+
&self.obj
209+
}
210+
211+
/// Acquires a mutable reference to the underlying stream
212+
///
213+
/// Note that mutation of the stream may result in surprising results if
214+
/// this encoder is continued to be used.
215+
pub fn get_mut(&mut self) -> &mut R {
216+
&mut self.obj
217+
}
218+
219+
/// Consumes this decoder, returning the underlying reader.
220+
pub fn into_inner(self) -> R {
221+
self.obj
222+
}
223+
224+
/// Returns the number of bytes that the decompressor has consumed.
225+
///
226+
/// Note that this will likely be smaller than what the decompressor
227+
/// actually read from the underlying stream due to buffering.
228+
pub fn total_in(&self) -> u64 {
229+
self.data.total_in()
230+
}
231+
232+
/// Returns the number of bytes that the decompressor has produced.
233+
pub fn total_out(&self) -> u64 {
234+
self.data.total_out()
235+
}
236+
}
237+
238+
impl<R: BufRead> Read for DeflateDecoder<R> {
239+
fn read(&mut self, into: &mut [u8]) -> io::Result<usize> {
240+
zio::read(&mut self.obj, &mut self.data, into)
241+
}
242+
}
243+
244+
#[cfg(feature = "tokio")]
245+
impl<R: AsyncRead + BufRead> AsyncRead for DeflateDecoder<R> {}
246+
247+
impl<W: BufRead + Write> Write for DeflateDecoder<W> {
248+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
249+
self.get_mut().write(buf)
250+
}
251+
252+
fn flush(&mut self) -> io::Result<()> {
253+
self.get_mut().flush()
254+
}
255+
}
256+
257+
#[cfg(feature = "tokio")]
258+
impl<R: AsyncWrite + BufRead> AsyncWrite for DeflateDecoder<R> {
259+
fn shutdown(&mut self) -> Poll<(), io::Error> {
260+
self.get_mut().shutdown()
261+
}
262+
}

0 commit comments

Comments
 (0)