From 6c0356da10eef6e02cee247d334f8515dd2952cb Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Wed, 30 Sep 2020 19:06:00 +0200 Subject: [PATCH] core/io: fix reading behaviour with e.g. BufReader --- nihav-core/src/io/byteio.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs index adbb636..7b5f220 100644 --- a/nihav-core/src/io/byteio.rs +++ b/nihav-core/src/io/byteio.rs @@ -564,18 +564,30 @@ impl ByteIO for FileReader { } 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) } -- 2.39.5