Skip to content

Commit

Permalink
Make Buffer::read() consistent with other InputStreams, returning "" …
Browse files Browse the repository at this point in the history
…at EOF
  • Loading branch information
thekid committed Jun 22, 2024
1 parent fa24d20 commit 2faec8c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
10 changes: 3 additions & 7 deletions src/main/php/io/streams/Buffer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,17 @@ public function available() {
* Read a string
*
* @param int $limit
* @return ?string
* @return string
*/
public function read($limit= 8192) {
if ($this->file) {
$this->draining || $this->file->seek(0, SEEK_SET) && $this->draining= true;
$chunk= $this->file->read($limit);
return false === $chunk ? null : $chunk;
} else if ($this->pointer < $this->size) {
return (string)$this->file->read($limit);
} else {
$this->draining= true;
$chunk= substr($this->memory, $this->pointer, $limit);
$this->pointer+= strlen($chunk);
return $chunk;
} else {
$this->draining= true;
return null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/php/io/unittest/BufferTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function read_after_eof($length) {
Assert::equals($length, $fixture->available());
Assert::equals($bytes, $fixture->read());
Assert::equals(0, $fixture->available());
Assert::equals(null, $fixture->read());
Assert::equals('', $fixture->read());
}

#[Test, Values([127, 128, 129])]
Expand Down

0 comments on commit 2faec8c

Please sign in to comment.