1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: 22: 23: 24: 25: 26:
27:
28: class Swift_CharacterStream_NgCharacterStream implements Swift_CharacterStream
29: {
30: 31: 32: 33: 34:
35: private $_charReader;
36:
37: 38: 39: 40: 41:
42: private $_charReaderFactory;
43:
44: 45: 46: 47: 48:
49: private $_charset;
50:
51: 52: 53: 54: 55:
56: private $_datas = "";
57:
58: 59: 60: 61: 62:
63: private $_datasSize = 0;
64:
65: 66: 67: 68: 69:
70: private $_map;
71:
72: 73: 74: 75: 76:
77: private $_mapType = 0;
78:
79: 80: 81: 82: 83:
84: private $_charCount = 0;
85:
86: 87: 88: 89: 90:
91: private $_currentPos = 0;
92:
93: 94: 95: 96: 97: 98:
99: public function __construct(Swift_CharacterReaderFactory $factory, $charset)
100: {
101: $this->setCharacterReaderFactory($factory);
102: $this->setCharacterSet($charset);
103: }
104:
105:
106:
107: 108: 109: 110:
111: public function setCharacterSet($charset)
112: {
113: $this->_charset = $charset;
114: $this->_charReader = null;
115: $this->_mapType = 0;
116: }
117:
118: 119: 120: 121:
122: public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory)
123: {
124: $this->_charReaderFactory = $factory;
125: }
126:
127: 128: 129: 130:
131: public function flushContents()
132: {
133: $this->_datas = null;
134: $this->_map = null;
135: $this->_charCount = 0;
136: $this->_currentPos = 0;
137: $this->_datasSize = 0;
138: }
139:
140: 141: 142: 143: 144:
145: public function importByteStream(Swift_OutputByteStream $os)
146: {
147: $this->flushContents();
148: $blocks=512;
149: $os->setReadPointer(0);
150: while(false!==($read = $os->read($blocks)))
151: $this->write($read);
152: }
153:
154: 155: 156: 157: 158:
159: public function importString($string)
160: {
161: $this->flushContents();
162: $this->write($string);
163: }
164:
165: 166: 167: 168: 169: 170:
171: public function read($length)
172: {
173: if ($this->_currentPos>=$this->_charCount) {
174: return false;
175: }
176: $ret=false;
177: $length = ($this->_currentPos+$length > $this->_charCount)
178: ? $this->_charCount - $this->_currentPos
179: : $length;
180: switch ($this->_mapType) {
181: case Swift_CharacterReader::MAP_TYPE_FIXED_LEN:
182: $len = $length*$this->_map;
183: $ret = substr($this->_datas,
184: $this->_currentPos * $this->_map,
185: $len);
186: $this->_currentPos += $length;
187: break;
188:
189: case Swift_CharacterReader::MAP_TYPE_INVALID:
190: $end = $this->_currentPos + $length;
191: $end = $end > $this->_charCount
192: ?$this->_charCount
193: :$end;
194: $ret = '';
195: for (; $this->_currentPos < $length; ++$this->_currentPos) {
196: if (isset ($this->_map[$this->_currentPos])) {
197: $ret .= '?';
198: } else {
199: $ret .= $this->_datas[$this->_currentPos];
200: }
201: }
202: break;
203:
204: case Swift_CharacterReader::MAP_TYPE_POSITIONS:
205: $end = $this->_currentPos + $length;
206: $end = $end > $this->_charCount
207: ?$this->_charCount
208: :$end;
209: $ret = '';
210: $start = 0;
211: if ($this->_currentPos>0) {
212: $start = $this->_map['p'][$this->_currentPos-1];
213: }
214: $to = $start;
215: for (; $this->_currentPos < $end; ++$this->_currentPos) {
216: if (isset($this->_map['i'][$this->_currentPos])) {
217: $ret .= substr($this->_datas, $start, $to - $start).'?';
218: $start = $this->_map['p'][$this->_currentPos];
219: } else {
220: $to = $this->_map['p'][$this->_currentPos];
221: }
222: }
223: $ret .= substr($this->_datas, $start, $to - $start);
224: break;
225: }
226:
227: return $ret;
228: }
229:
230: 231: 232: 233: 234: 235:
236: public function readBytes($length)
237: {
238: $read=$this->read($length);
239: if ($read!==false) {
240: $ret = array_map('ord', str_split($read, 1));
241:
242: return $ret;
243: }
244:
245: return false;
246: }
247:
248: 249: 250: 251: 252:
253: public function setPointer($charOffset)
254: {
255: if ($this->_charCount<$charOffset) {
256: $charOffset=$this->_charCount;
257: }
258: $this->_currentPos = $charOffset;
259: }
260:
261: 262: 263: 264: 265:
266: public function write($chars)
267: {
268: if (!isset($this->_charReader)) {
269: $this->_charReader = $this->_charReaderFactory->getReaderFor(
270: $this->_charset);
271: $this->_map = array();
272: $this->_mapType = $this->_charReader->getMapType();
273: }
274: $ignored='';
275: $this->_datas .= $chars;
276: $this->_charCount += $this->_charReader->getCharPositions(substr($this->_datas, $this->_datasSize), $this->_datasSize, $this->_map, $ignored);
277: if ($ignored!==false) {
278: $this->_datasSize=strlen($this->_datas)-strlen($ignored);
279: } else {
280: $this->_datasSize=strlen($this->_datas);
281: }
282: }
283: }
284: