ArrayByteStream.php 4.0 KB

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