FileInputStream.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 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 /* $Id: FileInputStream.class.php 4687 2007-12-09 18:57:18Z voxus $ */
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                 // boo.
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; // eof
00109             
00110             return $result;
00111         }
00112     }
00113 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4