FileChunks: save offset instead of chunk index
This commit is contained in:
parent
b999c35965
commit
256f3ce8ba
1 changed files with 15 additions and 12 deletions
|
|
@ -8,7 +8,7 @@ use log::error;
|
|||
|
||||
pub struct FileChunks<'t> {
|
||||
file_path: &'t Path,
|
||||
chunk_index: u64,
|
||||
offset: u64,
|
||||
chunk_size: usize,
|
||||
}
|
||||
|
||||
|
|
@ -16,15 +16,15 @@ impl<'t> FileChunks<'t> {
|
|||
pub(super) fn new(path: &'t Path, chunk_size: usize) -> Self {
|
||||
Self {
|
||||
file_path: path,
|
||||
chunk_index: 0,
|
||||
offset: 0,
|
||||
chunk_size,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn seek(self, chunk_index: u64) -> Self {
|
||||
pub fn seek(self, offset: u64) -> Self {
|
||||
Self {
|
||||
file_path: self.file_path,
|
||||
chunk_index,
|
||||
offset,
|
||||
chunk_size: self.chunk_size,
|
||||
}
|
||||
}
|
||||
|
|
@ -34,15 +34,10 @@ impl Iterator for FileChunks<'_> {
|
|||
type Item = Chunk;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let offset = self.chunk_index
|
||||
* u64::try_from(self.chunk_size)
|
||||
.inspect_err(|e| error!("Error converting to u64: {e}"))
|
||||
.ok()?;
|
||||
|
||||
let mut f = File::open(self.file_path)
|
||||
.inspect_err(|e| error!("Error opening file: {e}"))
|
||||
.ok()?;
|
||||
f.seek(SeekFrom::Start(offset)).ok()?;
|
||||
f.seek(SeekFrom::Start(self.offset)).ok()?;
|
||||
|
||||
let mut bytes = vec![0; self.chunk_size];
|
||||
let read_len = (f.read(&mut bytes))
|
||||
|
|
@ -50,9 +45,17 @@ impl Iterator for FileChunks<'_> {
|
|||
.ok()?;
|
||||
bytes.truncate(read_len);
|
||||
|
||||
self.chunk_index += 1;
|
||||
let read_len: u64 = read_len
|
||||
.try_into()
|
||||
.inspect_err(|e| error!("Error converting to u64: {e}"))
|
||||
.ok()?;
|
||||
self.offset += read_len;
|
||||
|
||||
Some(Self::Item { offset, bytes }).filter(|c| !c.bytes.is_empty())
|
||||
Some(Self::Item {
|
||||
offset: self.offset,
|
||||
bytes,
|
||||
})
|
||||
.filter(|c| !c.bytes.is_empty())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue