home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / phing / system / io / InputStream.php < prev    next >
Encoding:
PHP Script  |  2007-08-28  |  5.4 KB  |  179 lines

  1. <?php
  2. /*
  3.  *  $Id: FileReader.php 123 2006-09-14 20:19:08Z mrook $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information please see
  19.  * <http://phing.info>. 
  20.  */
  21.  
  22. /**
  23.  * Wrapper class for PHP stream that supports read operations.
  24.  * 
  25.  * @package   phing.system.io
  26.  */
  27. class InputStream {
  28.     
  29.     /**
  30.      * @var resource The attached PHP stream.
  31.      */
  32.     protected $stream;
  33.     
  34.     /**
  35.      * @var int Position of stream cursor.
  36.      */
  37.     protected $currentPosition = 0;
  38.     
  39.     /**
  40.      * @var int Marked position of stream cursor.
  41.      */
  42.     protected $mark = 0;
  43.     
  44.     /**
  45.      * Construct a new InputStream.
  46.      * @param resource $stream Configured PHP stream for writing.
  47.      */
  48.     public function __construct($stream) {
  49.         if (!is_resource($stream)) {
  50.             throw new IOException("Passed argument is not a valid stream.");
  51.         }
  52.         $this->stream = $stream;
  53.     }
  54.  
  55.     /**
  56.      * Skip over $n bytes.
  57.      * @param int $n
  58.      */
  59.     public function skip($n) {
  60.         $start = $this->currentPosition;
  61.  
  62.         $ret = @fseek($this->stream, $n, SEEK_CUR);
  63.         if ( $ret === -1 )
  64.             return -1;
  65.  
  66.         $this->currentPosition = ftell($this->stream);
  67.  
  68.         if ( $start > $this->currentPosition )
  69.             $skipped = $start - $this->currentPosition;
  70.         else
  71.             $skipped = $this->currentPosition - $start;
  72.  
  73.         return $skipped;
  74.     }
  75.     
  76.     /**
  77.      * Read data from stream until $len chars or EOF.
  78.      * @param int $len Num chars to read.  If not specified this stream will read until EOF.
  79.      * @return string chars read or -1 if eof.
  80.      */
  81.     public function read($len = null) {
  82.         
  83.         if ($this->eof()) {
  84.             return -1;
  85.         }
  86.         
  87.         if ($len === null) { // we want to keep reading until we get an eof
  88.             $out = "";
  89.             while(!$this->eof()) {
  90.                 $out .= fread($this->stream, 8192);
  91.                 $this->currentPosition = ftell($this->stream);
  92.             }
  93.         } else {
  94.             $out = fread($this->stream, $len); // adding 1 seems to ensure that next call to read() will return EOF (-1)
  95.             $this->currentPosition = ftell($this->stream);
  96.         }
  97.  
  98.         return $out;
  99.     }    
  100.     
  101.     /**
  102.      * Marks the current position in this input stream.
  103.      * @throws IOException - if the underlying stream doesn't support this method.
  104.      */
  105.     public function mark() {
  106.         if (!$this->markSupported()) {
  107.             throw new IOException(get_class($this) . " does not support mark() and reset() methods.");
  108.         }
  109.         $this->mark = $this->currentPosition;
  110.     }
  111.     
  112.     /**
  113.      * Whether the input stream supports mark and reset methods.
  114.      * @return boolean
  115.      */
  116.     public function markSupported() {
  117.         return false;
  118.     }
  119.     
  120.     /**
  121.      * Repositions this stream to the position at the time the mark method was last called on this input stream.
  122.      * @throws IOException - if the underlying stream doesn't support this method.
  123.      */
  124.     function reset() {
  125.         if (!$this->markSupported()) {
  126.             throw new IOException(get_class($this) . " does not support mark() and reset() methods.");
  127.         }
  128.         // goes back to last mark, by default this would be 0 (i.e. rewind file).
  129.         fseek($this->stream, SEEK_SET, $this->mark);
  130.         $this->mark = 0;
  131.     }
  132.     
  133.     /**
  134.      * Closes stream.
  135.      * @throws IOException if stream cannot be closed (note that calling close() on an already-closed stream will not raise an exception)
  136.      */
  137.     public function close() {
  138.         if ($this->stream === null) {
  139.             return;
  140.         }
  141.         if (false === @fclose($this->stream)) {
  142.             // FAILED.
  143.             $msg = "Cannot fclose " . $this->file->__toString() . " $php_errormsg";
  144.             throw new IOException($msg);
  145.         }
  146.         $this->stream = null;
  147.     }
  148.     
  149.     /**
  150.      * Whether eof has been reached with stream.
  151.      * @return boolean
  152.      */
  153.     public function eof() {
  154.         return feof($this->stream);
  155.     }
  156.      
  157.     /**
  158.      * Reads a entire until EOF and places contents in passed-in variable.  Stream is closed after read.
  159.      *
  160.      * @param string &$rBuffer String variable where read contents will be put.
  161.      * @return TRUE on success.
  162.      * @author  Charlie Killian, charlie@tizac.com
  163.      * @throws IOException - if there is an error reading from stream.
  164.      * @deprecated - Instead, use the read() method or a BufferedReader.
  165.      */
  166.     public function readInto(&$rBuffer) {
  167.         $rBuffer = $this->read();
  168.         $this->close();
  169.     }
  170.     
  171.     /**
  172.      * Returns string representation of attached stream.
  173.      * @return string
  174.      */
  175.     public function __toString() {
  176.         return (string) $this->stream;
  177.     }
  178. }
  179.