Skip to content

Commit

Permalink
Fix isEnd(). Replace feof() with fstat() check
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed Jun 18, 2017
1 parent f59058d commit ca4989c
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/BinaryStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,22 @@ public function readFloat($sizeInBits = 32) {
}
}

public function readChar() {
$chars = $this->readChars(1);
return $chars[0];
}
public function readChar() {
$chars = $this->readChars(1);
return $chars[0];
}

public function readChars($sizeInBytes = 1) {
$chars = array();
public function readChars($sizeInBytes = 1) {
$chars = array();

for ($i = 0; $i < $sizeInBytes; $i++) {
$chars[$i] = fgetc($this->fp);
if ($chars[$i] !== false)
$this->offset++;
}
for ($i = 0; $i < $sizeInBytes; $i++) {
$chars[$i] = fgetc($this->fp);
if ($chars[$i] !== false)
$this->offset++;
}

return $chars;
}
return $chars;
}

public function readString($sizeInBytes = 1) {
if ($this->bitOffset > 0) {
Expand Down Expand Up @@ -397,7 +397,10 @@ public function isMarked($name) {
}

public function isEnd() {
return feof($this->fp);
// feof() is simply useless (http://php.net/manual/ru/function.feof.php#67261)
// check by fstat() call
$stat = fstat($this->fp);
return $this->offset >= $stat['size'];
}

public function skip($bytes) {
Expand Down Expand Up @@ -575,7 +578,7 @@ public function writeString($string) {
/**
* Unpacks float (4 bytes) or double (8 bytes) from bytes. Takes into account current endianness settings.
* @param array $bytes Array of bytes. Should contain 4 or 8 elements.
* @return float Float
* @return float Float
*/
protected function unpackFloat(array $bytes) {
// own unpacker
Expand Down Expand Up @@ -616,7 +619,7 @@ protected function unpackFloat(array $bytes) {
* Packs float (4 bytes) or double (8 bytes) into bytes.
* @param float|double $float Float value
* @param int $sizeInBytes 4 or 8
* @return array Array of bytes representing float
* @return array Array of bytes representing float
*/
protected function packFloat($float, $sizeInBytes) {
// unpack exponent
Expand Down

0 comments on commit ca4989c

Please sign in to comment.