From 89de616c775e58728cacc7065e0e47528a78d9df Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sun, 14 May 2017 17:59:35 +0200 Subject: [PATCH] make read_buf() always read proper sizes --- src/io/byteio.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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 = Result; pub trait ByteIO { fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult; + fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult; fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult; fn read_byte(&mut self) -> ByteIOResult; fn peek_byte(&mut self) -> ByteIOResult; @@ -70,6 +71,10 @@ impl<'a> ByteReader<'a> { self.io.read_buf(buf) } + pub fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult { + self.io.read_buf_some(buf) + } + pub fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { self.io.peek_buf(buf) } @@ -224,6 +229,13 @@ impl<'a> ByteIO for MemoryReader<'a> { } fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + 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 { let read_size = self.peek_buf(buf)?; self.pos += read_size; Ok(read_size) @@ -281,6 +293,14 @@ impl<'a> ByteIO for FileReader<'a> { } fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { + 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 { let res = self.file.read(buf); if let Err(_) = res { return Err(ByteIOError::ReadError); } let sz = res.unwrap(); -- 2.39.5