00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class FileReader extends Reader
00017 {
00018 private $fd = null;
00019
00023 public static function create($fileName)
00024 {
00025 return new self($fileName);
00026 }
00027
00031 public function __construct($fileName)
00032 {
00033 if (!is_readable($fileName))
00034 throw new WrongStateException("Can not read {$fileName}");
00035
00036 try {
00037 $this->fd = fopen($fileName, 'rt');
00038 } catch (BaseException $e) {
00039 throw new IOException($e->getMessage());
00040 }
00041 }
00042
00043 public function __destruct()
00044 {
00045 try {
00046 $this->close();
00047 } catch (BaseException $e) {
00048
00049 }
00050 }
00051
00052 public function isEof()
00053 {
00054 return feof($this->fd);
00055 }
00056
00057 public function markSupported()
00058 {
00059 return true;
00060 }
00061
00065 public function mark()
00066 {
00067 $this->mark = ftell($this->fd);
00068
00069 return $this;
00070 }
00071
00075 public function reset()
00076 {
00077 if (fseek($this->fd, $this->mark) < 0)
00078 throw new IOException(
00079 'mark has been invalidated'
00080 );
00081
00082 return $this;
00083 }
00084
00088 public function close()
00089 {
00090 if (!fclose($this->fd))
00091 throw new IOException('failed to close the file');
00092
00093 return $this;
00094 }
00095
00096 public function read($length)
00097 {
00098 $result = null;
00099
00100 for ($i = 0; $i < $length; $i++) {
00101 if (
00102 ($char = fgetc($this->fd)) === false
00103 )
00104 break;
00105
00106 $result .= $char;
00107 }
00108
00109 return $result;
00110 }
00111 }
00112 ?>