]>
git.proxmox.com Git - rustc.git/blob - vendor/flate2/src/zlib/bufread.rs
2 use std
::io
::prelude
::*;
6 use crate::{Compress, Decompress}
;
8 /// A ZLIB encoder, or compressor.
10 /// This structure consumes a [`BufRead`] interface, reading uncompressed data
11 /// from the underlying reader, and emitting compressed data.
13 /// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
18 /// use std::io::prelude::*;
19 /// use flate2::Compression;
20 /// use flate2::bufread::ZlibEncoder;
21 /// use std::fs::File;
22 /// use std::io::BufReader;
24 /// // Use a buffered file to compress contents into a Vec<u8>
26 /// # fn open_hello_world() -> std::io::Result<Vec<u8>> {
27 /// let f = File::open("examples/hello_world.txt")?;
28 /// let b = BufReader::new(f);
29 /// let mut z = ZlibEncoder::new(b, Compression::fast());
30 /// let mut buffer = Vec::new();
31 /// z.read_to_end(&mut buffer)?;
36 pub struct ZlibEncoder
<R
> {
41 impl<R
: BufRead
> ZlibEncoder
<R
> {
42 /// Creates a new encoder which will read uncompressed data from the given
43 /// stream and emit the compressed stream.
44 pub fn new(r
: R
, level
: crate::Compression
) -> ZlibEncoder
<R
> {
47 data
: Compress
::new(level
, true),
52 pub fn reset_encoder_data
<R
>(zlib
: &mut ZlibEncoder
<R
>) {
56 impl<R
> ZlibEncoder
<R
> {
57 /// Resets the state of this encoder entirely, swapping out the input
58 /// stream for another.
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 pub fn reset(&mut self, r
: R
) -> R
{
65 reset_encoder_data(self);
66 mem
::replace(&mut self.obj
, r
)
69 /// Acquires a reference to the underlying reader
70 pub fn get_ref(&self) -> &R
{
74 /// Acquires a mutable reference to the underlying stream
76 /// Note that mutation of the stream may result in surprising results if
77 /// this encoder is continued to be used.
78 pub fn get_mut(&mut self) -> &mut R
{
82 /// Consumes this encoder, returning the underlying reader.
83 pub fn into_inner(self) -> R
{
87 /// Returns the number of bytes that have been read into this compressor.
89 /// Note that not all bytes read from the underlying object may be accounted
90 /// for, there may still be some active buffering.
91 pub fn total_in(&self) -> u64 {
95 /// Returns the number of bytes that the compressor has produced.
97 /// Note that not all bytes may have been read yet, some may still be
99 pub fn total_out(&self) -> u64 {
100 self.data
.total_out()
104 impl<R
: BufRead
> Read
for ZlibEncoder
<R
> {
105 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
106 zio
::read(&mut self.obj
, &mut self.data
, buf
)
110 impl<R
: BufRead
+ Write
> Write
for ZlibEncoder
<R
> {
111 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
112 self.get_mut().write(buf
)
115 fn flush(&mut self) -> io
::Result
<()> {
116 self.get_mut().flush()
120 /// A ZLIB decoder, or decompressor.
122 /// This structure consumes a [`BufRead`] interface, reading compressed data
123 /// from the underlying reader, and emitting uncompressed data.
125 /// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
130 /// use std::io::prelude::*;
132 /// # use flate2::Compression;
133 /// # use flate2::write::ZlibEncoder;
134 /// use flate2::bufread::ZlibDecoder;
137 /// # let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
138 /// # e.write_all(b"Hello World").unwrap();
139 /// # let bytes = e.finish().unwrap();
140 /// # println!("{}", decode_bufreader(bytes).unwrap());
143 /// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error
144 /// // Here &[u8] implements BufRead
146 /// fn decode_bufreader(bytes: Vec<u8>) -> io::Result<String> {
147 /// let mut z = ZlibDecoder::new(&bytes[..]);
148 /// let mut s = String::new();
149 /// z.read_to_string(&mut s)?;
154 pub struct ZlibDecoder
<R
> {
159 impl<R
: BufRead
> ZlibDecoder
<R
> {
160 /// Creates a new decoder which will decompress data read from the given
162 pub fn new(r
: R
) -> ZlibDecoder
<R
> {
165 data
: Decompress
::new(true),
170 pub fn reset_decoder_data
<R
>(zlib
: &mut ZlibDecoder
<R
>) {
171 zlib
.data
= Decompress
::new(true);
174 impl<R
> ZlibDecoder
<R
> {
175 /// Resets the state of this decoder entirely, swapping out the input
176 /// stream for another.
178 /// This will reset the internal state of this decoder and replace the
179 /// input stream with the one provided, returning the previous input
180 /// stream. Future data read from this decoder will be the decompressed
181 /// version of `r`'s data.
182 pub fn reset(&mut self, r
: R
) -> R
{
183 reset_decoder_data(self);
184 mem
::replace(&mut self.obj
, r
)
187 /// Acquires a reference to the underlying stream
188 pub fn get_ref(&self) -> &R
{
192 /// Acquires a mutable reference to the underlying stream
194 /// Note that mutation of the stream may result in surprising results if
195 /// this encoder is continued to be used.
196 pub fn get_mut(&mut self) -> &mut R
{
200 /// Consumes this decoder, returning the underlying reader.
201 pub fn into_inner(self) -> R
{
205 /// Returns the number of bytes that the decompressor has consumed.
207 /// Note that this will likely be smaller than what the decompressor
208 /// actually read from the underlying stream due to buffering.
209 pub fn total_in(&self) -> u64 {
213 /// Returns the number of bytes that the decompressor has produced.
214 pub fn total_out(&self) -> u64 {
215 self.data
.total_out()
219 impl<R
: BufRead
> Read
for ZlibDecoder
<R
> {
220 fn read(&mut self, into
: &mut [u8]) -> io
::Result
<usize> {
221 zio
::read(&mut self.obj
, &mut self.data
, into
)
225 impl<R
: BufRead
+ Write
> Write
for ZlibDecoder
<R
> {
226 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
227 self.get_mut().write(buf
)
230 fn flush(&mut self) -> io
::Result
<()> {
231 self.get_mut().flush()