[ Index ]

MailPress 7.2

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/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 = [];
  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 = [];
  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 = [];
  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 ? $this->arraySize : $end;
  86          $ret = '';
  87          for (; $this->offset < $end; ++$this->offset) {
  88              $ret .= $this->array[$this->offset];
  89          }
  90  
  91          return $ret;
  92      }
  93  
  94      /**
  95       * Writes $bytes to the end of the stream.
  96       *
  97       * @param string $bytes
  98       */
  99      public function write($bytes)
 100      {
 101          $to_add = str_split($bytes);
 102          foreach ($to_add as $value) {
 103              $this->array[] = $value;
 104          }
 105          $this->arraySize = count($this->array);
 106  
 107          foreach ($this->mirrors as $stream) {
 108              $stream->write($bytes);
 109          }
 110      }
 111  
 112      /**
 113       * Not used.
 114       */
 115      public function commit()
 116      {
 117      }
 118  
 119      /**
 120       * Attach $is to this stream.
 121       *
 122       * The stream acts as an observer, receiving all data that is written.
 123       * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
 124       */
 125      public function bind(Swift_InputByteStream $is)
 126      {
 127          $this->mirrors[] = $is;
 128      }
 129  
 130      /**
 131       * Remove an already bound stream.
 132       *
 133       * If $is is not bound, no errors will be raised.
 134       * If the stream currently has any buffered data it will be written to $is
 135       * before unbinding occurs.
 136       */
 137      public function unbind(Swift_InputByteStream $is)
 138      {
 139          foreach ($this->mirrors as $k => $stream) {
 140              if ($is === $stream) {
 141                  unset($this->mirrors[$k]);
 142              }
 143          }
 144      }
 145  
 146      /**
 147       * Move the internal read pointer to $byteOffset in the stream.
 148       *
 149       * @param int $byteOffset
 150       *
 151       * @return bool
 152       */
 153      public function setReadPointer($byteOffset)
 154      {
 155          if ($byteOffset > $this->arraySize) {
 156              $byteOffset = $this->arraySize;
 157          } elseif ($byteOffset < 0) {
 158              $byteOffset = 0;
 159          }
 160  
 161          $this->offset = $byteOffset;
 162      }
 163  
 164      /**
 165       * Flush the contents of the stream (empty it) and set the internal pointer
 166       * to the beginning.
 167       */
 168      public function flushBuffers()
 169      {
 170          $this->offset = 0;
 171          $this->array = [];
 172          $this->arraySize = 0;
 173  
 174          foreach ($this->mirrors as $stream) {
 175              $stream->flushBuffers();
 176          }
 177      }
 178  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1