XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
ArrayByteStream.php
Go to the documentation of this file.
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 //@require 'Swift/InputByteStream.php';
12 //@require 'Swift/OutputByteStream.php';
13 
22 {
23 
29  private $_array = array();
30 
36  private $_arraySize = 0;
37 
43  private $_offset = 0;
44 
46  private $_mirrors = array();
47 
53  public function __construct($stack = null)
54  {
55  if (is_array($stack))
56  {
57  $this->_array = $stack;
58  $this->_arraySize = count($stack);
59  }
60  elseif (is_string($stack))
61  {
62  $this->write($stack);
63  }
64  else
65  {
66  $this->_array = array();
67  }
68  }
69 
78  public function read($length)
79  {
80  if ($this->_offset == $this->_arraySize)
81  {
82  return false;
83  }
84 
85  // Don't use array slice
86  $end = $length + $this->_offset;
87  $end = $this->_arraySize<$end
88  ?$this->_arraySize
89  :$end;
90  $ret = '';
91  for (; $this->_offset < $end; ++$this->_offset)
92  {
93  $ret .= $this->_array[$this->_offset];
94  }
95  return $ret;
96  }
97 
102  public function write($bytes)
103  {
104  $to_add = str_split($bytes);
105  foreach ($to_add as $value)
106  {
107  $this->_array[] = $value;
108  }
109  $this->_arraySize = count($this->_array);
110 
111  foreach ($this->_mirrors as $stream)
112  {
113  $stream->write($bytes);
114  }
115  }
116 
120  public function commit()
121  {
122  }
123 
131  public function bind(Swift_InputByteStream $is)
132  {
133  $this->_mirrors[] = $is;
134  }
135 
144  public function unbind(Swift_InputByteStream $is)
145  {
146  foreach ($this->_mirrors as $k => $stream)
147  {
148  if ($is === $stream)
149  {
150  unset($this->_mirrors[$k]);
151  }
152  }
153  }
154 
160  public function setReadPointer($byteOffset)
161  {
162  if ($byteOffset > $this->_arraySize)
163  {
164  $byteOffset = $this->_arraySize;
165  }
166  elseif ($byteOffset < 0)
167  {
168  $byteOffset = 0;
169  }
170 
171  $this->_offset = $byteOffset;
172  }
173 
178  public function flushBuffers()
179  {
180  $this->_offset = 0;
181  $this->_array = array();
182  $this->_arraySize = 0;
183 
184  foreach ($this->_mirrors as $stream)
185  {
186  $stream->flushBuffers();
187  }
188  }
189 
190 }