Struct ruzstd::frame_decoder::FrameDecoder
source · pub struct FrameDecoder { /* private fields */ }
Expand description
This implements a decoder for zstd frames. This decoder is able to decode frames only partially and gives control over how many bytes/blocks will be decoded at a time (so you don’t have to decode a 10GB file into memory all at once). It reads bytes as needed from a provided source and can be read from to collect partial results.
If you want to just read the whole frame with an io::Read without having to deal with manually calling decode_blocks you can use the provided StreamingDecoder with wraps this FrameDecoder
Workflow is as follows:
use ruzstd::frame_decoder::BlockDecodingStrategy;
use std::io::{Read, Write};
// no_std environments can use the crate's own Read traits
use ruzstd::io::{Read, Write};
fn decode_this(mut file: impl Read) {
//Create a new decoder
let mut frame_dec = ruzstd::FrameDecoder::new();
let mut result = Vec::new();
// Use reset or init to make the decoder ready to decode the frame from the io::Read
frame_dec.reset(&mut file).unwrap();
// Loop until the frame has been decoded completely
while !frame_dec.is_finished() {
// decode (roughly) batch_size many bytes
frame_dec.decode_blocks(&mut file, BlockDecodingStrategy::UptoBytes(1024)).unwrap();
// read from the decoder to collect bytes from the internal buffer
let bytes_read = frame_dec.read(result.as_mut_slice()).unwrap();
// then do something with it
do_something(&result[0..bytes_read]);
}
// handle the last chunk of data
while frame_dec.can_collect() > 0 {
let x = frame_dec.read(result.as_mut_slice()).unwrap();
do_something(&result[0..x]);
}
}
fn do_something(data: &[u8]) {
std::io::stdout().write_all(data).unwrap();
}
Implementations§
source§impl FrameDecoder
impl FrameDecoder
sourcepub fn new() -> FrameDecoder ⓘ
pub fn new() -> FrameDecoder ⓘ
This will create a new decoder without allocating anything yet. init()/reset() will allocate all needed buffers if it is the first time this decoder is used else they just reset these buffers with not further allocations
sourcepub fn init(&mut self, source: impl Read) -> Result<(), FrameDecoderError>
pub fn init(&mut self, source: impl Read) -> Result<(), FrameDecoderError>
init() will allocate all needed buffers if it is the first time this decoder is used else they just reset these buffers with not further allocations
Note that all bytes currently in the decodebuffer from any previous frame will be lost. Collect them with collect()/collect_to_writer()
equivalent to reset()
sourcepub fn reset(&mut self, source: impl Read) -> Result<(), FrameDecoderError>
pub fn reset(&mut self, source: impl Read) -> Result<(), FrameDecoderError>
reset() will allocate all needed buffers if it is the first time this decoder is used else they just reset these buffers with not further allocations
Note that all bytes currently in the decodebuffer from any previous frame will be lost. Collect them with collect()/collect_to_writer()
equivalent to init()
sourcepub fn add_dict(&mut self, dict: Dictionary) -> Result<(), FrameDecoderError>
pub fn add_dict(&mut self, dict: Dictionary) -> Result<(), FrameDecoderError>
Add a dict to the FrameDecoder that can be used when needed. The FrameDecoder uses the appropriate one dynamically
pub fn force_dict(&mut self, dict_id: u32) -> Result<(), FrameDecoderError>
sourcepub fn content_size(&self) -> u64
pub fn content_size(&self) -> u64
Returns how many bytes the frame contains after decompression
sourcepub fn get_checksum_from_data(&self) -> Option<u32>
pub fn get_checksum_from_data(&self) -> Option<u32>
Returns the checksum that was read from the data. Only available after all bytes have been read. It is the last 4 bytes of a zstd-frame
sourcepub fn get_calculated_checksum(&self) -> Option<u32>
pub fn get_calculated_checksum(&self) -> Option<u32>
Returns the checksum that was calculated while decoding. Only a sensible value after all decoded bytes have been collected/read from the FrameDecoder
sourcepub fn bytes_read_from_source(&self) -> u64
pub fn bytes_read_from_source(&self) -> u64
Counter for how many bytes have been consumed while decoding the frame
sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Whether the current frames last block has been decoded yet If this returns true you can call the drain* functions to get all content (the read() function will drain automatically if this returns true)
sourcepub fn blocks_decoded(&self) -> usize
pub fn blocks_decoded(&self) -> usize
Counter for how many blocks have already been decoded
sourcepub fn decode_blocks(
&mut self,
source: impl Read,
strat: BlockDecodingStrategy
) -> Result<bool, FrameDecoderError>
pub fn decode_blocks( &mut self, source: impl Read, strat: BlockDecodingStrategy ) -> Result<bool, FrameDecoderError>
Decodes blocks from a reader. It requires that the framedecoder has been initialized first. The Strategy influences how many blocks will be decoded before the function returns This is important if you want to manage memory consumption carefully. If you don’t care about that you can just choose the strategy “All” and have all blocks of the frame decoded into the buffer
sourcepub fn collect(&mut self) -> Option<Vec<u8>>
pub fn collect(&mut self) -> Option<Vec<u8>>
Collect bytes and retain window_size bytes while decoding is still going on. After decoding of the frame (is_finished() == true) has finished it will collect all remaining bytes
sourcepub fn collect_to_writer(&mut self, w: impl Write) -> Result<usize, Error>
pub fn collect_to_writer(&mut self, w: impl Write) -> Result<usize, Error>
Collect bytes and retain window_size bytes while decoding is still going on. After decoding of the frame (is_finished() == true) has finished it will collect all remaining bytes
sourcepub fn can_collect(&self) -> usize
pub fn can_collect(&self) -> usize
How many bytes can currently be collected from the decodebuffer, while decoding is going on this will be lower than the actual decodbuffer size because window_size bytes need to be retained for decoding. After decoding of the frame (is_finished() == true) has finished it will report all remaining bytes
sourcepub fn decode_from_to(
&mut self,
source: &[u8],
target: &mut [u8]
) -> Result<(usize, usize), FrameDecoderError>
pub fn decode_from_to( &mut self, source: &[u8], target: &mut [u8] ) -> Result<(usize, usize), FrameDecoderError>
Decodes as many blocks as possible from the source slice and reads from the decodebuffer into the target slice The source slice may contain only parts of a frame but must contain at least one full block to make progress
By all means use decode_blocks if you have a io.Reader available. This is just for compatibility with other decompressors which try to serve an old-style c api
Returns (read, written), if read == 0 then the source did not contain a full block and further calls with the same input will not make any progress!
Note that no kind of block can be bigger than 128kb. So to be safe use at least 128*1024 (max block content size) + 3 (block_header size) + 18 (max frame_header size) bytes as your source buffer
You may call this function with an empty source after all bytes have been decoded. This is equivalent to just call decoder.read(&mut target)
Trait Implementations§
source§impl Default for FrameDecoder
impl Default for FrameDecoder
source§impl Read for FrameDecoder
impl Read for FrameDecoder
Read bytes from the decode_buffer that are no longer needed. While the frame is not yet finished this will retain window_size bytes, else it will drain it completely
source§fn read(&mut self, target: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, target: &mut [u8]) -> Result<usize, Error>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreAuto Trait Implementations§
impl RefUnwindSafe for FrameDecoder
impl !Send for FrameDecoder
impl !Sync for FrameDecoder
impl Unpin for FrameDecoder
impl UnwindSafe for FrameDecoder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<R> ReadBytesExt for R
impl<R> ReadBytesExt for R
source§fn read_u8(&mut self) -> Result<u8, Error>
fn read_u8(&mut self) -> Result<u8, Error>
source§fn read_i8(&mut self) -> Result<i8, Error>
fn read_i8(&mut self) -> Result<i8, Error>
source§fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
source§fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
source§fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
source§fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
source§fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
source§fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
source§fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
source§fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
source§fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
source§fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
source§fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
source§fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
source§fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
source§fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
source§fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
source§fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
source§fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
source§fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
source§fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
source§fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
source§fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
source§fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
source§fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
source§fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
source§fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
source§fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
source§fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
source§fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
source§fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
read_f32_into
instead