//! Bytestream reading/writing functionality.
pub use std::io::SeekFrom;
-use std::fs::File;
use std::io::prelude::*;
use std::ptr;
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.
/// [`MemoryReader`]: ./struct.MemoryReader.html
#[allow(dead_code)]
pub struct ByteReader<'a> {
- io: &'a mut ByteIO,
+ io: &'a mut dyn ByteIO,
}
/// Bytestream reader from memory.
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<T: Read+Seek> {
+ file: Box<T>,
eof: bool,
}
/// # 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> {
fn size(&mut self) -> i64 {
self.buf.len() as i64
}
+
+ fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
}
-impl<'a> FileReader<'a> {
+impl<T: Read+Seek> FileReader<T> {
/// 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<T: Read+Seek> ByteIO for FileReader<T> {
fn read_byte(&mut self) -> ByteIOResult<u8> {
let mut byte : [u8; 1] = [0];
let ret = self.file.read(&mut byte);
}
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)
}
fn size(&mut self) -> i64 {
-1
}
+
+ fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
}
/// High-level bytestream writer.
/// [`MemoryWriter`]: ./struct.MemoryWriter.html
#[allow(dead_code)]
pub struct ByteWriter<'a> {
- io: &'a mut ByteIO,
+ io: &'a mut dyn ByteIO,
}
/// Bytestream writer to memory.
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<T: Write+Seek> {
+ file: Box<T>,
}
/// Bytestream writer to memory.
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<()> {
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> {
fn size(&mut self) -> i64 {
self.buf.len() as i64
}
+
+ fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
}
impl<'a> GrowableMemoryWriter<'a> {
fn size(&mut self) -> i64 {
self.buf.len() as i64
}
+
+ fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
}
-impl FileWriter {
+impl<T: Write+Seek> FileWriter<T> {
/// 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<T: Write+Seek> ByteIO for FileWriter<T> {
#[allow(unused_variables)]
fn read_byte(&mut self) -> ByteIOResult<u8> {
Err(ByteIOError::NotImplemented)
fn size(&mut self) -> i64 {
-1
}
+
+ fn flush(&mut self) -> ByteIOResult<()> {
+ match self.file.flush() {
+ Ok(()) => Ok(()),
+ Err(_) => Err(ByteIOError::WriteError),
+ }
+ }
}
#[cfg(test)]