00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class FileInputStream extends InputStream
00017 {
00018 private $fd = null;
00019
00020 private $mark = null;
00021
00022 public function __construct($nameOrFd)
00023 {
00024 if (is_resource($nameOrFd)) {
00025 if (get_resource_type($nameOrFd) !== 'stream')
00026 throw new IOException('not a file resource');
00027
00028 $this->fd = $nameOrFd;
00029
00030 } else {
00031 try {
00032 $this->fd = fopen($nameOrFd, 'rb');
00033 } catch (BaseException $e) {
00034 throw new IOException($e->getMessage());
00035 }
00036 }
00037 }
00038
00039 public function __destruct()
00040 {
00041 try {
00042 $this->close();
00043 } catch (BaseException $e) {
00044
00045 }
00046 }
00047
00051 public static function create($nameOrFd)
00052 {
00053 return new self($nameOrFd);
00054 }
00055
00056 public function isEof()
00057 {
00058 return feof($this->fd);
00059 }
00060
00064 public function mark()
00065 {
00066 $this->mark = ftell($this->fd);
00067
00068 return $this;
00069 }
00070
00071 public function markSupported()
00072 {
00073 return true;
00074 }
00075
00079 public function reset()
00080 {
00081 if (fseek($this->fd, $this->mark) < 0)
00082 throw new IOException(
00083 'mark has been invalidated'
00084 );
00085
00086 return $this;
00087 }
00088
00092 public function close()
00093 {
00094 if (!fclose($this->fd))
00095 throw new IOException('failed to close the file');
00096
00097 return $this;
00098 }
00099
00100 public function read($length)
00101 {
00102 $result = fread($this->fd, $length);
00103
00104 if ($result === false)
00105 throw new IOException('failer to read from file');
00106
00107 if ($result === '')
00108 $result = null;
00109
00110 return $result;
00111 }
00112 }
00113 ?>