pub struct Reader<T: std::io::Read> {
input: T,
parser: crate::Parser,
buf: [u8; 4096],
}
impl<T: std::io::Read> Reader<T> {
pub fn new(input: T) -> Self {
Self {
input,
parser: crate::Parser::new(),
buf: [0; 4096],
}
}
#[allow(clippy::missing_panics_doc)]
pub fn read_frame(&mut self) -> crate::Result<crate::Frame> {
loop {
if let Some(frame) = self.parser.next_frame() {
return Ok(frame);
}
let bytes = self
.input
.read(&mut self.buf)
.map_err(|source| crate::Error::Read { source })?;
if bytes == 0 {
return Err(crate::Error::EOF);
}
self.parser.add_bytes(
&self.buf[..bytes],
);
}
}
pub fn offset(&self) -> Option<std::time::Duration> {
self.parser.offset()
}
}