|
0
|
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 |
/** |
|
|
13 |
* Allows reading and writing of bytes to and from an array. |
|
|
14 |
* @package Swift |
|
|
15 |
* @subpackage ByteStream |
|
|
16 |
* @author Chris Corbyn |
|
|
17 |
*/ |
|
|
18 |
class Swift_ByteStream_ArrayByteStream |
|
|
19 |
implements Swift_InputByteStream, Swift_OutputByteStream |
|
|
20 |
{ |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* The internal stack of bytes. |
|
|
24 |
* @var string[] |
|
|
25 |
* @access private |
|
|
26 |
*/ |
|
|
27 |
private $_array = array(); |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* The size of the stack |
|
|
31 |
* @var int |
|
|
32 |
* @access private |
|
|
33 |
*/ |
|
|
34 |
private $_arraySize = 0; |
|
|
35 |
|
|
|
36 |
/** |
|
|
37 |
* The internal pointer offset. |
|
|
38 |
* @var int |
|
|
39 |
* @access private |
|
|
40 |
*/ |
|
|
41 |
private $_offset = 0; |
|
|
42 |
|
|
|
43 |
/** Bound streams */ |
|
|
44 |
private $_mirrors = array(); |
|
|
45 |
|
|
|
46 |
/** |
|
|
47 |
* Create a new ArrayByteStream. |
|
|
48 |
* If $stack is given the stream will be populated with the bytes it contains. |
|
|
49 |
* @param mixed $stack of bytes in string or array form, optional |
|
|
50 |
*/ |
|
|
51 |
public function __construct($stack = null) |
|
|
52 |
{ |
|
|
53 |
if (is_array($stack)) |
|
|
54 |
{ |
|
|
55 |
$this->_array = $stack; |
|
|
56 |
$this->_arraySize = count($stack); |
|
|
57 |
} |
|
|
58 |
elseif (is_string($stack)) |
|
|
59 |
{ |
|
|
60 |
$this->write($stack); |
|
|
61 |
} |
|
|
62 |
else |
|
|
63 |
{ |
|
|
64 |
$this->_array = array(); |
|
|
65 |
} |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
/** |
|
|
69 |
* Reads $length bytes from the stream into a string and moves the pointer |
|
|
70 |
* through the stream by $length. If less bytes exist than are requested the |
|
|
71 |
* remaining bytes are given instead. If no bytes are remaining at all, boolean |
|
|
72 |
* false is returned. |
|
|
73 |
* @param int $length |
|
|
74 |
* @return string |
|
|
75 |
*/ |
|
|
76 |
public function read($length) |
|
|
77 |
{ |
|
|
78 |
if ($this->_offset == $this->_arraySize) |
|
|
79 |
{ |
|
|
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 |
{ |
|
|
91 |
$ret .= $this->_array[$this->_offset]; |
|
|
92 |
} |
|
|
93 |
return $ret; |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
/** |
|
|
97 |
* Writes $bytes to the end of the stream. |
|
|
98 |
* @param string $bytes |
|
|
99 |
*/ |
|
|
100 |
public function write($bytes) |
|
|
101 |
{ |
|
|
102 |
$to_add = str_split($bytes); |
|
|
103 |
foreach ($to_add as $value) |
|
|
104 |
{ |
|
|
105 |
$this->_array[] = $value; |
|
|
106 |
} |
|
|
107 |
$this->_arraySize = count($this->_array); |
|
|
108 |
|
|
|
109 |
foreach ($this->_mirrors as $stream) |
|
|
110 |
{ |
|
|
111 |
$stream->write($bytes); |
|
|
112 |
} |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
/** |
|
|
116 |
* Not used. |
|
|
117 |
*/ |
|
|
118 |
public function commit() |
|
|
119 |
{ |
|
|
120 |
} |
|
|
121 |
|
|
|
122 |
/** |
|
|
123 |
* Attach $is to this stream. |
|
|
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 |
* If $is is not bound, no errors will be raised. |
|
|
137 |
* If the stream currently has any buffered data it will be written to $is |
|
|
138 |
* before unbinding occurs. |
|
|
139 |
* |
|
|
140 |
* @param Swift_InputByteStream $is |
|
|
141 |
*/ |
|
|
142 |
public function unbind(Swift_InputByteStream $is) |
|
|
143 |
{ |
|
|
144 |
foreach ($this->_mirrors as $k => $stream) |
|
|
145 |
{ |
|
|
146 |
if ($is === $stream) |
|
|
147 |
{ |
|
|
148 |
unset($this->_mirrors[$k]); |
|
|
149 |
} |
|
|
150 |
} |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
/** |
|
|
154 |
* Move the internal read pointer to $byteOffset in the stream. |
|
|
155 |
* @param int $byteOffset |
|
|
156 |
* @return boolean |
|
|
157 |
*/ |
|
|
158 |
public function setReadPointer($byteOffset) |
|
|
159 |
{ |
|
|
160 |
if ($byteOffset > $this->_arraySize) |
|
|
161 |
{ |
|
|
162 |
$byteOffset = $this->_arraySize; |
|
|
163 |
} |
|
|
164 |
elseif ($byteOffset < 0) |
|
|
165 |
{ |
|
|
166 |
$byteOffset = 0; |
|
|
167 |
} |
|
|
168 |
|
|
|
169 |
$this->_offset = $byteOffset; |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
/** |
|
|
173 |
* Flush the contents of the stream (empty it) and set the internal pointer |
|
|
174 |
* to the beginning. |
|
|
175 |
*/ |
|
|
176 |
public function flushBuffers() |
|
|
177 |
{ |
|
|
178 |
$this->_offset = 0; |
|
|
179 |
$this->_array = array(); |
|
|
180 |
$this->_arraySize = 0; |
|
|
181 |
|
|
|
182 |
foreach ($this->_mirrors as $stream) |
|
|
183 |
{ |
|
|
184 |
$stream->flushBuffers(); |
|
|
185 |
} |
|
|
186 |
} |
|
|
187 |
|
|
|
188 |
} |