core/io: introduce flush() call for ByteIO
[nihav.git] / nihav-core / src / io / byteio.rs
index 3677501ae9f1c537ba03df96ad6889b5703bb1d0..44b26845f0a2622d7fe2f2d7b710fdc82d4ab78b 100644 (file)
@@ -49,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.
@@ -75,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.
@@ -241,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<usize> {
@@ -537,6 +539,8 @@ impl<'a> ByteIO for MemoryReader<'a> {
     fn size(&mut self) -> i64 {
         self.buf.len() as i64
     }
+
+    fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
 }
 
 impl<T: Read+Seek> FileReader<T> {
@@ -564,18 +568,30 @@ impl<T: Read+Seek> ByteIO for FileReader<T> {
     }
 
     fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
-        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<usize> {
         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)
     }
 
@@ -613,6 +629,8 @@ impl<T: Read+Seek> ByteIO for FileReader<T> {
     fn size(&mut self) -> i64 {
         -1
     }
+
+    fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
 }
 
 /// High-level bytestream writer.
@@ -639,7 +657,7 @@ impl<T: Read+Seek> ByteIO for FileReader<T> {
 /// [`MemoryWriter`]: ./struct.MemoryWriter.html
 #[allow(dead_code)]
 pub struct ByteWriter<'a> {
-    io: &'a mut ByteIO,
+    io: &'a mut dyn ByteIO,
 }
 
 /// Bytestream writer to memory.
@@ -665,7 +683,7 @@ pub struct GrowableMemoryWriter<'a> {
 
 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<()> {
@@ -762,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> {
@@ -838,6 +861,8 @@ impl<'a> ByteIO for MemoryWriter<'a> {
     fn size(&mut self) -> i64 {
         self.buf.len() as i64
     }
+
+    fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
 }
 
 impl<'a> GrowableMemoryWriter<'a> {
@@ -916,6 +941,8 @@ impl<'a> ByteIO for GrowableMemoryWriter<'a> {
     fn size(&mut self) -> i64 {
         self.buf.len() as i64
     }
+
+    fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
 }
 
 impl<T: Write+Seek> FileWriter<T> {
@@ -981,6 +1008,13 @@ impl<T: Write+Seek> ByteIO for FileWriter<T> {
     fn size(&mut self) -> i64 {
         -1
     }
+
+    fn flush(&mut self) -> ByteIOResult<()> {
+        match self.file.flush() {
+            Ok(()) => Ok(()),
+            Err(_) => Err(ByteIOError::WriteError),
+        }
+    }
 }
 
 #[cfg(test)]