Cheetah
Bytearray.class.php
Go to the documentation of this file.
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3 
65 class Bytearray
66 {
76  private $_byteArray = array();
77 
87  private $_bytes = '';
88 
98  private $_lastPosition = 0;
99 
109  private $_position = 0;
110 
111 
112 
126  public function setPosition($position)
127  {
128  $this->_lastPosition = $this->_position;
129  $this->_position = $position;
130  }
131 
132 
146  public function getPosition()
147  {
148  return $this->_position;
149  }
150 
151 
166  public function getLastPosition()
167  {
168  return $this->_lastPosition;
169  }
170 
171 
185  public function writeBytes($bytes, $offset = false, $inject = false)
186  {
187  // if offset not given - set offset to lastpos + 1 (append)
188  if (!$offset) {
189  $offset = $this->getPosition();
190  }
191 
192  if ($offset == $this->getPosition()) {
193  // offset is current pos (len) +1 so just append!
194  $this->_bytes .= $bytes;
195  $this->setPosition($offset + strlen($bytes));
196 
197  } elseif ($offset > $this->getPosition()) {
198  // the offset is larger then current bytes string len
199  // fill missing bytes with null-Bytes
200  $countFillBytes = $offset - $this->getPosition();
201  for ($i = 0; $i < $countFillBytes; $i++) {
202  $fillBytes .= pack("H", 0x00);
203  }
204  $this->_bytes .= $fillBytes;
205  $this->_bytes .= $bytes;
206  $this->setPosition($offset + strlen($bytes));
207 
208  } elseif ($offset < strlen($this->_bytes)+1) {
209  // we write into existing position in byte string
210  // so we check for overwrite (default) || inject into exisiting
211  if (!$inject) {
212  // we overwrite exisiting bytes
213  // we inject bytes into existing bytes
214  $bytesB = substr($this->_bytes, 0, $offset-1);
215  $bytesA = substr($this->_bytes, ($offset-1)+strlen($bytes), strlen($this->_bytes)-(($offset-1)+strlen($bytes)));
216  $this->_bytes = $bytesB . $bytes . $bytesA;
217  $this->setPosition(strlen($this->_bytes));
218 
219  } else {
220  // we inject bytes into existing bytes
221  $bytesB = substr($this->_bytes, 0, $offset-1);
222  $bytesA = substr($this->_bytes, $offset-1, strlen($this->_bytes)-($offset-1));
223  $this->_bytes = $bytesB . $bytes . $bytesA;
224  $this->setPosition(strlen($this->_bytes) . strlen($bytes));
225  }
226  }
227  }
228 
229 
243  public function readBytes($offset, $length)
244  {
245  return substr($this->_bytes, $offset, $length);
246  }
247 
248 
262  public function readAllBytes()
263  {
264  return $this->_bytes;
265  }
266 
267 
268  public function readUnsignedInt($offset)
269  {
270  $length = 4;
271  $bytes = $this->readBytes($offset, $length);
272  $bytes = unpack('N', $bytes);
273  return $bytes[count($bytes)];
274  }
275 
276 
277  public function readUTFBytes($offset, $length = 4)
278  {
279  $bytes = $this->readBytes($offset, $length);
280  return $bytes;
281  }
282 
283 
284  public function readDouble($offset, $length = 8)
285  {
286  $bytes = $this->readBytes($offset, $length);
287  $bytes = unpack('d', $bytes);
288  return $bytes[count($bytes)];
289  }
290 
291 
292  public function bytesAvailable()
293  {
294  return strlen($this->_bytes);
295  }
296 }
297 
298 ?>
Bytearray\setPosition
setPosition($position)
Definition: Bytearray.class.php:126
Bytearray
Definition: Bytearray.class.php:66
Bytearray\readUTFBytes
readUTFBytes($offset, $length=4)
Definition: Bytearray.class.php:277
php
Bytearray\getPosition
getPosition()
Definition: Bytearray.class.php:146
Bytearray\readAllBytes
readAllBytes()
Definition: Bytearray.class.php:262
Bytearray\readBytes
readBytes($offset, $length)
Definition: Bytearray.class.php:243
Bytearray\readUnsignedInt
readUnsignedInt($offset)
Definition: Bytearray.class.php:268
Bytearray\getLastPosition
getLastPosition()
Definition: Bytearray.class.php:166
Bytearray\readDouble
readDouble($offset, $length=8)
Definition: Bytearray.class.php:284
Bytearray\bytesAvailable
bytesAvailable()
Definition: Bytearray.class.php:292
Bytearray\writeBytes
writeBytes($bytes, $offset=false, $inject=false)
Definition: Bytearray.class.php:185