Skip to content

Always read size of chunksize if in MemoryStream #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions spec/Encoding/MemoryStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class MemoryStream implements StreamInterface

private $size = 0;

public function __construct($body = "", $chunkSize = null)
private $chunkSize;

public function __construct($body = "", $chunkSize = 8192)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you determine this default? is there a way to figure out the system default instead of hardcoding it here? i guess 8kb is the norm for 64bit and 4096 for 32bit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default value is the BUFSIZ C constant which depends on the compiler used, and there is no way to get it here.

When reading various streaming PHP librairies, 8192 was a widely value used for buffer, so decided to keep here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the explanation!
oh this is the spec and not the source. sorry i asked, for a test we don't need to overthink it anyways.

{
$this->size = strlen($body);
$this->resource = fopen('php://memory', 'rw+');
Expand All @@ -22,6 +24,8 @@ public function __construct($body = "", $chunkSize = null)

fwrite($this->resource, $body);
fseek($this->resource, 0);

$this->chunkSize = $chunkSize;
}

public function __toString()
Expand Down Expand Up @@ -89,7 +93,7 @@ public function isReadable()

public function read($length)
{
return fread($this->resource, $length);
return fread($this->resource, min($this->chunkSize, $length));
}

public function getContents()
Expand Down