diff options
author | Valentin Popov <valentin@popov.link> | 2024-07-19 15:37:58 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-07-19 15:37:58 +0300 |
commit | a990de90fe41456a23e58bd087d2f107d321f3a1 (patch) | |
tree | 15afc392522a9e85dc3332235e311b7d39352ea9 /vendor/flate2/src/deflate | |
parent | 3d48cd3f81164bbfc1a755dc1d4a9a02f98c8ddd (diff) | |
download | fparkan-a990de90fe41456a23e58bd087d2f107d321f3a1.tar.xz fparkan-a990de90fe41456a23e58bd087d2f107d321f3a1.zip |
Deleted vendor folder
Diffstat (limited to 'vendor/flate2/src/deflate')
-rw-r--r-- | vendor/flate2/src/deflate/bufread.rs | 245 | ||||
-rw-r--r-- | vendor/flate2/src/deflate/mod.rs | 193 | ||||
-rw-r--r-- | vendor/flate2/src/deflate/read.rs | 241 | ||||
-rw-r--r-- | vendor/flate2/src/deflate/write.rs | 322 |
4 files changed, 0 insertions, 1001 deletions
diff --git a/vendor/flate2/src/deflate/bufread.rs b/vendor/flate2/src/deflate/bufread.rs deleted file mode 100644 index c70a630..0000000 --- a/vendor/flate2/src/deflate/bufread.rs +++ /dev/null @@ -1,245 +0,0 @@ -use std::io; -use std::io::prelude::*; -use std::mem; - -use crate::zio; -use crate::{Compress, Decompress}; - -/// A DEFLATE encoder, or compressor. -/// -/// This structure implements a [`Read`] interface. When read from, it reads -/// uncompressed data from the underlying [`BufRead`] and provides the compressed data. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// use flate2::Compression; -/// use flate2::bufread::DeflateEncoder; -/// use std::fs::File; -/// use std::io::BufReader; -/// -/// # fn main() { -/// # println!("{:?}", open_hello_world().unwrap()); -/// # } -/// # -/// // Opens sample file, compresses the contents and returns a Vector -/// fn open_hello_world() -> io::Result<Vec<u8>> { -/// let f = File::open("examples/hello_world.txt")?; -/// let b = BufReader::new(f); -/// let mut deflater = DeflateEncoder::new(b, Compression::fast()); -/// let mut buffer = Vec::new(); -/// deflater.read_to_end(&mut buffer)?; -/// Ok(buffer) -/// } -/// ``` -#[derive(Debug)] -pub struct DeflateEncoder<R> { - obj: R, - data: Compress, -} - -impl<R: BufRead> DeflateEncoder<R> { - /// Creates a new encoder which will read uncompressed data from the given - /// stream and emit the compressed stream. - pub fn new(r: R, level: crate::Compression) -> DeflateEncoder<R> { - DeflateEncoder { - obj: r, - data: Compress::new(level, false), - } - } -} - -pub fn reset_encoder_data<R>(zlib: &mut DeflateEncoder<R>) { - zlib.data.reset(); -} - -impl<R> DeflateEncoder<R> { - /// Resets the state of this encoder entirely, swapping out the input - /// stream for another. - /// - /// This function will reset the internal state of this encoder and replace - /// the input stream with the one provided, returning the previous input - /// stream. Future data read from this encoder will be the compressed - /// version of `r`'s data. - pub fn reset(&mut self, r: R) -> R { - reset_encoder_data(self); - mem::replace(&mut self.obj, r) - } - - /// Acquires a reference to the underlying reader - pub fn get_ref(&self) -> &R { - &self.obj - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - &mut self.obj - } - - /// Consumes this encoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.obj - } - - /// Returns the number of bytes that have been read into this compressor. - /// - /// Note that not all bytes read from the underlying object may be accounted - /// for, there may still be some active buffering. - pub fn total_in(&self) -> u64 { - self.data.total_in() - } - - /// Returns the number of bytes that the compressor has produced. - /// - /// Note that not all bytes may have been read yet, some may still be - /// buffered. - pub fn total_out(&self) -> u64 { - self.data.total_out() - } -} - -impl<R: BufRead> Read for DeflateEncoder<R> { - fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { - zio::read(&mut self.obj, &mut self.data, buf) - } -} - -impl<W: BufRead + Write> Write for DeflateEncoder<W> { - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -/// A DEFLATE decoder, or decompressor. -/// -/// This structure implements a [`Read`] interface. When read from, it reads -/// compressed data from the underlying [`BufRead`] and provides the uncompressed data. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::DeflateEncoder; -/// use flate2::bufread::DeflateDecoder; -/// -/// # fn main() { -/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); -/// # e.write_all(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements Read -/// fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { -/// let mut deflater = DeflateDecoder::new(&bytes[..]); -/// let mut s = String::new(); -/// deflater.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct DeflateDecoder<R> { - obj: R, - data: Decompress, -} - -pub fn reset_decoder_data<R>(zlib: &mut DeflateDecoder<R>) { - zlib.data = Decompress::new(false); -} - -impl<R: BufRead> DeflateDecoder<R> { - /// Creates a new decoder which will decompress data read from the given - /// stream. - pub fn new(r: R) -> DeflateDecoder<R> { - DeflateDecoder { - obj: r, - data: Decompress::new(false), - } - } -} - -impl<R> DeflateDecoder<R> { - /// Resets the state of this decoder entirely, swapping out the input - /// stream for another. - /// - /// This will reset the internal state of this decoder and replace the - /// input stream with the one provided, returning the previous input - /// stream. Future data read from this decoder will be the decompressed - /// version of `r`'s data. - pub fn reset(&mut self, r: R) -> R { - reset_decoder_data(self); - mem::replace(&mut self.obj, r) - } - - /// Resets the state of this decoder's data - /// - /// This will reset the internal state of this decoder. It will continue - /// reading from the same stream. - pub fn reset_data(&mut self) { - reset_decoder_data(self); - } - - /// Acquires a reference to the underlying stream - pub fn get_ref(&self) -> &R { - &self.obj - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this decoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - &mut self.obj - } - - /// Consumes this decoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.obj - } - - /// Returns the number of bytes that the decompressor has consumed. - /// - /// Note that this will likely be smaller than what the decompressor - /// actually read from the underlying stream due to buffering. - pub fn total_in(&self) -> u64 { - self.data.total_in() - } - - /// Returns the number of bytes that the decompressor has produced. - pub fn total_out(&self) -> u64 { - self.data.total_out() - } -} - -impl<R: BufRead> Read for DeflateDecoder<R> { - fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { - zio::read(&mut self.obj, &mut self.data, into) - } -} - -impl<W: BufRead + Write> Write for DeflateDecoder<W> { - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} diff --git a/vendor/flate2/src/deflate/mod.rs b/vendor/flate2/src/deflate/mod.rs deleted file mode 100644 index 7f3bf70..0000000 --- a/vendor/flate2/src/deflate/mod.rs +++ /dev/null @@ -1,193 +0,0 @@ -pub mod bufread; -pub mod read; -pub mod write; - -#[cfg(test)] -mod tests { - use std::io::prelude::*; - - use rand::{thread_rng, Rng}; - - use super::{read, write}; - use crate::Compression; - - #[test] - fn roundtrip() { - let mut real = Vec::new(); - let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); - let v = crate::random_bytes().take(1024).collect::<Vec<_>>(); - for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0..v.len())]; - real.extend(to_write.iter().copied()); - w.write_all(to_write).unwrap(); - } - let result = w.finish().unwrap(); - let mut r = read::DeflateDecoder::new(&result[..]); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert_eq!(ret, real); - } - - #[test] - fn drop_writes() { - let mut data = Vec::new(); - write::DeflateEncoder::new(&mut data, Compression::default()) - .write_all(b"foo") - .unwrap(); - let mut r = read::DeflateDecoder::new(&data[..]); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert_eq!(ret, b"foo"); - } - - #[test] - fn total_in() { - let mut real = Vec::new(); - let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); - let v = crate::random_bytes().take(1024).collect::<Vec<_>>(); - for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0..v.len())]; - real.extend(to_write.iter().copied()); - w.write_all(to_write).unwrap(); - } - let mut result = w.finish().unwrap(); - - let result_len = result.len(); - - for _ in 0..200 { - result.extend(v.iter().copied()); - } - - let mut r = read::DeflateDecoder::new(&result[..]); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert_eq!(ret, real); - assert_eq!(r.total_in(), result_len as u64); - } - - #[test] - fn roundtrip2() { - let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); - let mut r = - read::DeflateDecoder::new(read::DeflateEncoder::new(&v[..], Compression::default())); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert_eq!(ret, v); - } - - #[test] - fn roundtrip3() { - let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); - let mut w = write::DeflateEncoder::new( - write::DeflateDecoder::new(Vec::new()), - Compression::default(), - ); - w.write_all(&v).unwrap(); - let w = w.finish().unwrap().finish().unwrap(); - assert_eq!(w, v); - } - - #[test] - fn reset_writer() { - let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); - let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); - w.write_all(&v).unwrap(); - let a = w.reset(Vec::new()).unwrap(); - w.write_all(&v).unwrap(); - let b = w.finish().unwrap(); - - let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); - w.write_all(&v).unwrap(); - let c = w.finish().unwrap(); - assert!(a == b && b == c); - } - - #[test] - fn reset_reader() { - let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); - let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); - let mut r = read::DeflateEncoder::new(&v[..], Compression::default()); - r.read_to_end(&mut a).unwrap(); - r.reset(&v[..]); - r.read_to_end(&mut b).unwrap(); - - let mut r = read::DeflateEncoder::new(&v[..], Compression::default()); - r.read_to_end(&mut c).unwrap(); - assert!(a == b && b == c); - } - - #[test] - fn reset_decoder() { - let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); - let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); - w.write_all(&v).unwrap(); - let data = w.finish().unwrap(); - - { - let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); - let mut r = read::DeflateDecoder::new(&data[..]); - r.read_to_end(&mut a).unwrap(); - r.reset(&data); - r.read_to_end(&mut b).unwrap(); - - let mut r = read::DeflateDecoder::new(&data[..]); - r.read_to_end(&mut c).unwrap(); - assert!(a == b && b == c && c == v); - } - - { - let mut w = write::DeflateDecoder::new(Vec::new()); - w.write_all(&data).unwrap(); - let a = w.reset(Vec::new()).unwrap(); - w.write_all(&data).unwrap(); - let b = w.finish().unwrap(); - - let mut w = write::DeflateDecoder::new(Vec::new()); - w.write_all(&data).unwrap(); - let c = w.finish().unwrap(); - assert!(a == b && b == c && c == v); - } - } - - #[test] - fn zero_length_read_with_data() { - let m = vec![3u8; 128 * 1024 + 1]; - let mut c = read::DeflateEncoder::new(&m[..], Compression::default()); - - let mut result = Vec::new(); - c.read_to_end(&mut result).unwrap(); - - let mut d = read::DeflateDecoder::new(&result[..]); - let mut data = Vec::new(); - assert_eq!(d.read(&mut data).unwrap(), 0); - } - - #[test] - fn qc_reader() { - ::quickcheck::quickcheck(test as fn(_) -> _); - - fn test(v: Vec<u8>) -> bool { - let mut r = read::DeflateDecoder::new(read::DeflateEncoder::new( - &v[..], - Compression::default(), - )); - let mut v2 = Vec::new(); - r.read_to_end(&mut v2).unwrap(); - v == v2 - } - } - - #[test] - fn qc_writer() { - ::quickcheck::quickcheck(test as fn(_) -> _); - - fn test(v: Vec<u8>) -> bool { - let mut w = write::DeflateEncoder::new( - write::DeflateDecoder::new(Vec::new()), - Compression::default(), - ); - w.write_all(&v).unwrap(); - v == w.finish().unwrap().finish().unwrap() - } - } -} diff --git a/vendor/flate2/src/deflate/read.rs b/vendor/flate2/src/deflate/read.rs deleted file mode 100644 index 2b6b8f2..0000000 --- a/vendor/flate2/src/deflate/read.rs +++ /dev/null @@ -1,241 +0,0 @@ -use std::io; -use std::io::prelude::*; - -use super::bufread; -use crate::bufreader::BufReader; - -/// A DEFLATE encoder, or compressor. -/// -/// This structure implements a [`Read`] interface. When read from, it reads -/// uncompressed data from the underlying [`Read`] and provides the compressed data. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// use flate2::Compression; -/// use flate2::read::DeflateEncoder; -/// -/// # fn main() { -/// # println!("{:?}", deflateencoder_read_hello_world().unwrap()); -/// # } -/// # -/// // Return a vector containing the Deflate compressed version of hello world -/// fn deflateencoder_read_hello_world() -> io::Result<Vec<u8>> { -/// let mut ret_vec = Vec::new(); -/// let c = b"hello world"; -/// let mut deflater = DeflateEncoder::new(&c[..], Compression::fast()); -/// deflater.read_to_end(&mut ret_vec)?; -/// Ok(ret_vec) -/// } -/// ``` -#[derive(Debug)] -pub struct DeflateEncoder<R> { - inner: bufread::DeflateEncoder<BufReader<R>>, -} - -impl<R: Read> DeflateEncoder<R> { - /// Creates a new encoder which will read uncompressed data from the given - /// stream and emit the compressed stream. - pub fn new(r: R, level: crate::Compression) -> DeflateEncoder<R> { - DeflateEncoder { - inner: bufread::DeflateEncoder::new(BufReader::new(r), level), - } - } -} - -impl<R> DeflateEncoder<R> { - /// Resets the state of this encoder entirely, swapping out the input - /// stream for another. - /// - /// This function will reset the internal state of this encoder and replace - /// the input stream with the one provided, returning the previous input - /// stream. Future data read from this encoder will be the compressed - /// version of `r`'s data. - /// - /// Note that there may be currently buffered data when this function is - /// called, and in that case the buffered data is discarded. - pub fn reset(&mut self, r: R) -> R { - super::bufread::reset_encoder_data(&mut self.inner); - self.inner.get_mut().reset(r) - } - - /// Acquires a reference to the underlying reader - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this encoder, returning the underlying reader. - /// - /// Note that there may be buffered bytes which are not re-acquired as part - /// of this transition. It's recommended to only call this function after - /// EOF has been reached. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } - - /// Returns the number of bytes that have been read into this compressor. - /// - /// Note that not all bytes read from the underlying object may be accounted - /// for, there may still be some active buffering. - pub fn total_in(&self) -> u64 { - self.inner.total_in() - } - - /// Returns the number of bytes that the compressor has produced. - /// - /// Note that not all bytes may have been read yet, some may still be - /// buffered. - pub fn total_out(&self) -> u64 { - self.inner.total_out() - } -} - -impl<R: Read> Read for DeflateEncoder<R> { - fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { - self.inner.read(buf) - } -} - -impl<W: Read + Write> Write for DeflateEncoder<W> { - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -/// A DEFLATE decoder, or decompressor. -/// -/// This structure implements a [`Read`] interface. When read from, it reads -/// compressed data from the underlying [`Read`] and provides the uncompressed data. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::DeflateEncoder; -/// use flate2::read::DeflateDecoder; -/// -/// # fn main() { -/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); -/// # e.write_all(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements Read -/// fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { -/// let mut deflater = DeflateDecoder::new(&bytes[..]); -/// let mut s = String::new(); -/// deflater.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct DeflateDecoder<R> { - inner: bufread::DeflateDecoder<BufReader<R>>, -} - -impl<R: Read> DeflateDecoder<R> { - /// Creates a new decoder which will decompress data read from the given - /// stream. - pub fn new(r: R) -> DeflateDecoder<R> { - DeflateDecoder::new_with_buf(r, vec![0; 32 * 1024]) - } - - /// Same as `new`, but the intermediate buffer for data is specified. - /// - /// Note that the capacity of the intermediate buffer is never increased, - /// and it is recommended for it to be large. - pub fn new_with_buf(r: R, buf: Vec<u8>) -> DeflateDecoder<R> { - DeflateDecoder { - inner: bufread::DeflateDecoder::new(BufReader::with_buf(buf, r)), - } - } -} - -impl<R> DeflateDecoder<R> { - /// Resets the state of this decoder entirely, swapping out the input - /// stream for another. - /// - /// This will reset the internal state of this decoder and replace the - /// input stream with the one provided, returning the previous input - /// stream. Future data read from this decoder will be the decompressed - /// version of `r`'s data. - /// - /// Note that there may be currently buffered data when this function is - /// called, and in that case the buffered data is discarded. - pub fn reset(&mut self, r: R) -> R { - super::bufread::reset_decoder_data(&mut self.inner); - self.inner.get_mut().reset(r) - } - - /// Acquires a reference to the underlying stream - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this decoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this decoder, returning the underlying reader. - /// - /// Note that there may be buffered bytes which are not re-acquired as part - /// of this transition. It's recommended to only call this function after - /// EOF has been reached. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } - - /// Returns the number of bytes that the decompressor has consumed. - /// - /// Note that this will likely be smaller than what the decompressor - /// actually read from the underlying stream due to buffering. - pub fn total_in(&self) -> u64 { - self.inner.total_in() - } - - /// Returns the number of bytes that the decompressor has produced. - pub fn total_out(&self) -> u64 { - self.inner.total_out() - } -} - -impl<R: Read> Read for DeflateDecoder<R> { - fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { - self.inner.read(into) - } -} - -impl<W: Read + Write> Write for DeflateDecoder<W> { - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} diff --git a/vendor/flate2/src/deflate/write.rs b/vendor/flate2/src/deflate/write.rs deleted file mode 100644 index 2c44556..0000000 --- a/vendor/flate2/src/deflate/write.rs +++ /dev/null @@ -1,322 +0,0 @@ -use std::io; -use std::io::prelude::*; - -use crate::zio; -use crate::{Compress, Decompress}; - -/// A DEFLATE encoder, or compressor. -/// -/// This structure implements a [`Write`] interface and takes a stream of -/// uncompressed data, writing the compressed data to the wrapped writer. -/// -/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use flate2::Compression; -/// use flate2::write::DeflateEncoder; -/// -/// // Vec<u8> implements Write to print the compressed bytes of sample string -/// # fn main() { -/// -/// let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); -/// e.write_all(b"Hello World").unwrap(); -/// println!("{:?}", e.finish().unwrap()); -/// # } -/// ``` -#[derive(Debug)] -pub struct DeflateEncoder<W: Write> { - inner: zio::Writer<W, Compress>, -} - -impl<W: Write> DeflateEncoder<W> { - /// Creates a new encoder which will write compressed data to the stream - /// given at the given compression level. - /// - /// When this encoder is dropped or unwrapped the final pieces of data will - /// be flushed. - pub fn new(w: W, level: crate::Compression) -> DeflateEncoder<W> { - DeflateEncoder { - inner: zio::Writer::new(w, Compress::new(level, false)), - } - } - - /// Acquires a reference to the underlying writer. - pub fn get_ref(&self) -> &W { - self.inner.get_ref() - } - - /// Acquires a mutable reference to the underlying writer. - /// - /// Note that mutating the output/input state of the stream may corrupt this - /// object, so care must be taken when using this method. - pub fn get_mut(&mut self) -> &mut W { - self.inner.get_mut() - } - - /// Resets the state of this encoder entirely, swapping out the output - /// stream for another. - /// - /// This function will finish encoding the current stream into the current - /// output stream before swapping out the two output streams. If the stream - /// cannot be finished an error is returned. - /// - /// After the current stream has been finished, this will reset the internal - /// state of this encoder and replace the output stream with the one - /// provided, returning the previous output stream. Future data written to - /// this encoder will be the compressed into the stream `w` provided. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn reset(&mut self, w: W) -> io::Result<W> { - self.inner.finish()?; - self.inner.data.reset(); - Ok(self.inner.replace(w)) - } - - /// Attempt to finish this output stream, writing out final chunks of data. - /// - /// Note that this function can only be used once data has finished being - /// written to the output stream. After this function is called then further - /// calls to `write` may result in a panic. - /// - /// # Panics - /// - /// Attempts to write data to this stream may result in a panic after this - /// function is called. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn try_finish(&mut self) -> io::Result<()> { - self.inner.finish() - } - - /// Consumes this encoder, flushing the output stream. - /// - /// This will flush the underlying data stream, close off the compressed - /// stream and, if successful, return the contained writer. - /// - /// Note that this function may not be suitable to call in a situation where - /// the underlying stream is an asynchronous I/O stream. To finish a stream - /// the `try_finish` (or `shutdown`) method should be used instead. To - /// re-acquire ownership of a stream it is safe to call this method after - /// `try_finish` or `shutdown` has returned `Ok`. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn finish(mut self) -> io::Result<W> { - self.inner.finish()?; - Ok(self.inner.take_inner()) - } - - /// Consumes this encoder, flushing the output stream. - /// - /// This will flush the underlying data stream and then return the contained - /// writer if the flush succeeded. - /// The compressed stream will not closed but only flushed. This - /// means that obtained byte array can by extended by another deflated - /// stream. To close the stream add the two bytes 0x3 and 0x0. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn flush_finish(mut self) -> io::Result<W> { - self.inner.flush()?; - Ok(self.inner.take_inner()) - } - - /// Returns the number of bytes that have been written to this compressor. - /// - /// Note that not all bytes written to this object may be accounted for, - /// there may still be some active buffering. - pub fn total_in(&self) -> u64 { - self.inner.data.total_in() - } - - /// Returns the number of bytes that the compressor has produced. - /// - /// Note that not all bytes may have been written yet, some may still be - /// buffered. - pub fn total_out(&self) -> u64 { - self.inner.data.total_out() - } -} - -impl<W: Write> Write for DeflateEncoder<W> { - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - self.inner.write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.inner.flush() - } -} - -impl<W: Read + Write> Read for DeflateEncoder<W> { - fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { - self.inner.get_mut().read(buf) - } -} - -/// A DEFLATE decoder, or decompressor. -/// -/// This structure implements a [`Write`] and will emit a stream of decompressed -/// data when fed a stream of compressed data. -/// -/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::DeflateEncoder; -/// use flate2::write::DeflateDecoder; -/// -/// # fn main() { -/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); -/// # e.write_all(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_writer(bytes).unwrap()); -/// # } -/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error -/// // Here Vec<u8> implements Write -/// fn decode_writer(bytes: Vec<u8>) -> io::Result<String> { -/// let mut writer = Vec::new(); -/// let mut deflater = DeflateDecoder::new(writer); -/// deflater.write_all(&bytes[..])?; -/// writer = deflater.finish()?; -/// let return_string = String::from_utf8(writer).expect("String parsing error"); -/// Ok(return_string) -/// } -/// ``` -#[derive(Debug)] -pub struct DeflateDecoder<W: Write> { - inner: zio::Writer<W, Decompress>, -} - -impl<W: Write> DeflateDecoder<W> { - /// Creates a new decoder which will write uncompressed data to the stream. - /// - /// When this encoder is dropped or unwrapped the final pieces of data will - /// be flushed. - pub fn new(w: W) -> DeflateDecoder<W> { - DeflateDecoder { - inner: zio::Writer::new(w, Decompress::new(false)), - } - } - - /// Acquires a reference to the underlying writer. - pub fn get_ref(&self) -> &W { - self.inner.get_ref() - } - - /// Acquires a mutable reference to the underlying writer. - /// - /// Note that mutating the output/input state of the stream may corrupt this - /// object, so care must be taken when using this method. - pub fn get_mut(&mut self) -> &mut W { - self.inner.get_mut() - } - - /// Resets the state of this decoder entirely, swapping out the output - /// stream for another. - /// - /// This function will finish encoding the current stream into the current - /// output stream before swapping out the two output streams. - /// - /// This will then reset the internal state of this decoder and replace the - /// output stream with the one provided, returning the previous output - /// stream. Future data written to this decoder will be decompressed into - /// the output stream `w`. - /// - /// # Errors - /// - /// This function will perform I/O to finish the stream, and if that I/O - /// returns an error then that will be returned from this function. - pub fn reset(&mut self, w: W) -> io::Result<W> { - self.inner.finish()?; - self.inner.data = Decompress::new(false); - Ok(self.inner.replace(w)) - } - - /// Attempt to finish this output stream, writing out final chunks of data. - /// - /// Note that this function can only be used once data has finished being - /// written to the output stream. After this function is called then further - /// calls to `write` may result in a panic. - /// - /// # Panics - /// - /// Attempts to write data to this stream may result in a panic after this - /// function is called. - /// - /// # Errors - /// - /// This function will perform I/O to finish the stream, returning any - /// errors which happen. - pub fn try_finish(&mut self) -> io::Result<()> { - self.inner.finish() - } - - /// Consumes this encoder, flushing the output stream. - /// - /// This will flush the underlying data stream and then return the contained - /// writer if the flush succeeded. - /// - /// Note that this function may not be suitable to call in a situation where - /// the underlying stream is an asynchronous I/O stream. To finish a stream - /// the `try_finish` (or `shutdown`) method should be used instead. To - /// re-acquire ownership of a stream it is safe to call this method after - /// `try_finish` or `shutdown` has returned `Ok`. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn finish(mut self) -> io::Result<W> { - self.inner.finish()?; - Ok(self.inner.take_inner()) - } - - /// Returns the number of bytes that the decompressor has consumed for - /// decompression. - /// - /// Note that this will likely be smaller than the number of bytes - /// successfully written to this stream due to internal buffering. - pub fn total_in(&self) -> u64 { - self.inner.data.total_in() - } - - /// Returns the number of bytes that the decompressor has written to its - /// output stream. - pub fn total_out(&self) -> u64 { - self.inner.data.total_out() - } -} - -impl<W: Write> Write for DeflateDecoder<W> { - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - self.inner.write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.inner.flush() - } -} - -impl<W: Read + Write> Read for DeflateDecoder<W> { - fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { - self.inner.get_mut().read(buf) - } -} |