/framework/vendor/swift/lib/classes/Swift/ByteStream/ArrayByteStream.php

http://zoop.googlecode.com/ · PHP · 190 lines · 94 code · 20 blank · 76 comment · 6 complexity · fc0ab0996e5b7631c33ca3e0c53bd3a7 MD5 · raw file

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