size = $size; } /** * create large file with given size in kilobyte * * @param int $kilobyte * @return LargeFileContent */ public static function withKilobytes($kilobyte) { return new self($kilobyte * 1024); } /** * create large file with given size in megabyte * * @param int $megabyte * @return LargeFileContent */ public static function withMegabytes($megabyte) { return self::withKilobytes($megabyte * 1024); } /** * create large file with given size in gigabyte * * @param int $gigabyte * @return LargeFileContent */ public static function withGigabytes($gigabyte) { return self::withMegabytes($gigabyte * 1024); } /** * returns actual content * * @return string */ public function content() { return $this->doRead(0, $this->size); } /** * returns size of content * * @return int */ public function size() { return $this->size; } /** * actual reading of given byte count starting at given offset * * @param int $offset * @param int $count */ protected function doRead($offset, $count) { if (($offset + $count) > $this->size) { $count = $this->size - $offset; } $result = ''; for ($i = 0; $i < $count; $i++) { if (isset($this->content[$i + $offset])) { $result .= $this->content[$i + $offset]; } else { $result .= ' '; } } return $result; } /** * actual writing of data with specified length at given offset * * @param string $data * @param int $offset * @param int $length */ protected function doWrite($data, $offset, $length) { for ($i = 0; $i < $length; $i++) { $this->content[$i + $offset] = substr($data, $i, 1); } if ($offset >= $this->size) { $this->size += $length; } elseif (($offset + $length) > $this->size) { $this->size = $offset + $length; } } /** * Truncates a file to a given length * * @param int $size length to truncate file to * @return bool */ public function truncate($size) { $this->size = $size; foreach (array_filter(array_keys($this->content), function($pos) use ($size) { return $pos >= $size; } ) as $removePos) { unset($this->content[$removePos]); } return true; } }