FileInputStream.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007-2009 by Ivan Y. Khvostishkov                       *
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 FileInputStream extends InputStream
00016     {
00017         private $fd     = null;
00018         
00019         private $mark   = null;
00020         
00021         public function __construct($nameOrFd)
00022         {
00023             if (is_resource($nameOrFd)) {
00024                 if (get_resource_type($nameOrFd) !== 'stream')
00025                     throw new IOException('not a file resource');
00026                 
00027                 $this->fd = $nameOrFd;
00028                 
00029             } else {
00030                 try {
00031                     $this->fd = fopen($nameOrFd, 'rb');
00032                 } catch (BaseException $e) {
00033                     throw new IOException($e->getMessage());
00034                 }
00035             }
00036         }
00037         
00038         public function __destruct()
00039         {
00040             try {
00041                 $this->close();
00042             } catch (BaseException $e) {
00043                 // boo.
00044             }
00045         }
00046         
00050         public static function create($nameOrFd)
00051         {
00052             return new self($nameOrFd);
00053         }
00054         
00055         public function isEof()
00056         {
00057             return feof($this->fd);
00058         }
00059         
00063         public function mark()
00064         {
00065             $this->mark = $this->getOffset();
00066             
00067             return $this;
00068         }
00069         
00070         public function getOffset()
00071         {
00072             return ftell($this->fd);
00073         }
00074         
00075         public function markSupported()
00076         {
00077             return true;
00078         }
00079         
00083         public function reset()
00084         {
00085             return $this->seek($this->mark);
00086         }
00087         
00091         public function seek($offset)
00092         {
00093             if (fseek($this->fd, $offset) < 0)
00094                 throw new IOException(
00095                     'mark has been invalidated'
00096                 );
00097             
00098             return $this;
00099         }
00100         
00104         public function close()
00105         {
00106             if (!fclose($this->fd))
00107                 throw new IOException('failed to close the file');
00108             
00109             return $this;
00110         }
00111 
00112         public function read($length)
00113         {
00114             return $this->realRead($length);
00115         }
00116 
00117         public function readString($length = null)
00118         {
00119             return $this->realRead($length, true);
00120         }
00121 
00122         public function realRead($length, $string = false)
00123         {
00124             $result = $string
00125                 ? (
00126                     $length === null
00127                     ? fgets($this->fd)
00128                     : fgets($this->fd, $length)
00129                 )
00130                 : fread($this->fd, $length);
00131 
00132             if ($string && $result === false && feof($this->fd))
00133                 $result = null; // fgets returns false on eof
00134 
00135             if ($result === false)
00136                 throw new IOException('failed to read from file');
00137 
00138             if ($result === '')
00139                 $result = null; // eof
00140 
00141             return $result;
00142         }
00143     }
00144 ?>