Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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
 
14
/**
15
 * Allows reading and writing of bytes to and from an array.
16
 * @package Swift
17
 * @subpackage ByteStream
18
 * @author Chris Corbyn
19
 */
20
class Swift_ByteStream_ArrayByteStream
21
  implements Swift_InputByteStream, Swift_OutputByteStream
22
{
23
 
24
  /**
25
   * The internal stack of bytes.
26
   * @var string[]
27
   * @access private
28
   */
29
  private $_array = array();
30
 
31
  /**
32
   * The size of the stack
33
   * @var int
34
   * @access private
35
   */
36
  private $_arraySize = 0;
37
 
38
  /**
39
   * The internal pointer offset.
40
   * @var int
41
   * @access private
42
   */
43
  private $_offset = 0;
44
 
45
  /** Bound streams */
46
  private $_mirrors = array();
47
 
48
  /**
49
   * Create a new ArrayByteStream.
50
   * If $stack is given the stream will be populated with the bytes it contains.
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
    {
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
 
70
  /**
71
   * Reads $length bytes from the stream into a string and moves the pointer
72
   * through the stream by $length. If less bytes exist than are requested the
73
   * remaining bytes are given instead. If no bytes are remaining at all, boolean
74
   * false is returned.
75
   * @param int $length
76
   * @return string
77
   */
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
 
98
  /**
99
   * Writes $bytes to the end of the stream.
100
   * @param string $bytes
101
   */
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
 
117
  /**
118
   * Not used.
119
   */
120
  public function commit()
121
  {
122
  }
123
 
124
  /**
125
   * Attach $is to this stream.
126
   * The stream acts as an observer, receiving all data that is written.
127
   * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
128
   *
129
   * @param Swift_InputByteStream $is
130
   */
131
  public function bind(Swift_InputByteStream $is)
132
  {
133
    $this->_mirrors[] = $is;
134
  }
135
 
136
  /**
137
   * Remove an already bound stream.
138
   * If $is is not bound, no errors will be raised.
139
   * If the stream currently has any buffered data it will be written to $is
140
   * before unbinding occurs.
141
   *
142
   * @param Swift_InputByteStream $is
143
   */
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
 
155
  /**
156
   * Move the internal read pointer to $byteOffset in the stream.
157
   * @param int $byteOffset
158
   * @return boolean
159
   */
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
 
174
  /**
175
   * Flush the contents of the stream (empty it) and set the internal pointer
176
   * to the beginning.
177
   */
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
}