aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKostya Shishkov <kostya.shishkov@gmail.com>2017-05-14 17:59:35 +0200
committerKostya Shishkov <kostya.shishkov@gmail.com>2017-05-14 17:59:35 +0200
commit89de616c775e58728cacc7065e0e47528a78d9df (patch)
tree041540944d334b3ddea2332175d1a86fe04b71b6 /src
parent20ef4353905883ca289782858ccfcd7d2146fa42 (diff)
downloadnihav-89de616c775e58728cacc7065e0e47528a78d9df.tar.gz
make read_buf() always read proper sizes
Diffstat (limited to 'src')
-rw-r--r--src/io/byteio.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/io/byteio.rs b/src/io/byteio.rs
index acd67d4..9b93e10 100644
--- a/src/io/byteio.rs
+++ b/src/io/byteio.rs
@@ -17,6 +17,7 @@ type ByteIOResult<T> = Result<T, ByteIOError>;
pub trait ByteIO {
fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
+ fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
fn read_byte(&mut self) -> ByteIOResult<u8>;
fn peek_byte(&mut self) -> ByteIOResult<u8>;
@@ -70,6 +71,10 @@ impl<'a> ByteReader<'a> {
self.io.read_buf(buf)
}
+ pub fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
+ self.io.read_buf_some(buf)
+ }
+
pub fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
self.io.peek_buf(buf)
}
@@ -225,6 +230,13 @@ impl<'a> ByteIO for MemoryReader<'a> {
fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
let read_size = self.peek_buf(buf)?;
+ if read_size < buf.len() { return Err(ByteIOError::EOF); }
+ self.pos += read_size;
+ Ok(read_size)
+ }
+
+ fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
+ let read_size = self.peek_buf(buf)?;
self.pos += read_size;
Ok(read_size)
}
@@ -284,6 +296,14 @@ impl<'a> ByteIO for FileReader<'a> {
let res = self.file.read(buf);
if let Err(_) = res { return Err(ByteIOError::ReadError); }
let sz = res.unwrap();
+ if sz < buf.len() { self.eof = true; return Err(ByteIOError::EOF); }
+ Ok(sz)
+ }
+
+ fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
+ let res = self.file.read(buf);
+ if let Err(_) = res { return Err(ByteIOError::ReadError); }
+ let sz = res.unwrap();
if sz < buf.len() { self.eof = true; }
Ok(sz)
}