Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00015 final class FileOutputStream extends OutputStream
00016 {
00017 private $fd = null;
00018
00019 public function __construct($nameOrFd, $append = false)
00020 {
00021 if (is_resource($nameOrFd)) {
00022 if (get_resource_type($nameOrFd) !== 'stream')
00023 throw new IOException('not a file resource');
00024
00025 $this->fd = $nameOrFd;
00026
00027 } else {
00028 try {
00029 $this->fd = fopen($nameOrFd, ($append ? 'a' : 'w').'b');
00030
00031 Assert::isNotFalse(
00032 $this->fd,
00033 "File {$nameOrFd} must be exist"
00034 );
00035 } catch (BaseException $e) {
00036 throw new IOException($e->getMessage());
00037 }
00038 }
00039 }
00040
00041 public function __destruct()
00042 {
00043 try {
00044 $this->close();
00045 } catch (BaseException $e) {
00046
00047 }
00048 }
00049
00053 public static function create($nameOrFd, $append = false)
00054 {
00055 return new self($nameOrFd, $append);
00056 }
00057
00061 public function write($buffer)
00062 {
00063 if (!$this->fd || $buffer === null)
00064 return $this;
00065
00066 try {
00067 $written = fwrite($this->fd, $buffer);
00068 } catch (BaseException $e) {
00069 throw new IOException($e->getMessage());
00070 }
00071
00072 if (!$written || $written < strlen($buffer))
00073 throw new IOException('disk full and/or buffer too large?');
00074
00075 return $this;
00076 }
00077
00081 public function close()
00082 {
00083 fclose($this->fd);
00084
00085 $this->fd = null;
00086
00087 return $this;
00088 }
00089 }
00090 ?>