Пример #1
0
 /**
  * Returns 
  *
  * @return  io.Stream
  */
 protected function archiveBytesAsStream($version = -1)
 {
     static $bytes = array(1 => "CCA", 2 => "CCA");
     $s = new Stream();
     $s->open(STREAM_WRITE);
     $s->write($bytes[$version < 0 ? $this->version() : $version]);
     $s->write(str_repeat("", 248));
     // Reserved bytes
     $s->close();
     return $s;
 }
Пример #2
0
 public function write($string)
 {
     parent::write($string);
     $result = fwrite($this->stream, $string);
     if ($result === false) {
         throw new \Exception('Unable to write stream');
     }
     return $result;
 }
Пример #3
0
 public function get_contents()
 {
     $data = 'Test';
     $f = new Stream();
     $f->open(STREAM_MODE_WRITE);
     $f->write($data);
     $f->close();
     $this->assertEquals($data, FileUtil::getContents($f));
 }
 /**
  * @return array|bool
  */
 protected function parseSizeForJPEG()
 {
     $state = null;
     while (true) {
         switch ($state) {
             default:
                 $this->stream->read(2);
                 $state = 'started';
                 break;
             case 'started':
                 $b = $this->getByte();
                 if ($b === false) {
                     return false;
                 }
                 $state = $b == 0xff ? 'sof' : 'started';
                 break;
             case 'sof':
                 $b = $this->getByte();
                 if ($b === 0xe1) {
                     $data = $this->stream->read($this->readInt($this->stream->read(2)) - 2);
                     $stream = new Stream();
                     $stream->write($data);
                     if ($stream->read(4) === 'Exif') {
                         $stream->read(2);
                         $exif = new Faster_Image_B52f1a8_Exif_Parser($stream);
                     }
                     break;
                 }
                 if (in_array($b, range(0xe0, 0xef))) {
                     $state = 'skipframe';
                     break;
                 }
                 if (in_array($b, array_merge(range(0xc0, 0xc3), range(0xc5, 0xc7), range(0xc9, 0xcb), range(0xcd, 0xcf)))) {
                     $state = 'readsize';
                     break;
                 }
                 if ($b == 0xff) {
                     $state = 'sof';
                     break;
                 }
                 $state = 'skipframe';
                 break;
             case 'skipframe':
                 $skip = $this->readInt($this->stream->read(2)) - 2;
                 $this->stream->read($skip);
                 $state = 'started';
                 break;
             case 'readsize':
                 $c = $this->stream->read(7);
                 $size = array($this->readInt(substr($c, 5, 2)), $this->readInt(substr($c, 3, 2)));
                 if (isset($exif) && $exif->isRotated()) {
                     return array_reverse($size);
                 }
                 return $size;
         }
     }
     return false;
 }
Пример #5
0
 /**
  * Write the stream to the given destionation directly without using extra memory like storing in an array etc.
  *
  * @param mixed $destination file path.
  */
 public function save($destination)
 {
     $dest = new Stream(is_resource($destination) ? $destination : fopen($destination, 'w+'));
     while (!$this->feof()) {
         $dest->write($this->read());
     }
     if (!is_resource($destination)) {
         // close the file if we opened it otwhise dont touch.
         $dest->close();
     }
 }
Пример #6
0
 /**
  * Get Thumbnail
  *
  * @return  img.Image  
  */
 public function getThumbnail()
 {
     $s = new Stream();
     $s->open(STREAM_MODE_WRITE);
     $s->write(exif_thumbnail($this->getFilename()));
     $s->rewind();
     return Image::loadFrom(new StreamReader($s));
 }
Пример #7
0
 /**
  * @param $file
  * @param $dstRedisServer
  * @param int $seek
  * @return bool
  */
 static function syncFromAof($file, $dstRedisServer, $seek = 0)
 {
     $fp = fopen($file, 'r');
     if (!$fp) {
         return false;
     }
     //偏移
     if ($seek > 0) {
         fseek($fp, $seek);
     }
     //目标Redis服务器
     $dstRedis = stream_socket_client($dstRedisServer, $errno, $errstr, 10);
     if (!$dstRedis) {
         return false;
     }
     $n_bytes = $seek;
     $n_lines = 0;
     $n_success = 0;
     $_send = '';
     $patten = "#^\\*(\\d+)\r\n\$#";
     readfile:
     while (!feof($fp)) {
         $line = fgets($fp, 8192);
         if ($line === false) {
             echo "line empty\n";
             break;
         }
         $n_lines++;
         $r = preg_match($patten, $line);
         if ($r) {
             if ($_send) {
                 if (Stream::write($dstRedis, $_send) === false) {
                     die("写入Redis失败. {$_send}");
                 }
                 $n_bytes += strlen($_send);
                 //清理数据
                 if (fread($dstRedis, 8192) == false) {
                     echo "读取Redis失败. {$_send}\n";
                     for ($i = 0; $i < 10; $i++) {
                         $dstRedis = stream_socket_client($dstRedisServer, $errno, $errstr, 10);
                         if (!$dstRedis) {
                             echo "连接到Redis({$dstRedisServer})失败, 1秒后重试.\n";
                             sleep(1);
                         }
                     }
                     if (!$dstRedis) {
                         echo "连接到Redis({$dstRedisServer})失败\n";
                         return false;
                     }
                     $_send = $line;
                     continue;
                 }
                 $n_success++;
                 if ($n_success % 10000 == 0) {
                     $seek = ftell($fp);
                     echo "KEY: {$n_success}, LINE: {$n_lines}, BYTE: {$n_bytes}, SEEK: {$seek}. 完成\n";
                 }
             }
             $_send = $line;
         } else {
             $_send .= $line;
         }
     }
     wait:
     //等待100ms后继续读
     sleep(2);
     $seek = ftell($fp);
     echo "read eof, seek={$seek}\n";
     //关闭文件
     fclose($fp);
     $fp = fopen($file, 'r');
     if (!$fp) {
         exit("打开文件失败,seek={$seek}\n");
     }
     if (fseek($fp, $seek) < 0) {
         exit("feek({$seek})失败\n");
     }
     goto readfile;
 }
Пример #8
0
 public function testCannotRetrieveStreamAfterMove()
 {
     $this->setExpectedException("RuntimeException", "File has already been moved");
     $stream = new Stream("php://temp", "wb+");
     $stream->write("test");
     $upload = new UploadedFile($stream, 1, UPLOAD_ERR_OK);
     $upload->moveTo(__DIR__ . "/tmpfile");
     $this->assertTrue(file_exists(__DIR__ . "/tmpfile"));
     $upload->getStream();
 }
 public function write($content)
 {
     $modified = $this->mutateOut($content);
     return parent::write($modified);
 }
Пример #10
0
 public function positionAfterReOpen()
 {
     $s = new Stream();
     $s->open(STREAM_MODE_WRITE);
     $s->write('GIF89a');
     $s->close();
     $s->open(STREAM_MODE_READ);
     $this->assertEquals(0, $s->tell());
     $this->assertEquals('GIF89a', $s->read());
     $s->close();
 }