Struct delharc::decode::LhaDecodeReader
source · pub struct LhaDecodeReader<R> { /* private fields */ }
Expand description
LhaDecodeReader
provides a convenient way to parse and decode LHA/LZH files.
To read the current archived file’s content use the std::io::Read
trait methods on the instance
of this type. After reading the whole file (until EOF), the calculated checksum should be verified
using LhaDecodeReader::crc_check
.
To parse and decode the next archive file, invoke LhaDecodeReader::next_file
.
After parsing the LHA header, a decompressed content of a file can be simply read from the
LhaDecodeReader<R>
, which decompresses it using a proper decoder, designated in the header,
while reading data from the underlying stream.
If the compression method is not supported by the decoder, but otherwise the header has been parsed
successfully, invoke LhaDecodeReader::is_decoder_supported
to ensure you can actually read the file.
Otherwise, trying to read from an unsupported decoder will result in an error.
§no_std
Without the std
feature in the absence of std::io
the crate’s Read
trait methods should
be used instead to read the content of the decompressed files.
Implementations§
source§impl<R: Read> LhaDecodeReader<R>
impl<R: Read> LhaDecodeReader<R>
sourcepub fn new(rd: R) -> Result<LhaDecodeReader<R>, LhaDecodeError<R>>
pub fn new(rd: R) -> Result<LhaDecodeReader<R>, LhaDecodeError<R>>
Return a new instance of LhaDecodeReader<R>
after reading and parsing the first header from source.
Provide a stream reader as rd
.
§Errors
Return an error if the header could not be read or parsed.
sourcepub fn begin_new(&mut self, rd: R) -> Result<bool, LhaDecodeError<R>>
pub fn begin_new(&mut self, rd: R) -> Result<bool, LhaDecodeError<R>>
Attempt to read the first file header from a new source stream and initialize a decoder returning
Ok(true)
on success. Return Ok(false)
if there are no more headers in the stream.
Provide a stream reader as rd
.
When Ok
is returned, regardles of the retuned boolean value, the inner reader is being always
replaced with the given rd
.
When Ok(false)
has been returned, trying to read from the decoder will result in an error.
§Errors
Returns an error if the header could not be read or parsed. In this instance the inner stream reader is not being replaced by a new one and the provided source stream can be retrieved from the returned error.
sourcepub fn begin_with_header_and_decoder(
&mut self,
header: LhaHeader,
decoder: DecoderAny<Take<R>>
)
pub fn begin_with_header_and_decoder( &mut self, header: LhaHeader, decoder: DecoderAny<Take<R>> )
Assign externally parsed header and decoder to this instance of LhaDecodeReader<R>
.
It is up to the caller to make sure the decoder and the header are matching each other.
The decoder should be initialized with the reader limited by the Take
wrapper
with its limit set to the LhaHeader::compressed_size
number of bytes.
This method assumes the file will be read and decoded from its beginning.
sourcepub fn next_file(&mut self) -> Result<bool, LhaDecodeError<R>>
pub fn next_file(&mut self) -> Result<bool, LhaDecodeError<R>>
Attempt to parse the next file’s header.
The remaining content of the previous file is being skipped if the current file’s content has not been read entirely.
On success returns Ok(true)
if the next header has been read and parsed successfully.
If there are no more headers, returns Ok(false)
.
§Errors
Returns an error if the header could not be read or parsed. In this instance the underlying stream source will be taken and returned with the error.
§Panic
Panics if called when the underlying stream reader has been already taken.
§no_std
To skip the remaining file’s content this function uses 8 KB stack-allocated buffer
when using with std
feature enabled. Without std
the buffer size is 512 bytes.
See also LhaDecodeReader::next_file_with_sink
.
sourcepub fn next_file_with_sink<const BUF: usize>(
&mut self
) -> Result<bool, LhaDecodeError<R>>
pub fn next_file_with_sink<const BUF: usize>( &mut self ) -> Result<bool, LhaDecodeError<R>>
Attempt to parse the next file’s header.
Exactly like LhaDecodeReader::next_file
but allows to specify the sink buffer
size as BUF
.
§Panics
Panics when BUF
= 0
.
sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Unwrap the underlying stream reader and return it.
§Panics
Panics if the reader has been already taken.
sourcepub fn take_inner(&mut self) -> Option<R>
pub fn take_inner(&mut self) -> Option<R>
Take the inner stream reader value out of the decoder, leaving a none in its place.
After this call, reading from this instance will result in a panic.
sourcepub fn len(&self) -> u64
pub fn len(&self) -> u64
Return the number of remaining bytes of the currently decompressed file to be read.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return whether the current file has been finished reading or if the file was empty.
sourcepub fn is_present(&self) -> bool
pub fn is_present(&self) -> bool
Return whether an underlying stream reader is present in the decoder.
sourcepub fn is_absent(&self) -> bool
pub fn is_absent(&self) -> bool
Return whether an underlying stream reader is absent from the decoder.
An attempt to read file’s content in this state will result in a panic.
sourcepub fn crc_is_ok(&self) -> bool
pub fn crc_is_ok(&self) -> bool
Return whether the computed CRC-16 matches the checksum in the header.
This should be called after the whole file has been read.
sourcepub fn crc_check(&self) -> LhaResult<u16, R>
pub fn crc_check(&self) -> LhaResult<u16, R>
Return CRC-16 checksum if the computed checksum matches the one in the header.
Otherwise return an LhaError::Checksum
error.
This should be called after the whole file has been read.
sourcepub fn is_decoder_supported(&self) -> bool
pub fn is_decoder_supported(&self) -> bool
Return whether the current file’s compression method is supported.
If this method returns false
, trying to read from the decoder will result in an error.
In this instance it is still ok to skip to the next file.
§Note
If the variant of compression is CompressionMethod::Lhd
this method will return false
.
In this instance check the result from header’s LhaHeader::is_directory
to determine
what steps should be taken next.
Trait Implementations§
source§impl<R: Debug> Debug for LhaDecodeReader<R>
impl<R: Debug> Debug for LhaDecodeReader<R>
source§impl<R: Read> Default for LhaDecodeReader<R>
impl<R: Read> Default for LhaDecodeReader<R>
A default implementation creates an instance of LhaDecodeReader<R>
with no reader present and
with a phony header.
source§impl<R: Read<Error = Error>> Read for LhaDecodeReader<R>
impl<R: Read<Error = Error>> Read for LhaDecodeReader<R>
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
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<R> Freeze for LhaDecodeReader<R>where
R: Freeze,
impl<R> RefUnwindSafe for LhaDecodeReader<R>where
R: RefUnwindSafe,
impl<R> Send for LhaDecodeReader<R>where
R: Send,
impl<R> Sync for LhaDecodeReader<R>where
R: Sync,
impl<R> Unpin for LhaDecodeReader<R>where
R: Unpin,
impl<R> UnwindSafe for LhaDecodeReader<R>where
R: UnwindSafe,
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> Read for Rwhere
R: Read,
impl<R> Read for Rwhere
R: Read,
source§fn unexpected_eof() -> <R as Read>::Error
fn unexpected_eof() -> <R as Read>::Error
source§fn read_all(&mut self, buf: &mut [u8]) -> Result<usize, <R as Read>::Error>
fn read_all(&mut self, buf: &mut [u8]) -> Result<usize, <R as Read>::Error>
io::Read::read
but continue on io::ErrorKind::Interrupted
.source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), <R as Read>::Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), <R as Read>::Error>
io::Read::read_exact
.