PageRenderTime 30ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/CharacterStream/NgCharacterStream.php

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