]>
git.proxmox.com Git - rustc.git/blob - src/vendor/flate2/src/zlib/read.rs
1 use std
::io
::prelude
::*;
4 #[cfg(feature = "tokio")]
6 #[cfg(feature = "tokio")]
7 use tokio_io
::{AsyncRead, AsyncWrite}
;
9 use bufreader
::BufReader
;
12 /// A ZLIB encoder, or compressor.
14 /// This structure implements a [`Read`] interface and will read uncompressed
15 /// data from an underlying stream and emit a stream of compressed data.
17 /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
22 /// use std::io::prelude::*;
23 /// use flate2::Compression;
24 /// use flate2::read::ZlibEncoder;
25 /// use std::fs::File;
27 /// // Open example file and compress the contents using Read interface
29 /// # fn open_hello_world() -> std::io::Result<Vec<u8>> {
30 /// let f = File::open("examples/hello_world.txt")?;
31 /// let mut z = ZlibEncoder::new(f, Compression::fast());
32 /// let mut buffer = [0;50];
33 /// let byte_count = z.read(&mut buffer)?;
34 /// # Ok(buffer[0..byte_count].to_vec())
38 pub struct ZlibEncoder
<R
> {
39 inner
: bufread
::ZlibEncoder
<BufReader
<R
>>,
42 impl<R
: Read
> ZlibEncoder
<R
> {
43 /// Creates a new encoder which will read uncompressed data from the given
44 /// stream and emit the compressed stream.
45 pub fn new(r
: R
, level
: ::Compression
) -> ZlibEncoder
<R
> {
47 inner
: bufread
::ZlibEncoder
::new(BufReader
::new(r
), level
),
52 impl<R
> ZlibEncoder
<R
> {
53 /// Resets the state of this encoder entirely, swapping out the input
54 /// stream for another.
56 /// This function will reset the internal state of this encoder and replace
57 /// the input stream with the one provided, returning the previous input
58 /// stream. Future data read from this encoder will be the compressed
59 /// version of `r`'s data.
61 /// Note that there may be currently buffered data when this function is
62 /// called, and in that case the buffered data is discarded.
63 pub fn reset(&mut self, r
: R
) -> R
{
64 super::bufread
::reset_encoder_data(&mut self.inner
);
65 self.inner
.get_mut().reset(r
)
68 /// Acquires a reference to the underlying stream
69 pub fn get_ref(&self) -> &R
{
70 self.inner
.get_ref().get_ref()
73 /// Acquires a mutable reference to the underlying stream
75 /// Note that mutation of the stream may result in surprising results if
76 /// this encoder is continued to be used.
77 pub fn get_mut(&mut self) -> &mut R
{
78 self.inner
.get_mut().get_mut()
81 /// Consumes this encoder, returning the underlying reader.
83 /// Note that there may be buffered bytes which are not re-acquired as part
84 /// of this transition. It's recommended to only call this function after
85 /// EOF has been reached.
86 pub fn into_inner(self) -> R
{
87 self.inner
.into_inner().into_inner()
90 /// Returns the number of bytes that have been read into this compressor.
92 /// Note that not all bytes read from the underlying object may be accounted
93 /// for, there may still be some active buffering.
94 pub fn total_in(&self) -> u64 {
98 /// Returns the number of bytes that the compressor has produced.
100 /// Note that not all bytes may have been read yet, some may still be
102 pub fn total_out(&self) -> u64 {
103 self.inner
.total_out()
107 impl<R
: Read
> Read
for ZlibEncoder
<R
> {
108 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
113 #[cfg(feature = "tokio")]
114 impl<R
: AsyncRead
> AsyncRead
for ZlibEncoder
<R
> {}
116 impl<W
: Read
+ Write
> Write
for ZlibEncoder
<W
> {
117 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
118 self.get_mut().write(buf
)
121 fn flush(&mut self) -> io
::Result
<()> {
122 self.get_mut().flush()
126 #[cfg(feature = "tokio")]
127 impl<R
: AsyncRead
+ AsyncWrite
> AsyncWrite
for ZlibEncoder
<R
> {
128 fn shutdown(&mut self) -> Poll
<(), io
::Error
> {
129 self.get_mut().shutdown()
133 /// A ZLIB decoder, or decompressor.
135 /// This structure implements a [`Read`] interface and takes a stream of
136 /// compressed data as input, providing the decompressed data when read from.
138 /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
143 /// use std::io::prelude::*;
145 /// # use flate2::Compression;
146 /// # use flate2::write::ZlibEncoder;
147 /// use flate2::read::ZlibDecoder;
150 /// # let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
151 /// # e.write(b"Hello World").unwrap();
152 /// # let bytes = e.finish().unwrap();
153 /// # println!("{}", decode_reader(bytes).unwrap());
156 /// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error
157 /// // Here &[u8] implements Read
159 /// fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
160 /// let mut z = ZlibDecoder::new(&bytes[..]);
161 /// let mut s = String::new();
162 /// z.read_to_string(&mut s)?;
167 pub struct ZlibDecoder
<R
> {
168 inner
: bufread
::ZlibDecoder
<BufReader
<R
>>,
172 impl<R
: Read
> ZlibDecoder
<R
> {
173 /// Creates a new decoder which will decompress data read from the given
175 pub fn new(r
: R
) -> ZlibDecoder
<R
> {
176 ZlibDecoder
::new_with_buf(r
, vec
![0; 32 * 1024])
179 /// Same as `new`, but the intermediate buffer for data is specified.
181 /// Note that the specified buffer will only be used up to its current
182 /// length. The buffer's capacity will also not grow over time.
183 pub fn new_with_buf(r
: R
, buf
: Vec
<u8>) -> ZlibDecoder
<R
> {
185 inner
: bufread
::ZlibDecoder
::new(BufReader
::with_buf(buf
, r
)),
190 impl<R
> ZlibDecoder
<R
> {
191 /// Resets the state of this decoder entirely, swapping out the input
192 /// stream for another.
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.
199 /// Note that there may be currently buffered data when this function is
200 /// called, and in that case the buffered data is discarded.
201 pub fn reset(&mut self, r
: R
) -> R
{
202 super::bufread
::reset_decoder_data(&mut self.inner
);
203 self.inner
.get_mut().reset(r
)
206 /// Acquires a reference to the underlying stream
207 pub fn get_ref(&self) -> &R
{
208 self.inner
.get_ref().get_ref()
211 /// Acquires a mutable reference to the underlying stream
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 self.inner
.get_mut().get_mut()
219 /// Consumes this decoder, returning the underlying reader.
221 /// Note that there may be buffered bytes which are not re-acquired as part
222 /// of this transition. It's recommended to only call this function after
223 /// EOF has been reached.
224 pub fn into_inner(self) -> R
{
225 self.inner
.into_inner().into_inner()
228 /// Returns the number of bytes that the decompressor has consumed.
230 /// Note that this will likely be smaller than what the decompressor
231 /// actually read from the underlying stream due to buffering.
232 pub fn total_in(&self) -> u64 {
233 self.inner
.total_in()
236 /// Returns the number of bytes that the decompressor has produced.
237 pub fn total_out(&self) -> u64 {
238 self.inner
.total_out()
242 impl<R
: Read
> Read
for ZlibDecoder
<R
> {
243 fn read(&mut self, into
: &mut [u8]) -> io
::Result
<usize> {
244 self.inner
.read(into
)
248 #[cfg(feature = "tokio")]
249 impl<R
: AsyncRead
> AsyncRead
for ZlibDecoder
<R
> {}
251 impl<R
: Read
+ Write
> Write
for ZlibDecoder
<R
> {
252 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
253 self.get_mut().write(buf
)
256 fn flush(&mut self) -> io
::Result
<()> {
257 self.get_mut().flush()
261 #[cfg(feature = "tokio")]
262 impl<R
: AsyncWrite
+ AsyncRead
> AsyncWrite
for ZlibDecoder
<R
> {
263 fn shutdown(&mut self) -> Poll
<(), io
::Error
> {
264 self.get_mut().shutdown()