zvault/src/chunking/fixed.rs

41 lines
1.2 KiB
Rust
Raw Normal View History

2017-04-19 17:54:53 +00:00
use super::*;
use std::cmp::min;
pub struct FixedChunker {
2018-02-24 22:28:18 +00:00
buffer: [u8; 0x1000],
2017-04-19 17:54:53 +00:00
size: usize
}
impl FixedChunker {
pub fn new(avg_size: usize) -> FixedChunker {
FixedChunker{
2018-02-24 22:28:18 +00:00
buffer: [0; 0x1000],
2017-04-19 17:54:53 +00:00
size: avg_size,
}
}
}
impl Chunker for FixedChunker {
#[allow(unknown_lints,explicit_counter_loop)]
2018-02-19 21:30:59 +00:00
fn chunk(&mut self, r: &mut Read, w: &mut Write) -> Result<ChunkerStatus, ChunkerError> {
2017-04-19 17:54:53 +00:00
let mut todo = self.size;
loop {
// Fill the buffer, there might be some bytes still in there from last chunk
let max_read = min(todo, self.buffer.len());
let read = try!(r.read(&mut self.buffer[..max_read]).map_err(ChunkerError::Read));
// If nothing to do, finish
if read == 0 {
return Ok(ChunkerStatus::Finished)
}
// Write all bytes from this chunk out to sink and store rest for next chunk
try!(w.write_all(&self.buffer[..read]).map_err(ChunkerError::Write));
todo -= read;
if todo == 0 {
return Ok(ChunkerStatus::Continue)
}
}
}
}