From 2669ee5738bafc877dd21eb79ae74fc366f96f22 Mon Sep 17 00:00:00 2001 From: Rogiel Sulzbach Date: Mon, 18 Jan 2016 11:12:49 -0200 Subject: [PATCH] Implements end-of-file calls on streams --- src/Stream/Block/BlockStream.php | 6 +++++- src/Stream/CompressedStream.php | 7 +++++++ src/Stream/EncryptedStream.php | 7 +++++++ src/Stream/FileStream.php | 7 +++++++ src/Stream/MemoryStream.php | 7 +++++++ src/Stream/Parser/BinaryStreamParser.php | 4 ++++ src/Stream/Stream.php | 7 +++++++ 7 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Stream/Block/BlockStream.php b/src/Stream/Block/BlockStream.php index 0545263..c965e3e 100644 --- a/src/Stream/Block/BlockStream.php +++ b/src/Stream/Block/BlockStream.php @@ -106,7 +106,7 @@ class BlockStream implements Stream { } public function readBytes($bytes) { - if($this->position >= $this->block->getSize()) { + if($this->eof()) { return false; } if(($this->position + $bytes) > $this->block->getSize()) { @@ -146,6 +146,10 @@ class BlockStream implements Stream { $this->position += $bytes; } + public function eof() { + return $this->position >= $this->block->getSize(); + } + // ----------------------------------------------------------------------------------------------------------------- private function readSector(Sector $sector) { diff --git a/src/Stream/CompressedStream.php b/src/Stream/CompressedStream.php index 8f1d6af..e23fbf9 100644 --- a/src/Stream/CompressedStream.php +++ b/src/Stream/CompressedStream.php @@ -86,4 +86,11 @@ class CompressedStream implements Stream{ $this->stream->skip($position); } + /** + * {@inheritdoc} + */ + public function eof() { + return $this->stream->eof(); + } + } \ No newline at end of file diff --git a/src/Stream/EncryptedStream.php b/src/Stream/EncryptedStream.php index dd6e006..a912699 100644 --- a/src/Stream/EncryptedStream.php +++ b/src/Stream/EncryptedStream.php @@ -107,4 +107,11 @@ class EncryptedStream implements Stream{ $this->bufferPointer = 0xFF; } + /** + * {@inheritdoc} + */ + public function eof() { + return $this->stream->eof(); + } + } \ No newline at end of file diff --git a/src/Stream/FileStream.php b/src/Stream/FileStream.php index 2f5c94d..511bba8 100644 --- a/src/Stream/FileStream.php +++ b/src/Stream/FileStream.php @@ -77,6 +77,13 @@ class FileStream implements Stream { fseek($this->handle, $position, SEEK_CUR); } + /** + * {@inheritdoc} + */ + public function eof() { + return feof($this->handle); + } + // ----------------------------------------------------------------------------------------------------------------- public function __clone() { diff --git a/src/Stream/MemoryStream.php b/src/Stream/MemoryStream.php index 6c31f77..0da6beb 100644 --- a/src/Stream/MemoryStream.php +++ b/src/Stream/MemoryStream.php @@ -77,4 +77,11 @@ class MemoryStream implements Stream { $this->pointer += $position; } + /** + * {@inheritdoc} + */ + public function eof() { + return strlen($this->data) >= $this->pointer; + } + } \ No newline at end of file diff --git a/src/Stream/Parser/BinaryStreamParser.php b/src/Stream/Parser/BinaryStreamParser.php index 3964a7d..d6e8932 100644 --- a/src/Stream/Parser/BinaryStreamParser.php +++ b/src/Stream/Parser/BinaryStreamParser.php @@ -69,4 +69,8 @@ class BinaryStreamParser { return $this->stream->readBytes($size); } + public function eof() { + return $this->stream->eof(); + } + } \ No newline at end of file diff --git a/src/Stream/Stream.php b/src/Stream/Stream.php index 5aaa579..e9e13ae 100644 --- a/src/Stream/Stream.php +++ b/src/Stream/Stream.php @@ -64,4 +64,11 @@ interface Stream { */ public function skip($bytes); + /** + * Checks if the stream has already reached the EOF + * + * @return boolean if the end of file has been reached + */ + public function eof(); + } \ No newline at end of file