57 self::_initializeMaps();
68 $this->_charset = $charset;
69 $this->_charReader = null;
79 $this->_charReaderFactory = $factory;
88 if (!isset($this->_charReader))
90 $this->_charReader = $this->_charReaderFactory
91 ->getReaderFor($this->_charset);
94 $startLength = $this->_charReader->getInitialByteSize();
95 while (
false !== $bytes = $os->
read($startLength))
98 for (
$i = 0, $len = strlen($bytes);
$i < $len; ++
$i)
100 $c[] = self::$_byteMap[$bytes[
$i]];
103 $need = $this->_charReader
104 ->validateByteSequence($c, $size);
106 false !== $bytes = $os->
read($need))
108 for (
$i = 0, $len = strlen($bytes);
$i < $len; ++
$i)
110 $c[] = self::$_byteMap[$bytes[
$i]];
113 $this->_array[] = $c;
126 $this->
write($string);
137 if ($this->_offset == $this->_array_size)
145 for (
$i = $this->_offset;
$i < $end; ++
$i)
147 if (!isset($this->_array[
$i]))
151 $arrays[] = $this->_array[
$i];
155 foreach ($arrays as $array)
157 $chars .= implode(
'', array_map(
'chr', $array));
170 if ($this->_offset == $this->_array_size)
176 for (
$i = $this->_offset;
$i < $end; ++
$i)
178 if (!isset($this->_array[
$i]))
182 $arrays[] = $this->_array[
$i];
185 return call_user_func_array(
'array_merge', $arrays);
194 if (!isset($this->_charReader))
196 $this->_charReader = $this->_charReaderFactory->getReaderFor(
200 $startLength = $this->_charReader->getInitialByteSize();
202 $fp = fopen(
'php://memory',
'w+b');
205 fseek($fp, 0, SEEK_SET);
215 if ($buf_len - $buf_pos < $startLength)
217 $buf = array_splice($buffer, $buf_pos);
221 $buffer = array_merge($buf, $new);
222 $buf_len = count($buffer);
230 if ($buf_len - $buf_pos > 0)
233 for (
$i = 0;
$i < $startLength && isset($buffer[$buf_pos]); ++
$i)
236 $bytes[] = $buffer[$buf_pos++];
238 $need = $this->_charReader->validateByteSequence(
242 if ($buf_len - $buf_pos < $need)
248 $buffer = array_merge($buffer, $new);
249 $buf_len = count($buffer);
252 for (
$i = 0;
$i < $need && isset($buffer[$buf_pos]); ++
$i)
254 $bytes[] = $buffer[$buf_pos++];
257 $this->_array[] = $bytes;
272 if ($charOffset > $this->_array_size)
280 $this->_offset = $charOffset;
289 $this->_array = array();
290 $this->_array_size = 0;
295 if (!feof($fp) && ($bytes = fread($fp, $len)) !==
false)
298 for (
$i = 0, $len = strlen($bytes);
$i < $len; ++
$i)
300 $buf[] = self::$_byteMap[$bytes[
$i]];
309 if (!isset(self::$_charMap))
311 self::$_charMap = array();
312 for ($byte = 0; $byte < 256; ++$byte)
314 self::$_charMap[$byte] = chr($byte);
316 self::$_byteMap = array_flip(self::$_charMap);