Skip to main content

FileReader

Struct FileReader 

Source
pub struct FileReader<'a, DATA: Read + Seek> { /* private fields */ }
Expand description

A reader for file content in a FAT filesystem.

This struct provides a Read implementation that follows the cluster chain to read file contents.

§Buffering

When the alloc feature is enabled, the reader can optionally buffer data to reduce the number of seek and read operations:

  • with_buffer: Enable cluster-level buffering. Each cluster is read entirely into memory and subsequent reads are served from the buffer.

  • with_cached_chain: Pre-cache the entire cluster chain. This is useful for small files where you want to avoid repeated FAT lookups.

Implementations§

Source§

impl<'a, DATA: Read + Seek> FileReader<'a, DATA>

Source

pub fn new(fs: &'a FatVolume<DATA>, entry: &FileEntry) -> Result<Self>

Create a new FileReader for a file entry.

Returns an error if the entry is a directory.

Source

pub fn size(&self) -> usize

Returns the total size of the file in bytes.

Source

pub fn remaining(&self) -> usize

Returns the number of bytes remaining to be read.

Source

pub fn with_buffer(self) -> Self

Enable cluster-level buffering.

When enabled, each cluster is read entirely into memory on first access, and subsequent reads within that cluster are served from the buffer. This reduces the number of seek operations at the cost of memory usage.

Memory usage: One cluster size (typically 4KB to 64KB).

Source

pub fn with_cached_chain(self) -> Result<Self>

Pre-cache the entire cluster chain.

This reads the entire FAT chain for the file into memory, eliminating the need for FAT lookups during sequential reads. This is most beneficial for fragmented files or when performing many random seeks.

Memory usage: 4 bytes per cluster in the file.

Source

pub fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Read data from the file.

Reads up to buf.len() bytes, or fewer at end-of-file. Use a small buf to stream incrementally; a larger buf allows more bytes per call (including contiguous-cluster bulk I/O when the chain is cached). The underlying Read / Seek implementation can enforce alignment or transfer sizes as needed. Optional per-cluster buffering applies when enabled.

Source

pub fn read_to_vec(&mut self) -> Result<Vec<u8>>

Read all bytes from the current read position through the end of the file.

Data is read starting at this reader’s current offset in the file (the same position the next read would use—not necessarily offset 0). Bytes already consumed by earlier read or read_to_vec calls are not read again. The allocation size is remaining; bytes are read using the same internal bulk-read path as read.

Auto Trait Implementations§

§

impl<'a, DATA> !RefUnwindSafe for FileReader<'a, DATA>

§

impl<'a, DATA> !Send for FileReader<'a, DATA>

§

impl<'a, DATA> !Sync for FileReader<'a, DATA>

§

impl<'a, DATA> !UnwindSafe for FileReader<'a, DATA>

§

impl<'a, DATA> Freeze for FileReader<'a, DATA>

§

impl<'a, DATA> Unpin for FileReader<'a, DATA>

§

impl<'a, DATA> UnsafeUnpin for FileReader<'a, DATA>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybePod for T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.