[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/ByteStream/ -> ArrayByteStream.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of SwiftMailer.
   5   * (c) 2004-2009 Chris Corbyn
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * Allows reading and writing of bytes to and from an array.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_ByteStream_ArrayByteStream implements Swift_InputByteStream, Swift_OutputByteStream
  17  {
  18      /**
  19       * The internal stack of bytes.
  20       *
  21       * @var string[]
  22       */
  23      private $_array = array();
  24  
  25      /**
  26       * The size of the stack.
  27       *
  28       * @var int
  29       */
  30      private $_arraySize = 0;
  31  
  32      /**
  33       * The internal pointer offset.
  34       *
  35       * @var int
  36       */
  37      private $_offset = 0;
  38  
  39      /**
  40       * Bound streams.
  41       *
  42       * @var Swift_InputByteStream[]
  43       */
  44      private $_mirrors = array();
  45  
  46      /**
  47       * Create a new ArrayByteStream.
  48       *
  49       * If $stack is given the stream will be populated with the bytes it contains.
  50       *
  51       * @param mixed $stack of bytes in string or array form, optional
  52       */
  53      public function __construct($stack = null)
  54      {
  55          if (is_array($stack)) {
  56              $this->_array = $stack;
  57              $this->_arraySize = count($stack);
  58          } elseif (is_string($stack)) {
  59              $this->write($stack);
  60          } else {
  61              $this->_array = array();
  62          }
  63      }
  64  
  65      /**
  66       * Reads $length bytes from the stream into a string and moves the pointer
  67       * through the stream by $length.
  68       *
  69       * If less bytes exist than are requested the
  70       * remaining bytes are given instead. If no bytes are remaining at all, boolean
  71       * false is returned.
  72       *
  73       * @param int $length
  74       *
  75       * @return string
  76       */
  77      public function read($length)
  78      {
  79          if ($this->_offset == $this->_arraySize) {
  80              return false;
  81          }
  82  
  83          // Don't use array slice
  84          $end = $length + $this->_offset;
  85          $end = $this->_arraySize < $end
  86              ? $this->_arraySize
  87              : $end;
  88          $ret = '';
  89          for (; $this->_offset < $end; ++$this->_offset) {
  90              $ret .= $this->_array[$this->_offset];
  91          }
  92  
  93          return $ret;
  94      }
  95  
  96      /**
  97       * Writes $bytes to the end of the stream.
  98       *
  99       * @param string $bytes
 100       */
 101      public function write($bytes)
 102      {
 103          $to_add = str_split($bytes);
 104          foreach ($to_add as $value) {
 105              $this->_array[] = $value;
 106          }
 107          $this->_arraySize = count($this->_array);
 108  
 109          foreach ($this->_mirrors as $stream) {
 110              $stream->write($bytes);
 111          }
 112      }
 113  
 114      /**
 115       * Not used.
 116       */
 117      public function commit()
 118      {
 119      }
 120  
 121      /**
 122       * Attach $is to this stream.
 123       *
 124       * The stream acts as an observer, receiving all data that is written.
 125       * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
 126       *
 127       * @param Swift_InputByteStream $is
 128       */
 129      public function bind(Swift_InputByteStream $is)
 130      {
 131          $this->_mirrors[] = $is;
 132      }
 133  
 134      /**
 135       * Remove an already bound stream.
 136       *
 137       * If $is is not bound, no errors will be raised.
 138       * If the stream currently has any buffered data it will be written to $is
 139       * before unbinding occurs.
 140       *
 141       * @param Swift_InputByteStream $is
 142       */
 143      public function unbind(Swift_InputByteStream $is)
 144      {
 145          foreach ($this->_mirrors as $k => $stream) {
 146              if ($is === $stream) {
 147                  unset($this->_mirrors[$k]);
 148              }
 149          }
 150      }
 151  
 152      /**
 153       * Move the internal read pointer to $byteOffset in the stream.
 154       *
 155       * @param int $byteOffset
 156       *
 157       * @return bool
 158       */
 159      public function setReadPointer($byteOffset)
 160      {
 161          if ($byteOffset > $this->_arraySize) {
 162              $byteOffset = $this->_arraySize;
 163          } elseif ($byteOffset < 0) {
 164              $byteOffset = 0;
 165          }
 166  
 167          $this->_offset = $byteOffset;
 168      }
 169  
 170      /**
 171       * Flush the contents of the stream (empty it) and set the internal pointer
 172       * to the beginning.
 173       */
 174      public function flushBuffers()
 175      {
 176          $this->_offset = 0;
 177          $this->_array = array();
 178          $this->_arraySize = 0;
 179  
 180          foreach ($this->_mirrors as $stream) {
 181              $stream->flushBuffers();
 182          }
 183      }
 184  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1