bookdata/io/
ext.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::fs::File;
use std::io;
use std::io::Read;
use zip::read::ZipFile;

use log::*;

/// Trait for read types that can report their size.
pub trait LengthRead: Read {
    fn input_size(&self) -> io::Result<u64>;

    /// Read all bytes from a file, using its size to pre-allocate capacity.
    fn read_all_sized(&mut self) -> io::Result<Vec<u8>> {
        let mut cap = self.input_size()? as usize;
        let mut out = vec![0; cap + 1];
        let mut pos = 0;
        loop {
            trace!("requesting {} bytes", cap - pos);
            let ls = self.read(&mut out[pos..])?;
            pos += ls;
            if pos >= cap {
                // the size hint was wrong
                warn!("size was wrong, expanding");
                cap += 16 * 1024;
                out.resize(cap, 0);
            }
            if ls == 0 {
                out.truncate(pos);
                return Ok(out);
            }
        }
    }
}

impl LengthRead for File {
    fn input_size(&self) -> io::Result<u64> {
        let meta = self.metadata()?;
        Ok(meta.len())
    }
}

impl<'a> LengthRead for ZipFile<'a> {
    fn input_size(&self) -> io::Result<u64> {
        Ok(self.size())
    }
}