]>
git.proxmox.com Git - rustc.git/blob - vendor/flate2/src/deflate/write.rs
2 use std
::io
::prelude
::*;
4 #[cfg(feature = "tokio")]
6 #[cfg(feature = "tokio")]
7 use tokio_io
::{AsyncRead, AsyncWrite}
;
10 use crate::{Compress, Decompress}
;
12 /// A DEFLATE encoder, or compressor.
14 /// This structure implements a [`Write`] interface and takes a stream of
15 /// uncompressed data, writing the compressed data to the wrapped writer.
17 /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
22 /// use std::io::prelude::*;
23 /// use flate2::Compression;
24 /// use flate2::write::DeflateEncoder;
26 /// // Vec<u8> implements Write to print the compressed bytes of sample string
29 /// let mut e = DeflateEncoder::new(Vec::new(), Compression::default());
30 /// e.write_all(b"Hello World").unwrap();
31 /// println!("{:?}", e.finish().unwrap());
35 pub struct DeflateEncoder
<W
: Write
> {
36 inner
: zio
::Writer
<W
, Compress
>,
39 impl<W
: Write
> DeflateEncoder
<W
> {
40 /// Creates a new encoder which will write compressed data to the stream
41 /// given at the given compression level.
43 /// When this encoder is dropped or unwrapped the final pieces of data will
45 pub fn new(w
: W
, level
: crate::Compression
) -> DeflateEncoder
<W
> {
47 inner
: zio
::Writer
::new(w
, Compress
::new(level
, false)),
51 /// Acquires a reference to the underlying writer.
52 pub fn get_ref(&self) -> &W
{
56 /// Acquires a mutable reference to the underlying writer.
58 /// Note that mutating the output/input state of the stream may corrupt this
59 /// object, so care must be taken when using this method.
60 pub fn get_mut(&mut self) -> &mut W
{
64 /// Resets the state of this encoder entirely, swapping out the output
65 /// stream for another.
67 /// This function will finish encoding the current stream into the current
68 /// output stream before swapping out the two output streams. If the stream
69 /// cannot be finished an error is returned.
71 /// After the current stream has been finished, this will reset the internal
72 /// state of this encoder and replace the output stream with the one
73 /// provided, returning the previous output stream. Future data written to
74 /// this encoder will be the compressed into the stream `w` provided.
78 /// This function will perform I/O to complete this stream, and any I/O
79 /// errors which occur will be returned from this function.
80 pub fn reset(&mut self, w
: W
) -> io
::Result
<W
> {
82 self.inner
.data
.reset();
83 Ok(self.inner
.replace(w
))
86 /// Attempt to finish this output stream, writing out final chunks of data.
88 /// Note that this function can only be used once data has finished being
89 /// written to the output stream. After this function is called then further
90 /// calls to `write` may result in a panic.
94 /// Attempts to write data to this stream may result in a panic after this
95 /// function is called.
99 /// This function will perform I/O to complete this stream, and any I/O
100 /// errors which occur will be returned from this function.
101 pub fn try_finish(&mut self) -> io
::Result
<()> {
105 /// Consumes this encoder, flushing the output stream.
107 /// This will flush the underlying data stream, close off the compressed
108 /// stream and, if successful, return the contained writer.
110 /// Note that this function may not be suitable to call in a situation where
111 /// the underlying stream is an asynchronous I/O stream. To finish a stream
112 /// the `try_finish` (or `shutdown`) method should be used instead. To
113 /// re-acquire ownership of a stream it is safe to call this method after
114 /// `try_finish` or `shutdown` has returned `Ok`.
118 /// This function will perform I/O to complete this stream, and any I/O
119 /// errors which occur will be returned from this function.
120 pub fn finish(mut self) -> io
::Result
<W
> {
121 self.inner
.finish()?
;
122 Ok(self.inner
.take_inner())
125 /// Consumes this encoder, flushing the output stream.
127 /// This will flush the underlying data stream and then return the contained
128 /// writer if the flush succeeded.
129 /// The compressed stream will not closed but only flushed. This
130 /// means that obtained byte array can by extended by another deflated
131 /// stream. To close the stream add the two bytes 0x3 and 0x0.
135 /// This function will perform I/O to complete this stream, and any I/O
136 /// errors which occur will be returned from this function.
137 pub fn flush_finish(mut self) -> io
::Result
<W
> {
139 Ok(self.inner
.take_inner())
142 /// Returns the number of bytes that have been written to this compresor.
144 /// Note that not all bytes written to this object may be accounted for,
145 /// there may still be some active buffering.
146 pub fn total_in(&self) -> u64 {
147 self.inner
.data
.total_in()
150 /// Returns the number of bytes that the compressor has produced.
152 /// Note that not all bytes may have been written yet, some may still be
154 pub fn total_out(&self) -> u64 {
155 self.inner
.data
.total_out()
159 impl<W
: Write
> Write
for DeflateEncoder
<W
> {
160 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
161 self.inner
.write(buf
)
164 fn flush(&mut self) -> io
::Result
<()> {
169 #[cfg(feature = "tokio")]
170 impl<W
: AsyncWrite
> AsyncWrite
for DeflateEncoder
<W
> {
171 fn shutdown(&mut self) -> Poll
<(), io
::Error
> {
172 self.inner
.finish()?
;
173 self.inner
.get_mut().shutdown()
177 impl<W
: Read
+ Write
> Read
for DeflateEncoder
<W
> {
178 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
179 self.inner
.get_mut().read(buf
)
183 #[cfg(feature = "tokio")]
184 impl<W
: AsyncRead
+ AsyncWrite
> AsyncRead
for DeflateEncoder
<W
> {}
186 /// A DEFLATE decoder, or decompressor.
188 /// This structure implements a [`Write`] and will emit a stream of decompressed
189 /// data when fed a stream of compressed data.
191 /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Read.html
196 /// use std::io::prelude::*;
198 /// # use flate2::Compression;
199 /// # use flate2::write::DeflateEncoder;
200 /// use flate2::write::DeflateDecoder;
203 /// # let mut e = DeflateEncoder::new(Vec::new(), Compression::default());
204 /// # e.write_all(b"Hello World").unwrap();
205 /// # let bytes = e.finish().unwrap();
206 /// # println!("{}", decode_writer(bytes).unwrap());
208 /// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error
209 /// // Here Vec<u8> implements Write
210 /// fn decode_writer(bytes: Vec<u8>) -> io::Result<String> {
211 /// let mut writer = Vec::new();
212 /// let mut deflater = DeflateDecoder::new(writer);
213 /// deflater.write_all(&bytes[..])?;
214 /// writer = deflater.finish()?;
215 /// let return_string = String::from_utf8(writer).expect("String parsing error");
216 /// Ok(return_string)
220 pub struct DeflateDecoder
<W
: Write
> {
221 inner
: zio
::Writer
<W
, Decompress
>,
224 impl<W
: Write
> DeflateDecoder
<W
> {
225 /// Creates a new decoder which will write uncompressed data to the stream.
227 /// When this encoder is dropped or unwrapped the final pieces of data will
229 pub fn new(w
: W
) -> DeflateDecoder
<W
> {
231 inner
: zio
::Writer
::new(w
, Decompress
::new(false)),
235 /// Acquires a reference to the underlying writer.
236 pub fn get_ref(&self) -> &W
{
240 /// Acquires a mutable reference to the underlying writer.
242 /// Note that mutating the output/input state of the stream may corrupt this
243 /// object, so care must be taken when using this method.
244 pub fn get_mut(&mut self) -> &mut W
{
248 /// Resets the state of this decoder entirely, swapping out the output
249 /// stream for another.
251 /// This function will finish encoding the current stream into the current
252 /// output stream before swapping out the two output streams.
254 /// This will then reset the internal state of this decoder and replace the
255 /// output stream with the one provided, returning the previous output
256 /// stream. Future data written to this decoder will be decompressed into
257 /// the output stream `w`.
261 /// This function will perform I/O to finish the stream, and if that I/O
262 /// returns an error then that will be returned from this function.
263 pub fn reset(&mut self, w
: W
) -> io
::Result
<W
> {
264 self.inner
.finish()?
;
265 self.inner
.data
= Decompress
::new(false);
266 Ok(self.inner
.replace(w
))
269 /// Attempt to finish this output stream, writing out final chunks of data.
271 /// Note that this function can only be used once data has finished being
272 /// written to the output stream. After this function is called then further
273 /// calls to `write` may result in a panic.
277 /// Attempts to write data to this stream may result in a panic after this
278 /// function is called.
282 /// This function will perform I/O to finish the stream, returning any
283 /// errors which happen.
284 pub fn try_finish(&mut self) -> io
::Result
<()> {
288 /// Consumes this encoder, flushing the output stream.
290 /// This will flush the underlying data stream and then return the contained
291 /// writer if the flush succeeded.
293 /// Note that this function may not be suitable to call in a situation where
294 /// the underlying stream is an asynchronous I/O stream. To finish a stream
295 /// the `try_finish` (or `shutdown`) method should be used instead. To
296 /// re-acquire ownership of a stream it is safe to call this method after
297 /// `try_finish` or `shutdown` has returned `Ok`.
301 /// This function will perform I/O to complete this stream, and any I/O
302 /// errors which occur will be returned from this function.
303 pub fn finish(mut self) -> io
::Result
<W
> {
304 self.inner
.finish()?
;
305 Ok(self.inner
.take_inner())
308 /// Returns the number of bytes that the decompressor has consumed for
311 /// Note that this will likely be smaller than the number of bytes
312 /// successfully written to this stream due to internal buffering.
313 pub fn total_in(&self) -> u64 {
314 self.inner
.data
.total_in()
317 /// Returns the number of bytes that the decompressor has written to its
319 pub fn total_out(&self) -> u64 {
320 self.inner
.data
.total_out()
324 impl<W
: Write
> Write
for DeflateDecoder
<W
> {
325 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
326 self.inner
.write(buf
)
329 fn flush(&mut self) -> io
::Result
<()> {
334 #[cfg(feature = "tokio")]
335 impl<W
: AsyncWrite
> AsyncWrite
for DeflateDecoder
<W
> {
336 fn shutdown(&mut self) -> Poll
<(), io
::Error
> {
337 self.inner
.finish()?
;
338 self.inner
.get_mut().shutdown()
342 impl<W
: Read
+ Write
> Read
for DeflateDecoder
<W
> {
343 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
344 self.inner
.get_mut().read(buf
)
348 #[cfg(feature = "tokio")]
349 impl<W
: AsyncRead
+ AsyncWrite
> AsyncRead
for DeflateDecoder
<W
> {}