# StringInputStream.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
    00015     final class StringInputStream extends InputStream
    00016     {
    00017         private $string     = null;
    00018         private $length     = null;
    00019
    00020         private $position   = 0;
    00021         private $mark       = 0;
    00022
    00023         public function __construct($string)
    00024         {
    00025             Assert::isString($string);
    00026
    00027             $this->string = $string;
    00028             $this->length = strlen($string);
    00029         }
    00030
    00034         public static function create($string)
    00035         {
    00036             return new self($string);
    00037         }
    00038
    00039         public function isEof()
    00040         {
    00041             return ($this->position >= $this->length);
    00042         }
    00043
    00047         public function mark()
    00048         {
    00049             $this->mark = $this->position;
    00050
    00051             return $this;
    00052         }
    00053
    00054         public function markSupported()
    00055         {
    00056             return true;
    00057         }
    00058
    00062         public function reset()
    00063         {
    00064             $this->position = $this->mark;
    00065
    00066             return $this;
    00067         }
    00068
    00072         public function close()
    00073         {
    00074             $this->string = null;
    00075
    00076             return $this;
    00077         }
    00078
    00079         public function read($count)
    00080         {
    00081             if (!$this->string || $this->isEof())
    00082                 return null;
    00083
    00084             if ($count == 1) {
    00085                 $result = $this->string[$this->position];
    00086             } else {
    00087                 $result = substr($this->string, $this->position, $count);
    00088             }
    00089
    00090             $this->position += $count;
    00091
    00092             return $result;
    00093         }
    00094     }
    00095 ?>