pub struct FrameEncoder<W: Write> { /* private fields */ }
Expand description
A writer for compressing a LZ4 stream.
This FrameEncoder
wraps any other writer that implements io::Write
.
Bytes written to this writer are compressed using the LZ4 frame
format.
Writes are buffered automatically, so there’s no need to wrap the given
writer in a std::io::BufWriter
.
To ensure a well formed stream the encoder must be finalized by calling
either the finish()
, try_finish()
, or auto_finish()
methods.
§Example 1
Serializing json values into a compressed file.
let compressed_file = std::fs::File::create("datafile").unwrap();
let mut compressor = lz4_flex::frame::FrameEncoder::new(compressed_file);
serde_json::to_writer(&mut compressor, &serde_json::json!({ "an": "object" })).unwrap();
compressor.finish().unwrap();
§Example 2
Serializing multiple json values into a compressed file using linked blocks.
let compressed_file = std::fs::File::create("datafile").unwrap();
let mut frame_info = lz4_flex::frame::FrameInfo::new();
frame_info.block_mode = lz4_flex::frame::BlockMode::Linked;
let mut compressor = lz4_flex::frame::FrameEncoder::with_frame_info(frame_info, compressed_file);
for i in 0..10u64 {
serde_json::to_writer(&mut compressor, &serde_json::json!({ "i": i })).unwrap();
}
compressor.finish().unwrap();
Implementations§
Source§impl<W: Write> FrameEncoder<W>
impl<W: Write> FrameEncoder<W>
Sourcepub fn auto_finish(self) -> AutoFinishEncoder<W> ⓘ
pub fn auto_finish(self) -> AutoFinishEncoder<W> ⓘ
Returns a wrapper around self
that will finish the stream on drop.
§Note
Errors on drop get silently ignored. If you want to handle errors then use finish()
or
try_finish()
instead.
Sourcepub fn with_frame_info(frame_info: FrameInfo, wtr: W) -> Self
pub fn with_frame_info(frame_info: FrameInfo, wtr: W) -> Self
Creates a new Encoder with the specified FrameInfo.
Sourcepub fn frame_info(&mut self) -> &FrameInfo
pub fn frame_info(&mut self) -> &FrameInfo
The frame information used by this Encoder.
Sourcepub fn finish(self) -> Result<W, Error>
pub fn finish(self) -> Result<W, Error>
Consumes this encoder, flushing internal buffer and writing stream terminator.
Sourcepub fn try_finish(&mut self) -> Result<(), Error>
pub fn try_finish(&mut self) -> Result<(), Error>
Attempt to finish this output stream, flushing internal buffer and writing stream terminator.
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Returns the underlying writer without flushing the stream. This may leave the output in an unfinished state.
Trait Implementations§
Source§impl<W: Write> Write for FrameEncoder<W>
impl<W: Write> Write for FrameEncoder<W>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
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
)