pub struct CompressorWriter<W>where
W: Write,{ /* private fields */ }
Expand description
Wraps a writer and compresses its output.
CompressorWriter<W>
wraps a writer and adds brotli compression to the
output. It is critical to finish the compression stream, otherwise
decompression will not be successful. Dropping will attempt to finish the
compression stream, any errors that might arise however will be ignored.
Calling into_inner
ensures that the compression stream is finished.
Calling flush
will not only flush the underlying writer, but also flush
all of its compression stream. This will lead to a slight decrease of
compression quality, as output will be forced to be flushed as is and not
compressed till the block is finished.
§Examples
Let’s compress some text file named text.txt
and write the output to
test.brotli
:
use std::fs::File;
use std::io;
use brotlic::CompressorWriter;
let mut input = File::open("test.txt")?; // test.txt is uncompressed
let mut output = File::create("test.brotli")?;
let mut compressed_output = CompressorWriter::new(output);
io::copy(&mut input, &mut compressed_output)?;
To decompress it again, use DecompressorWriter
.
Implementations§
Source§impl<W> CompressorWriter<W>where
W: Write,
impl<W> CompressorWriter<W>where
W: Write,
Sourcepub fn new(inner: W) -> CompressorWriter<W> ⓘ
pub fn new(inner: W) -> CompressorWriter<W> ⓘ
Creates a new CompressorWriter<W>
with a newly created encoder.
§Panics
Panics if the encoder fails to be allocated or initialized
Sourcepub fn with_encoder(encoder: BrotliEncoder, inner: W) -> CompressorWriter<W> ⓘ
pub fn with_encoder(encoder: BrotliEncoder, inner: W) -> CompressorWriter<W> ⓘ
Creates a new CompressorWriter<W>
with a specified encoder.
§Examples
use brotlic::{BrotliEncoderOptions, CompressorWriter, Quality, WindowSize};
let encoder = BrotliEncoderOptions::new()
.quality(Quality::new(4)?)
.window_size(WindowSize::new(16)?)
.build()?;
let underlying_storage = Vec::new();
let writer = CompressorWriter::with_encoder(encoder, underlying_storage);
Sourcepub fn get_mut(&mut self) -> &mut W
pub fn get_mut(&mut self) -> &mut W
Gets a mutable reference to the underlying writer.
It is inadvisable to directly write to the underlying writer.
Sourcepub fn into_inner(self) -> Result<W, IntoInnerError<CompressorWriter<W>>>
pub fn into_inner(self) -> Result<W, IntoInnerError<CompressorWriter<W>>>
Sourcepub fn into_parts(self) -> (W, Result<BrotliEncoder, WriterPanicked>)
pub fn into_parts(self) -> (W, Result<BrotliEncoder, WriterPanicked>)
Disassembles this CompressorWriter<W>
, returning the underlying writer
and encoder.
If the underlying writer panicked, it is not known what portion of the
data was written. In this case, we return WriterPanicked
to get the
encoder back. It is worth noting that the compression stream is not
finished and hence cannot be successfully decompressed. To obtain the
writer once the compression stream is finished, use into_inner
.
into_parts
makes no attempt to finish the compression stream and
cannot fail.
Trait Implementations§
Source§impl<W> Debug for CompressorWriter<W>
impl<W> Debug for CompressorWriter<W>
Source§impl<W> Drop for CompressorWriter<W>where
W: Write,
impl<W> Drop for CompressorWriter<W>where
W: Write,
Source§impl<W> Write for CompressorWriter<W>where
W: Write,
impl<W> Write for CompressorWriter<W>where
W: Write,
Source§fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Source§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)