NgCharacterStream.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. * A CharacterStream implementation which stores characters in an internal array.
  11. *
  12. * @package Swift
  13. * @subpackage CharacterStream
  14. * @author Xavier De Cock <xdecock@gmail.com>
  15. */
  16. class Swift_CharacterStream_NgCharacterStream implements Swift_CharacterStream
  17. {
  18. /**
  19. * The char reader (lazy-loaded) for the current charset.
  20. *
  21. * @var Swift_CharacterReader
  22. */
  23. private $_charReader;
  24. /**
  25. * A factory for creating CharacterReader instances.
  26. *
  27. * @var Swift_CharacterReaderFactory
  28. */
  29. private $_charReaderFactory;
  30. /**
  31. * The character set this stream is using.
  32. *
  33. * @var string
  34. */
  35. private $_charset;
  36. /**
  37. * The data's stored as-is.
  38. *
  39. * @var string
  40. */
  41. private $_datas = '';
  42. /**
  43. * Number of bytes in the stream
  44. *
  45. * @var integer
  46. */
  47. private $_datasSize = 0;
  48. /**
  49. * Map.
  50. *
  51. * @var mixed
  52. */
  53. private $_map;
  54. /**
  55. * Map Type.
  56. *
  57. * @var integer
  58. */
  59. private $_mapType = 0;
  60. /**
  61. * Number of characters in the stream.
  62. *
  63. * @var integer
  64. */
  65. private $_charCount = 0;
  66. /**
  67. * Position in the stream.
  68. *
  69. * @var integer
  70. */
  71. private $_currentPos = 0;
  72. /**
  73. * Constructor.
  74. *
  75. * @param Swift_CharacterReaderFactory $factory
  76. * @param string $charset
  77. */
  78. public function __construct(Swift_CharacterReaderFactory $factory, $charset)
  79. {
  80. $this->setCharacterReaderFactory($factory);
  81. $this->setCharacterSet($charset);
  82. }
  83. /* -- Changing parameters of the stream -- */
  84. /**
  85. * Set the character set used in this CharacterStream.
  86. *
  87. * @param string $charset
  88. */
  89. public function setCharacterSet($charset)
  90. {
  91. $this->_charset = $charset;
  92. $this->_charReader = null;
  93. $this->_mapType = 0;
  94. }
  95. /**
  96. * Set the CharacterReaderFactory for multi charset support.
  97. *
  98. * @param Swift_CharacterReaderFactory $factory
  99. */
  100. public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory)
  101. {
  102. $this->_charReaderFactory = $factory;
  103. }
  104. /**
  105. * @see Swift_CharacterStream::flushContents()
  106. */
  107. public function flushContents()
  108. {
  109. $this->_datas = null;
  110. $this->_map = null;
  111. $this->_charCount = 0;
  112. $this->_currentPos = 0;
  113. $this->_datasSize = 0;
  114. }
  115. /**
  116. * @see Swift_CharacterStream::importByteStream()
  117. *
  118. * @param Swift_OutputByteStream $os
  119. */
  120. public function importByteStream(Swift_OutputByteStream $os)
  121. {
  122. $this->flushContents();
  123. $blocks=512;
  124. $os->setReadPointer(0);
  125. while(false!==($read = $os->read($blocks)))
  126. $this->write($read);
  127. }
  128. /**
  129. * @see Swift_CharacterStream::importString()
  130. *
  131. * @param string $string
  132. */
  133. public function importString($string)
  134. {
  135. $this->flushContents();
  136. $this->write($string);
  137. }
  138. /**
  139. * @see Swift_CharacterStream::read()
  140. *
  141. * @param integer $length
  142. *
  143. * @return string
  144. */
  145. public function read($length)
  146. {
  147. if ($this->_currentPos>=$this->_charCount) {
  148. return false;
  149. }
  150. $ret=false;
  151. $length = ($this->_currentPos+$length > $this->_charCount)
  152. ? $this->_charCount - $this->_currentPos
  153. : $length;
  154. switch ($this->_mapType) {
  155. case Swift_CharacterReader::MAP_TYPE_FIXED_LEN:
  156. $len = $length*$this->_map;
  157. $ret = substr($this->_datas,
  158. $this->_currentPos * $this->_map,
  159. $len);
  160. $this->_currentPos += $length;
  161. break;
  162. case Swift_CharacterReader::MAP_TYPE_INVALID:
  163. $end = $this->_currentPos + $length;
  164. $end = $end > $this->_charCount
  165. ?$this->_charCount
  166. :$end;
  167. $ret = '';
  168. for (; $this->_currentPos < $length; ++$this->_currentPos) {
  169. if (isset ($this->_map[$this->_currentPos])) {
  170. $ret .= '?';
  171. } else {
  172. $ret .= $this->_datas[$this->_currentPos];
  173. }
  174. }
  175. break;
  176. case Swift_CharacterReader::MAP_TYPE_POSITIONS:
  177. $end = $this->_currentPos + $length;
  178. $end = $end > $this->_charCount
  179. ?$this->_charCount
  180. :$end;
  181. $ret = '';
  182. $start = 0;
  183. if ($this->_currentPos>0) {
  184. $start = $this->_map['p'][$this->_currentPos-1];
  185. }
  186. $to = $start;
  187. for (; $this->_currentPos < $end; ++$this->_currentPos) {
  188. if (isset($this->_map['i'][$this->_currentPos])) {
  189. $ret .= substr($this->_datas, $start, $to - $start).'?';
  190. $start = $this->_map['p'][$this->_currentPos];
  191. } else {
  192. $to = $this->_map['p'][$this->_currentPos];
  193. }
  194. }
  195. $ret .= substr($this->_datas, $start, $to - $start);
  196. break;
  197. }
  198. return $ret;
  199. }
  200. /**
  201. * @see Swift_CharacterStream::readBytes()
  202. *
  203. * @param integer $length
  204. *
  205. * @return integer[]
  206. */
  207. public function readBytes($length)
  208. {
  209. $read=$this->read($length);
  210. if ($read!==false) {
  211. $ret = array_map('ord', str_split($read, 1));
  212. return $ret;
  213. }
  214. return false;
  215. }
  216. /**
  217. * @see Swift_CharacterStream::setPointer()
  218. *
  219. * @param integer $charOffset
  220. */
  221. public function setPointer($charOffset)
  222. {
  223. if ($this->_charCount<$charOffset) {
  224. $charOffset=$this->_charCount;
  225. }
  226. $this->_currentPos = $charOffset;
  227. }
  228. /**
  229. * @see Swift_CharacterStream::write()
  230. *
  231. * @param string $chars
  232. */
  233. public function write($chars)
  234. {
  235. if (!isset($this->_charReader)) {
  236. $this->_charReader = $this->_charReaderFactory->getReaderFor(
  237. $this->_charset);
  238. $this->_map = array();
  239. $this->_mapType = $this->_charReader->getMapType();
  240. }
  241. $ignored='';
  242. $this->_datas .= $chars;
  243. $this->_charCount += $this->_charReader->getCharPositions(substr($this->_datas, $this->_datasSize), $this->_datasSize, $this->_map, $ignored);
  244. if ($ignored!==false) {
  245. $this->_datasSize=strlen($this->_datas)-strlen($ignored);
  246. } else {
  247. $this->_datasSize=strlen($this->_datas);
  248. }
  249. }
  250. }