X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-core%2Fsrc%2Fio%2Fbyteio.rs;h=44b26845f0a2622d7fe2f2d7b710fdc82d4ab78b;hb=cf095d5693cc631dbf4fd6fa1dc229f859978017;hp=4463b2ba5563d8504afc4923f151d985712ce1ca;hpb=28884194ba2b2e762be5b34886d4b68dd0d1856b;p=nihav.git diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs index 4463b2b..44b2684 100644 --- a/nihav-core/src/io/byteio.rs +++ b/nihav-core/src/io/byteio.rs @@ -1,6 +1,5 @@ //! Bytestream reading/writing functionality. pub use std::io::SeekFrom; -use std::fs::File; use std::io::prelude::*; use std::ptr; @@ -50,6 +49,8 @@ pub trait ByteIO { fn is_seekable(&mut self) -> bool; /// Returns stream size or -1 if it is not known. fn size(&mut self) -> i64; + /// Flushes output if possible. + fn flush(&mut self) -> ByteIOResult<()>; } /// High-level bytestream reader. @@ -76,7 +77,7 @@ pub trait ByteIO { /// [`MemoryReader`]: ./struct.MemoryReader.html #[allow(dead_code)] pub struct ByteReader<'a> { - io: &'a mut ByteIO, + io: &'a mut dyn ByteIO, } /// Bytestream reader from memory. @@ -85,9 +86,9 @@ pub struct MemoryReader<'a> { pos: usize, } -/// Bytestream reader from file. -pub struct FileReader<'a> { - file: &'a File, +/// Bytestream reader from anything implementing `std::io::Read` and `std::io::Seek`. +pub struct FileReader { + file: Box, eof: bool, } @@ -200,6 +201,7 @@ write_int_func!(write_u64le, u64, 8, to_le); /// # Ok(()) /// # } /// ```` +#[allow(clippy::identity_op)] pub fn write_u24be(dst: &mut [u8], val: u32) -> ByteIOResult<()> { if dst.len() < 3 { return Err(ByteIOError::WriteError); } dst[0] = (val >> 16) as u8; @@ -208,6 +210,7 @@ pub fn write_u24be(dst: &mut [u8], val: u32) -> ByteIOResult<()> { Ok(()) } /// Writes 24-bit little-endian integer to the provided buffer. +#[allow(clippy::identity_op)] pub fn write_u24le(dst: &mut [u8], val: u32) -> ByteIOResult<()> { if dst.len() < 3 { return Err(ByteIOError::WriteError); } dst[0] = (val >> 0) as u8; @@ -240,7 +243,7 @@ impl<'a> ByteReader<'a> { /// # Ok(()) /// # } /// ```` - pub fn new(io: &'a mut ByteIO) -> Self { ByteReader { io } } + pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteReader { io } } /// Reads data into provided buffer. Partial read is treated as success. pub fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { @@ -267,6 +270,20 @@ impl<'a> ByteReader<'a> { self.io.peek_byte() } + /// Reads four-byte array from the stream. + pub fn read_tag(&mut self) -> ByteIOResult<[u8; 4]> { + let mut buf = [0u8; 4]; + self.io.read_buf(&mut buf)?; + Ok(buf) + } + + /// Reads four-byte array from the stream without advancing read position. + pub fn peek_tag(&mut self) -> ByteIOResult<[u8; 4]> { + let mut buf = [0u8; 4]; + self.io.peek_buf(&mut buf)?; + Ok(buf) + } + /// Reads 16-bit big-endian integer from the stream. pub fn read_u16be(&mut self) -> ByteIOResult { read_int!(self, u16, 2, to_be) @@ -472,7 +489,7 @@ impl<'a> ByteIO for MemoryReader<'a> { } fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { - let copy_size = if self.buf.len() - self.pos < buf.len() { self.buf.len() } else { buf.len() }; + let copy_size = if self.buf.len() - self.pos < buf.len() { self.buf.len() - self.pos } else { buf.len() }; if copy_size == 0 { return Err(ByteIOError::EOF); } let dst = &mut buf[0..copy_size]; dst.copy_from_slice(&self.buf[self.pos..][..copy_size]); @@ -522,17 +539,19 @@ impl<'a> ByteIO for MemoryReader<'a> { fn size(&mut self) -> i64 { self.buf.len() as i64 } + + fn flush(&mut self) -> ByteIOResult<()> { Ok(()) } } -impl<'a> FileReader<'a> { +impl FileReader { /// Constructs a new instance of `FileReader`. - pub fn new_read(file: &'a mut File) -> Self { - FileReader { file, eof : false } + pub fn new_read(file: T) -> Self { + FileReader { file: Box::new(file), eof : false } } } -impl<'a> ByteIO for FileReader<'a> { +impl ByteIO for FileReader { fn read_byte(&mut self) -> ByteIOResult { let mut byte : [u8; 1] = [0]; let ret = self.file.read(&mut byte); @@ -549,18 +568,30 @@ impl<'a> ByteIO for FileReader<'a> { } fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { - let ret = self.file.read(buf); - if ret.is_err() { return Err(ByteIOError::ReadError); } - let sz = ret.unwrap(); - if sz < buf.len() { self.eof = true; return Err(ByteIOError::EOF); } - Ok(sz) + match self.file.read_exact(buf) { + Ok(()) => Ok(buf.len()), + Err(err) => { + if err.kind() == std::io::ErrorKind::UnexpectedEof { + self.eof = true; + Err(ByteIOError::EOF) + } else { + Err(ByteIOError::ReadError) + } + }, + } } fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult { let ret = self.file.read(buf); if ret.is_err() { return Err(ByteIOError::ReadError); } let sz = ret.unwrap(); - if sz < buf.len() { self.eof = true; } + if sz < buf.len() { + if let Err(_err) = self.file.read(&mut buf[sz..][..1]) { + self.eof = true; + } else { + return Ok(sz + 1); + } + } Ok(sz) } @@ -598,6 +629,8 @@ impl<'a> ByteIO for FileReader<'a> { fn size(&mut self) -> i64 { -1 } + + fn flush(&mut self) -> ByteIOResult<()> { Ok(()) } } /// High-level bytestream writer. @@ -624,7 +657,7 @@ impl<'a> ByteIO for FileReader<'a> { /// [`MemoryWriter`]: ./struct.MemoryWriter.html #[allow(dead_code)] pub struct ByteWriter<'a> { - io: &'a mut ByteIO, + io: &'a mut dyn ByteIO, } /// Bytestream writer to memory. @@ -633,14 +666,24 @@ pub struct MemoryWriter<'a> { pos: usize, } -/// Bytestream writer to file. -pub struct FileWriter { - file: File, +/// Bytestream writer to anything implementing `std::io::Write` and `std::io::Seek`. +pub struct FileWriter { + file: Box, +} + +/// Bytestream writer to memory. +/// +/// Unlike [`MemoryWriter`] which writes to an array of fixed size, `GrowableMemoryWriter` grows output size when output size exceeds capacity. +/// +/// [`MemoryWriter`]: ./struct.MemoryWriter.html +pub struct GrowableMemoryWriter<'a> { + buf: &'a mut Vec, + pos: usize, } impl<'a> ByteWriter<'a> { /// Constructs a new instance of `ByteWriter`. - pub fn new(io: &'a mut ByteIO) -> Self { ByteWriter { io } } + pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteWriter { io } } /// Writes byte array to the output. pub fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { @@ -737,6 +780,11 @@ impl<'a> ByteWriter<'a> { if sz == -1 { return -1; } sz - (self.tell() as i64) } + + /// Flushes output stream if possible. + pub fn flush(&mut self) -> ByteIOResult<()> { + self.io.flush() + } } impl<'a> MemoryWriter<'a> { @@ -783,9 +831,87 @@ impl<'a> ByteIO for MemoryWriter<'a> { fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { if self.pos + buf.len() > self.buf.len() { return Err(ByteIOError::WriteError); } - for i in 0..buf.len() { - self.buf[self.pos + i] = buf[i]; + self.buf[self.pos..][..buf.len()].copy_from_slice(buf); + self.pos += buf.len(); + Ok(()) + } + + fn tell(&mut self) -> u64 { + self.pos as u64 + } + + fn seek(&mut self, pos: SeekFrom) -> ByteIOResult { + let cur_pos = self.pos as i64; + let cur_size = self.buf.len() as i64; + match pos { + SeekFrom::Start(x) => self.real_seek(x as i64), + SeekFrom::Current(x) => self.real_seek(cur_pos + x), + SeekFrom::End(x) => self.real_seek(cur_size + x), } + } + + fn is_eof(&self) -> bool { + self.pos >= self.buf.len() + } + + fn is_seekable(&mut self) -> bool { + true + } + + fn size(&mut self) -> i64 { + self.buf.len() as i64 + } + + fn flush(&mut self) -> ByteIOResult<()> { Ok(()) } +} + +impl<'a> GrowableMemoryWriter<'a> { + + /// Constructs a new instance of `GrowableMemoryWriter`. + pub fn new_write(buf: &'a mut Vec) -> Self { + GrowableMemoryWriter { buf, pos: 0 } + } + + fn real_seek(&mut self, pos: i64) -> ByteIOResult { + if pos < 0 || (pos as usize) > self.buf.len() { + return Err(ByteIOError::WrongRange) + } + self.pos = pos as usize; + Ok(pos as u64) + } +} + +impl<'a> ByteIO for GrowableMemoryWriter<'a> { + #[allow(unused_variables)] + fn read_byte(&mut self) -> ByteIOResult { + Err(ByteIOError::NotImplemented) + } + + #[allow(unused_variables)] + fn peek_byte(&mut self) -> ByteIOResult { + Err(ByteIOError::NotImplemented) + } + + #[allow(unused_variables)] + fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + Err(ByteIOError::NotImplemented) + } + + #[allow(unused_variables)] + fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult { + Err(ByteIOError::NotImplemented) + } + + #[allow(unused_variables)] + fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + Err(ByteIOError::NotImplemented) + } + + fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { + if self.pos + buf.len() > self.buf.len() { + self.buf.resize(self.pos + buf.len(), 0); + } + self.buf[self.pos..][..buf.len()].copy_from_slice(buf); self.pos += buf.len(); Ok(()) } @@ -815,16 +941,18 @@ impl<'a> ByteIO for MemoryWriter<'a> { fn size(&mut self) -> i64 { self.buf.len() as i64 } + + fn flush(&mut self) -> ByteIOResult<()> { Ok(()) } } -impl FileWriter { +impl FileWriter { /// Constructs a new instance of `FileWriter`. - pub fn new_write(file: File) -> Self { - FileWriter { file } + pub fn new_write(file: T) -> Self { + FileWriter { file: Box::new(file) } } } -impl ByteIO for FileWriter { +impl ByteIO for FileWriter { #[allow(unused_variables)] fn read_byte(&mut self) -> ByteIOResult { Err(ByteIOError::NotImplemented) @@ -880,6 +1008,13 @@ impl ByteIO for FileWriter { fn size(&mut self) -> i64 { -1 } + + fn flush(&mut self) -> ByteIOResult<()> { + match self.file.flush() { + Ok(()) => Ok(()), + Err(_) => Err(ByteIOError::WriteError), + } + } } #[cfg(test)]