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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use cfb;
use internal::streamname::{self, SUMMARY_INFO_STREAM_NAME};
use std::io::{self, Read, Seek, Write};

// ========================================================================= //

/// An IO reader for an embedded binary stream in a package.
pub struct StreamReader<'a, F: 'a> {
    stream: cfb::Stream<'a, F>,
}

impl<'a, F> StreamReader<'a, F> {
    pub(crate) fn new(stream: cfb::Stream<'a, F>) -> StreamReader<'a, F> {
        StreamReader { stream: stream }
    }
}

impl<'a, F: Read + Seek> Read for StreamReader<'a, F> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.stream.read(buf)
    }
}

// ========================================================================= //

/// An IO writer for an embedded binary stream in a package.
pub struct StreamWriter<'a, F: 'a> {
    stream: cfb::Stream<'a, F>,
}

impl<'a, F> StreamWriter<'a, F> {
    pub(crate) fn new(stream: cfb::Stream<'a, F>) -> StreamWriter<'a, F> {
        StreamWriter { stream: stream }
    }
}

impl<'a, F: Read + Seek + Write> Write for StreamWriter<'a, F> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.stream.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> { self.stream.flush() }
}

// ========================================================================= //

/// An iterator over the names of the binary streams in a package.
///
/// No guarantees are made about the order in which items are returned.
pub struct Streams<'a> {
    entries: cfb::Entries<'a>,
}

impl<'a> Streams<'a> {
    pub(crate) fn new(entries: cfb::Entries<'a>) -> Streams<'a> {
        Streams { entries: entries }
    }
}

impl<'a> Iterator for Streams<'a> {
    type Item = String;

    fn next(&mut self) -> Option<String> {
        loop {
            let entry = match self.entries.next() {
                Some(entry) => entry,
                None => return None,
            };
            if !entry.is_stream() || entry.name() == SUMMARY_INFO_STREAM_NAME {
                continue;
            }
            let (name, is_table) = streamname::decode(entry.name());
            if !is_table {
                return Some(name);
            }
        }
    }
}

// ========================================================================= //