FileReader.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Vladimir A. Altuchov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class FileReader extends Reader
00016     {
00017         private $fd = null;
00018                 
00022         public static function create($fileName)
00023         {
00024             return new self($fileName);
00025         }
00026         
00030         public function __construct($fileName)
00031         {
00032             if (!is_readable($fileName))
00033                 throw new WrongStateException("Can not read {$fileName}");
00034             
00035             try {
00036                 $this->fd = fopen($fileName, 'rt');
00037             } catch (BaseException $e) {
00038                 throw new IOException($e->getMessage());
00039             }
00040         }
00041         
00042         public function __destruct()
00043         {
00044             try {
00045                 $this->close();
00046             } catch (BaseException $e) {
00047                 // boo.
00048             }
00049         }
00050         
00051         public function isEof()
00052         {
00053             return feof($this->fd);
00054         }
00055         
00056         public function markSupported()
00057         {
00058             return true;
00059         }
00060         
00064         public function mark()
00065         {
00066             $this->mark = ftell($this->fd);
00067             
00068             return $this;
00069         }
00070         
00074         public function reset()
00075         {
00076             if (fseek($this->fd, $this->mark) < 0)
00077                 throw new IOException(
00078                     'mark has been invalidated'
00079                 );
00080             
00081             return $this;
00082         }
00083         
00087         public function close()
00088         {
00089             if (!fclose($this->fd))
00090                 throw new IOException('failed to close the file');
00091             
00092             return $this;
00093         }
00094         
00095         public function read($length)
00096         {
00097             $result = null;
00098             
00099             for ($i = 0; $i < $length; $i++) {
00100                 if (
00101                     ($char = fgetc($this->fd)) === false
00102                 )
00103                     break;
00104                 
00105                 $result .= $char;
00106             }
00107             
00108             return $result;
00109         }
00110     }
00111 ?>