30 public static function factory($resource =
'',
$size =
null)
36 switch (gettype($resource)) {
40 return new static($resource,
$size);
42 if (method_exists($resource,
'__toString')) {
55 if (!is_callable($callable)) {
56 throw new InvalidArgumentException(
'Must specify a callable');
59 $this->rewindFunction = $callable;
66 return $this->rewindFunction ? call_user_func($this->rewindFunction, $this) : parent::rewind();
78 $stream = fopen(
'php://temp',
'r+');
87 public function compress($filter =
'zlib.deflate')
90 $this->contentEncoding = $result ? $filter :
false;
95 public function uncompress($filter =
'zlib.inflate')
101 if ($filter ==
'zlib.inflate') {
107 if (stream_get_contents($this->stream, 3, 0) ===
"\x1f\x8b\x08") {
112 $this->contentEncoding =
false;
127 public function getContentMd5($rawOutput =
false, $base64Encode =
false)
129 if ($hash = self::getHash($this,
'md5', $rawOutput)) {
130 return $hash && $base64Encode ? base64_encode($hash) : $hash;
147 public static function calculateMd5(EntityBodyInterface $body, $rawOutput =
false, $base64Encode =
false)
149 Version::warn(__CLASS__ .
' is deprecated. Use getContentMd5()');
150 return $body->getContentMd5($rawOutput, $base64Encode);
155 $this->contentEncoding = $streamFilterContentEncoding;
162 return strtr($this->contentEncoding, array(
163 'zlib.deflate' =>
'gzip',
164 'bzip2.compress' =>
'compress'
176 $handle = fopen(
'php://temp',
'r+');
177 $filter = @stream_filter_append($handle, $filter, STREAM_FILTER_WRITE);
183 $this->
seek($offsetStart);
184 while ($data = fread($this->stream, 8096)) {
185 fwrite($handle, $data);
188 fclose($this->stream);
189 $this->stream = $handle;
190 stream_filter_remove($filter);
191 $stat = fstat($this->stream);
192 $this->size = $stat[
'size'];
197 $this->rewindFunction =
null;