use Error;
use std::io::Read;
fn load_data_portions<R: Read>(stream: &mut R, limit: usize) -> Result<Vec<u8>, Error> {
const PORTION_SIZE: usize = 1024;
let mut buf = vec![0u8; limit];
let mut effective_size = 0usize;
while effective_size + PORTION_SIZE < limit {
let read_bytes = stream.read(&mut buf[effective_size..(effective_size + PORTION_SIZE)])?;
if read_bytes == 0 {
buf.truncate(effective_size);
return Ok(buf);
}
effective_size += read_bytes;
}
Err(Error::TooBig)
}
pub fn load_data<R: Read>(stream: &mut R,
limit: usize,
data_length: Option<u64>)
-> Result<Vec<u8>, Error> {
match data_length {
None => load_data_portions(stream, limit),
Some(len) if (len as usize) > limit => Err(Error::TooBig),
Some(len) => {
let mut data = vec![0u8; len as usize];
stream.read_exact(&mut data)?;
Ok(data)
}
}
}