NgCharacterStream.php 7.4KB

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