00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class FileOutputStream extends OutputStream
00017 {
00018 private $fd = null;
00019
00020 public function __construct($nameOrFd, $append = false)
00021 {
00022 if (is_resource($nameOrFd)) {
00023 if (get_resource_type($nameOrFd) !== 'stream')
00024 throw new IOException('not a file resource');
00025
00026 $this->fd = $nameOrFd;
00027
00028 } else {
00029 try {
00030 $this->fd = fopen($nameOrFd, ($append ? 'a' : 'w').'b');
00031 } catch (BaseException $e) {
00032 throw new IOException($e->getMessage());
00033 }
00034
00035 }
00036 }
00037
00038 public function __destruct()
00039 {
00040 try {
00041 $this->close();
00042 } catch (BaseException $e) {
00043
00044 }
00045 }
00046
00050 public static function create($nameOrFd, $append = false)
00051 {
00052 return new self($nameOrFd, $append);
00053 }
00054
00058 public function write($buffer)
00059 {
00060 if (!$this->fd || $buffer === null)
00061 return $this;
00062
00063 try {
00064 $written = fwrite($this->fd, $buffer);
00065 } catch (BaseException $e) {
00066 throw new IOException($e->getMessage());
00067 }
00068
00069 if (!$written || $written < strlen($buffer))
00070 throw new IOException('disk full and/or buffer too large?');
00071
00072 return $this;
00073 }
00074
00078 public function close()
00079 {
00080 fclose($this->fd);
00081
00082 $this->fd = null;
00083
00084 return $this;
00085 }
00086 }
00087 ?>